Files
pip/libs/main/core/pitextstream.h
2022-05-10 18:47:11 +03:00

93 lines
4.0 KiB
C++

/*! \file pitextstream.h
* \ingroup Core
* \~\brief
* \~english Text serialization interface
* \~russian Интерфейс текстовой сериализации
*/
/*
PIP - Platform Independent Primitives
Text serialization interface
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 interface.
//! \~russian Интерфейс текстовой сериализации.
template<typename P>
class PITextStream {
public:
PITextStream(PIBinaryStream<P> * stream): s(stream) {}
PITextStream<P> & newLine() {s->binaryStreamAppend('\n'); return *this;}
PITextStream<P> & space() {s->binaryStreamAppend(' '); 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);}
bool textStreamSkipSpaces() {
//if ()
}
PIString textStreamTakeLine() {
}
PIString textStreamTakeWord();
private:
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