diff --git a/libs/console/piscreen.cpp b/libs/console/piscreen.cpp index 6ee52ebe..fa8388a2 100644 --- a/libs/console/piscreen.cpp +++ b/libs/console/piscreen.cpp @@ -51,6 +51,18 @@ PRIVATE_DEFINITION_END(PIScreen::SystemConsole) PIScreen::SystemConsole::SystemConsole() { width = height = pwidth = pheight = 0; mouse_x = mouse_y = -1; +} + + +PIScreen::SystemConsole::~SystemConsole() { +#ifdef WINDOWS + SetConsoleMode(PRIVATE->hOut, PRIVATE->smode); + SetConsoleTextAttribute(PRIVATE->hOut, PRIVATE->dattr); +#endif +} + + +void PIScreen::SystemConsole::begin() { int w, h; #ifdef WINDOWS PRIVATE->ulcoord.X = 0; @@ -74,18 +86,6 @@ PIScreen::SystemConsole::SystemConsole() { # endif #endif resize(w, h); -} - - -PIScreen::SystemConsole::~SystemConsole() { -#ifdef WINDOWS - SetConsoleMode(PRIVATE->hOut, PRIVATE->smode); - SetConsoleTextAttribute(PRIVATE->hOut, PRIVATE->dattr); -#endif -} - - -void PIScreen::SystemConsole::begin() { #ifdef WINDOWS SetConsoleMode(PRIVATE->hOut, ENABLE_WRAP_AT_EOL_OUTPUT); GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->sbi); @@ -93,6 +93,7 @@ void PIScreen::SystemConsole::begin() { PRIVATE->bc.Y = 0; #endif clear(); + clearScreen(); hideCursor(); } diff --git a/libs/main/core/pibinarystream.h b/libs/main/core/pibinarystream.h index 35625665..592c53f0 100644 --- a/libs/main/core/pibinarystream.h +++ b/libs/main/core/pibinarystream.h @@ -69,8 +69,24 @@ public: } template void binaryStreamAppend(T v) {binaryStreamAppend(&v, sizeof(v));} - uchar binaryStreamTakeByte() {uchar r = 0; binaryStreamTake(&r, sizeof(r)); return r;} - int binaryStreamTakeInt() {int r = 0; binaryStreamTake(&r, sizeof(r)); return r;} + uchar binaryStreamTakeByte(bool * ok = nullptr) { + uchar r = 0; + if (binaryStreamTake(&r, sizeof(r))) { + if (ok) *ok = true; + } else { + if (ok) *ok = false; + } + return r; + } + int binaryStreamTakeInt(bool * ok = nullptr) { + int r = 0; + if (binaryStreamTake(&r, sizeof(r))) { + if (ok) *ok = true; + } else { + if (ok) *ok = false; + } + return r; + } }; diff --git a/libs/main/core/pitextstream.h b/libs/main/core/pitextstream.h new file mode 100644 index 00000000..2abe38f9 --- /dev/null +++ b/libs/main/core/pitextstream.h @@ -0,0 +1,92 @@ +/*! \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 . +*/ + +#ifndef PITEXTSTREAM_H +#define PITEXTSTREAM_H + +#include "pistring.h" + + +//! \ingroup Core +//! \~\brief +//! \~english Text serialization interface. +//! \~russian Интерфейс текстовой сериализации. +template +class PITextStream { +public: + PITextStream(PIBinaryStream

* stream): s(stream) {} + + PITextStream

& newLine() {s->binaryStreamAppend('\n'); return *this;} + PITextStream

& 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

* 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 diff --git a/libs/main/io_devices/piiostream.h b/libs/main/io_devices/piiostream.h new file mode 100644 index 00000000..70e23ec0 --- /dev/null +++ b/libs/main/io_devices/piiostream.h @@ -0,0 +1,59 @@ +/*! \file piiostream.h + * \ingroup IO + * \~\brief + * \~english PIBinaryStream functionality for PIIODevice + * \~russian Функциональность PIBinaryStream для PIIODevice + */ +/* + PIP - Platform Independent Primitives + PIBinaryStream functionality for PIIODevice + 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 PIIOSTREAM_H +#define PIIOSTREAM_H + +#include "piiodevice.h" + + +//! \ingroup IO +//! \~\brief +//! \~english PIBinaryStream functionality for PIIODevice. +//! \~russian Функциональность PIBinaryStream для PIIODevice. +class PIP_EXPORT PIIOBinaryStream: public PIBinaryStream { +public: + + //! \~english Contructs %PIIOBinaryStream for "device" device + //! \~russian Создает %PIIOBinaryStream для устройства "device" + PIIOBinaryStream(PIIODevice * device): dev(device) {} + + bool binaryStreamAppendImp(const void * d, size_t s) { + if (!dev) return false; + return dev->write(d, s); + } + + bool binaryStreamTakeImp(void * d, size_t s) { + if (!dev) return false; + return dev->read(d, s); + } + +private: + PIIODevice * dev; + +}; + + +#endif // PIIOSTREAM_H diff --git a/libs/main/io_devices/pipeer.cpp b/libs/main/io_devices/pipeer.cpp index 96a07810..40b2a91e 100644 --- a/libs/main/io_devices/pipeer.cpp +++ b/libs/main/io_devices/pipeer.cpp @@ -573,7 +573,7 @@ bool PIPeer::mbcastRead(uchar * data, int size) { if (type <= 0 || type >= 4) return true; PeerInfo pi; ba >> pi.name; -// piCoutObj << "received mb from" << pi.name << "packet" << type; + //piCout << "received mb from" << pi.name << "packet" << type; if (pi.name == self_info.name) return true; PIMutexLocker locker(mc_mutex); diag_s.received(size); @@ -789,6 +789,7 @@ void PIPeer::sendPeerInfo(const PeerInfo & info) { void PIPeer::sendPeerRemove(const PIString & peer) { + //piCout << name() << "sendPeerRemove" << peer; PIByteArray ba; ba << int(2) << peer; sendMBcast(ba); @@ -858,7 +859,7 @@ void PIPeer::syncPeers() { PeerInfo & cp(peers[i]); if (cp.sync > 3) { pn = cp.name; - //piCoutObj << "sync: remove " << pn; + //piCout << "sync: remove " << pn; cp.destroy(); addToRemoved(cp); peers.remove(i); diff --git a/main.cpp b/main.cpp index 477dab6c..f409fc61 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,7 @@ #include "pip.h" #include "pibinarystream.h" +#include "pitextstream.h" +#include "piiostream.h" //#include "stream.h" //#include "ccm.h" //#include "pisystemmonitor_.h" @@ -33,8 +35,23 @@ PICout operator << (PICout c, const TS & v) {c << '{' << v.i << ", " << v.f << ' template inline PIBinaryStream

& operator <<(PIBinaryStream

& s, const TS & v) {s << v.i; return s;} template inline PIBinaryStream

& operator >>(PIBinaryStream

& s, TS & v) {s >> v.i; return s;} + int main(int argc, char * argv[]) { PIByteArray ba; + PIFile f; + f.open("_", PIIODevice::ReadWrite); + f.clear(); + PIIOBinaryStream ios(&f); + auto ts = createPITextStream(&ios); + + ts << "hello" << uchar('1'); + ts.space(); + (ts << PIString::fromUTF8("№") << 123).space() << -0.1f; + ts.newLine(); + + piCout << PIString(ba); + //PIIOByteArray ioba(&ba); + //PIIOBinaryStream stream(&ioba); /*ProcessStatsFixed_ ps; ba << ps; piCout << "s" << ba.data;*/ @@ -53,7 +70,8 @@ int main(int argc, char * argv[]) { ba >> vi; piCout << "res" << vi; piCout << "r" << ba.data;*/ - PIMap map; + + /*PIMap map; map[PIIODevice::ReadOnly] = {"str0", PIString::fromUTF8("русский!")}; piMSleep(100); map[PIIODevice::ReadWrite] = {""}; @@ -61,13 +79,14 @@ int main(int argc, char * argv[]) { map[PIIODevice::WriteOnly] = {PIString::fromUTF8("русский!"), "", "1234567890", "qwertyuiop[]"}; piCout << map; - ba << map; + stream << map; piCout << ba; map.clear(); - ba >> map; + ioba.seekToBegin(); + stream >> map; piCout << map; - piCout << ba; + piCout << ba;*/ return 0; } diff --git a/utils/system_daemon/main.cpp b/utils/system_daemon/main.cpp index dc74f203..ba1f2ba8 100755 --- a/utils/system_daemon/main.cpp +++ b/utils/system_daemon/main.cpp @@ -394,7 +394,7 @@ int main(int argc, char * argv[]) { MainMenu * menu = new MainMenu(*daemon); if (sapp) CONNECTU(sapp, messageReceived, menu, messageFromApp); if (cli.hasArgument("silent")) { - PICout::setBufferActive(false); + PICout::setOutputDevices(PICout::StdOut); PIKbdListener ls; ls.enableExitCapture(PIKbdListener::F10); ls.start();