130 lines
5.0 KiB
C++
130 lines
5.0 KiB
C++
/*! \file pitextstream.h
|
|
* \ingroup Core
|
|
* \~\brief
|
|
* \~english Text serialization functionality over PIBinaryStream
|
|
* \~russian Функциональность текстовой сериализации поверх PIBinaryStream
|
|
*/
|
|
/*
|
|
PIP - Platform Independent Primitives
|
|
Text serialization functionality over PIBinaryStream
|
|
Ivan Pelipenko peri4ko@yandex.ru
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef PITEXTSTREAM_H
|
|
#define PITEXTSTREAM_H
|
|
|
|
#include "pistring.h"
|
|
|
|
|
|
//! \ingroup Core
|
|
//! \~\brief
|
|
//! \~english Text serialization functionality over PIBinaryStream.
|
|
//! \~russian Функциональность текстовой сериализации поверх PIBinaryStream.
|
|
template<typename P>
|
|
class PITextStream {
|
|
public:
|
|
PITextStream(PIBinaryStream<P> * stream): s(stream) {}
|
|
|
|
PITextStream<P> & space() {s->binaryStreamAppend(' '); return *this;}
|
|
PITextStream<P> & newLine() {s->binaryStreamAppend('\n'); return *this;}
|
|
void append(const PIString & v) {
|
|
if (v.isEmpty()) return;
|
|
auto utf8 = v.toUTF8();
|
|
s->binaryStreamAppend(utf8.data(), utf8.size());
|
|
}
|
|
void append(const PIConstChars & v) {if (!v.isEmpty()) s->binaryStreamAppend(v.data(), v.size());}
|
|
void append(char v) {s->binaryStreamAppend(v);}
|
|
void append(const char * v) {append(PIConstChars(v));}
|
|
void append(bool v) {append(v ? "true" : "false");}
|
|
void append(int v) {append(PIString::fromNumber(v));}
|
|
void append(llong v) {append(PIString::fromNumber(v));}
|
|
void append(float v) {append(PIString::fromNumber(v));}
|
|
void append(double v) {append(PIString::fromNumber(v));}
|
|
|
|
char takeChar(bool * ok) {return (char)s->binaryStreamTakeByte(ok);}
|
|
|
|
PIString takeLine() {
|
|
PIByteArray ret;
|
|
bool ok = true;
|
|
for (;;) {
|
|
char b = takeChar(&ok);
|
|
if (!ok || b == '\n') break;
|
|
if (b != '\r')
|
|
ret.append((uchar)b);
|
|
}
|
|
return PIString::fromUTF8(ret);
|
|
}
|
|
PIString takeWord() {
|
|
static PIConstChars spaces(" \t\n\r");
|
|
return takeUntil(spaces);
|
|
}
|
|
PIString takeCWord() {
|
|
static PIConstChars chars(" \t\n\r:;%$&#@!?~/*-+=.,\\\"'`[](){}<>");
|
|
return takeUntil(chars);
|
|
}
|
|
|
|
private:
|
|
PIString takeUntil(const PIConstChars & chars) {
|
|
static PIConstChars spaces(" \t\n\r");
|
|
bool ok = true;
|
|
char c = skipWhile(spaces, &ok);
|
|
if (!ok || chars.contains(c)) return PIString();
|
|
PIByteArray ret;
|
|
ret.append((uchar)c);
|
|
for (;;) {
|
|
c = takeChar(&ok);
|
|
if (!ok || chars.contains(c)) break;
|
|
ret.append((uchar)c);
|
|
}
|
|
return PIString::fromUTF8(ret);
|
|
}
|
|
// returns first non-"chars" char
|
|
char skipWhile(const PIConstChars & chars, bool * rok) {
|
|
bool ok = true;
|
|
char c = 0;
|
|
for (;;) {
|
|
c = takeChar(&ok);
|
|
if (!ok || !chars.contains(c)) break;
|
|
}
|
|
if (rok) *rok = ok;
|
|
return c;
|
|
}
|
|
|
|
PIBinaryStream<P> * s;
|
|
|
|
};
|
|
|
|
template<typename P>
|
|
inline PITextStream<P> createPITextStream(PIBinaryStream<P> * stream) {return PITextStream<P>(stream);}
|
|
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, bool v) {s.append(v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, char v) {s.append(v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, uchar v) {s.append((int)v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, short v) {s.append((int)v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, ushort v) {s.append((int)v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, int v) {s.append((int)v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, uint v) {s.append((int)v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, llong v) {s.append((llong)v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, ullong v) {s.append((llong)v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, float v) {s.append(v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, double v) {s.append(v); return s;}
|
|
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, const char * v) {s.append(v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, const PIConstChars & v) {s.append(v); return s;}
|
|
template<typename P> inline PITextStream<P> & operator <<(PITextStream<P> & s, const PIString & v) {s.append(v); return s;}
|
|
|
|
#endif
|