/*! \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 . */ #ifndef PITEXTSTREAM_H #define PITEXTSTREAM_H #include "pistring.h" //! \ingroup Core //! \~\brief //! \~english Text serialization functionality over PIBinaryStream. //! \~russian Функциональность текстовой сериализации поверх PIBinaryStream. template class PITextStream { public: PITextStream(PIBinaryStream

* stream): s(stream) {} PITextStream

& space() {s->binaryStreamAppend(' '); return *this;} PITextStream

& 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

* s; }; template inline PITextStream

createPITextStream(PIBinaryStream

* stream) {return PITextStream

(stream);} template inline PITextStream

& operator <<(PITextStream

& s, bool v) {s.append(v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, char v) {s.append(v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, uchar v) {s.append((int)v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, short v) {s.append((int)v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, ushort v) {s.append((int)v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, int v) {s.append((int)v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, uint v) {s.append((int)v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, llong v) {s.append((llong)v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, ullong v) {s.append((llong)v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, float v) {s.append(v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, double v) {s.append(v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, const char * v) {s.append(v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, const PIConstChars & v) {s.append(v); return s;} template inline PITextStream

& operator <<(PITextStream

& s, const PIString & v) {s.append(v); return s;} #endif