Debian Codegraph @

src/Base/Stream.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25
26 #ifndef BASE_STREAM_H
27 #define BASE_STREAM_H
28
29 #ifdef __GNUC__
30 # include <cstdint>
31 #endif
32
33 #include <fstream>
34 #include <sstream>
35 #include <string>
36 #include <vector>
37 #include "FileInfo.h"
38
39
40 class QByteArray;
41 class QIODevice;
42 class QBuffer;
43 using PyObject = struct _object;
44
45 namespace Base
46 {
47
48 class BaseExport Stream
49 {
50 public:
51 enum ByteOrder
52 {
53 BigEndian,
54 LittleEndian
55 };
56
57 ByteOrder byteOrder() const;
58 void setByteOrder(ByteOrder);
59 virtual ~Stream();
60
61 protected:
62 Stream();
63 Stream(const Stream&) = default;
64 Stream(Stream&&) = default;
65 Stream& operator=(const Stream&) = default;
66 Stream& operator=(Stream&&) = default;
67
68 bool isSwapped() const
69 {
70 return _swap;
71 };
72
73 private:
74 bool _swap {false};
75 };
76
77 /**
78 * The OutputStream class provides writing of binary data to an ostream.
79 * @author Werner Mayer
80 */
81 class BaseExport OutputStream: public Stream
82 {
83 public:
84 explicit OutputStream(std::ostream& rout);
85 ~OutputStream() override;
86
87 OutputStream& operator<<(bool b);
88 OutputStream& operator<<(int8_t ch);
89 OutputStream& operator<<(uint8_t uch);
90 OutputStream& operator<<(int16_t s);
91 OutputStream& operator<<(uint16_t us);
92 OutputStream& operator<<(int32_t i);
93 OutputStream& operator<<(uint32_t ui);
94 OutputStream& operator<<(int64_t l);
95 OutputStream& operator<<(uint64_t ul);
96 OutputStream& operator<<(float f);
97 OutputStream& operator<<(double d);
98
99 OutputStream& write(const char* s, int n);
100
101 OutputStream(const OutputStream&) = delete;
102 OutputStream(OutputStream&&) = delete;
103 void operator=(const OutputStream&) = delete;
104 void operator=(OutputStream&&) = delete;
105
106 private:
107 std::ostream& _out;
108 };
109
110 /**
111 * The InputStream class provides reading of binary data from an istream.
112 * @author Werner Mayer
113 */
114 class BaseExport InputStream: public Stream
115 {
116 public:
117 explicit InputStream(std::istream& rin);
118 ~InputStream() override;
119
120 InputStream& operator>>(bool& b);
121 InputStream& operator>>(int8_t& ch);
122 InputStream& operator>>(uint8_t& uch);
123 InputStream& operator>>(int16_t& s);
124 InputStream& operator>>(uint16_t& us);
125 InputStream& operator>>(int32_t& i);
126 InputStream& operator>>(uint32_t& ui);
127 InputStream& operator>>(int64_t& l);
128 InputStream& operator>>(uint64_t& ul);
129 InputStream& operator>>(float& f);
130 InputStream& operator>>(double& d);
131
132 InputStream& read(char* s, int n);
133
134 explicit operator bool() const
135 {
136 // test if _Ipfx succeeded
137 return !_in.eof();
138 }
139
140 InputStream(const InputStream&) = delete;
141 InputStream(InputStream&&) = delete;
142 void operator=(const InputStream&) = delete;
143 void operator=(InputStream&&) = delete;
144
145 private:
146 std::istream& _in;
147 };
148
149 /**
150 * The TextInputStream class provides reading of ASCII data from an istream, with custom handling
151 * for std::string to make it easier to read a single multi-line (or multi-word) string. This is
152 * designed for easy compatibility with the LinkStage3 implementation of the InputStream class, used
153 * to store StringHashers for the toponaming mitigation technique.
154 */
155 class BaseExport TextInputStream: public Stream
156 {
157 public:
158 /** Constructor
159 * @param rin: upstream input
160 */
161 explicit TextInputStream(std::istream& rin)
162 : _in(rin)
163 {}
164
165 TextInputStream(const TextInputStream&) = delete;
166
167 TextInputStream(const TextInputStream&&) noexcept = delete;
168
169 void operator=(const TextInputStream&) = delete;
170
171 void operator=(const TextInputStream&&) = delete;
172
173 ~TextInputStream() override = default;
174
175 TextInputStream& operator>>(bool& input)
176 {
177 _in >> input;
178 return *this;
179 }
180
181 TextInputStream& operator>>(int8_t& ch)
182 {
183 int index {};
184 _in >> index;
185 ch = (int8_t)index;
186 return *this;
187 }
188
189 TextInputStream& operator>>(uint8_t& uch)
190 {
191 unsigned uns {};
192 _in >> uns;
193 uch = (uint8_t)uns;
194 return *this;
195 }
196
197 TextInputStream& operator>>(int16_t& int16)
198 {
199 _in >> int16;
200 return *this;
201 }
202
203 TextInputStream& operator>>(uint16_t& us)
204 {
205 _in >> us;
206 return *this;
207 }
208
209 TextInputStream& operator>>(int32_t& int32)
210 {
211 _in >> int32;
212 return *this;
213 }
214
215 TextInputStream& operator>>(uint32_t& ui)
216 {
217 _in >> ui;
218 return *this;
219 }
220
221 TextInputStream& operator>>(int64_t& int64)
222 {
223 _in >> int64;
224 return *this;
225 }
226
227 TextInputStream& operator>>(uint64_t& ul)
228 {
229 _in >> ul;
230 return *this;
231 }
232
233 TextInputStream& operator>>(float& flt)
234 {
235 _in >> flt;
236 return *this;
237 }
238
239 TextInputStream& operator>>(double& dbl)
240 {
241 _in >> dbl;
242 return *this;
243 }
244
245 TextInputStream& operator>>(std::string& str);
246
247 TextInputStream& operator>>(char& chr)
248 {
249 chr = (char)_in.get();
250 return *this;
251 }
252
253 explicit operator bool() const
254 {
255 // test if _Ipfx succeeded
256 return !_in.eof();
257 }
258
259 private:
260 std::istream& _in;
261 std::ostringstream _ss;
262 };
263
264 /**
265 * The TextOutputStream class provides writing of ASCII data to an ostream, with custom handling
266 * for std::string to make it easier to write a single multi-line (or multi-word) string. This is
267 * designed for easy compatibility with the LinkStage3 implementation of the OutputStream class,
268 * used to store StringHashers for the toponaming mitigation technique.
269 */
270 class BaseExport TextOutputStream: public Stream
271 {
272 public:
273 /** Constructor
274 * @param rout: upstream output
275 */
276 explicit TextOutputStream(std::ostream& rout)
277 : _out(rout)
278 {}
279
280 TextOutputStream(const TextOutputStream&) = delete;
281
282 TextOutputStream(const TextOutputStream&&) noexcept = delete;
283
284 void operator=(const TextOutputStream&) = delete;
285
286 void operator=(const TextOutputStream&&) = delete;
287
288 ~TextOutputStream() override = default;
289
290 template<typename T>
291 TextOutputStream& operator<<(T object)
292 {
293 _out << object << '\n';
294 return *this;
295 }
296
297 TextOutputStream& operator<<(const char* object)
298 {
299 std::string_view str(object);
300
301 // Count the lines so that we can deal with potential EOL conversions by external software
302 uint32_t lineCount = 0;
303 for (const auto character : str) {
304 if (character == '\n') {
305 ++lineCount;
306 }
307 }
308 // Stores the line count followed by a colon as the delimiter. We don't use
309 // whitespace because the input stream may also start with whitespace.
310 _out << lineCount << ':';
311
312 // Store the text, and normalize the end of line to a single '\n'.
313 bool foundSlashR = false;
314 for (const auto character : str) {
315 if (character == '\r') {
316 foundSlashR = true;
317 continue;
318 }
319 if (foundSlashR) {
320 if (character != '\n') {
321 // We allow '\r' if it is not at the end of a line
322 _out.put('\r');
323 }
324 foundSlashR = false; // Reset for next time
325 }
326 _out.put(character);
327 }
328
329 // Add an extra newline as the delimiter for the following data.
330 _out.put('\n');
331 return *this;
332 }
333
334 TextOutputStream& operator<<(const std::string& object)
335 {
336 return (*this) << object.c_str();
337 }
338
339 TextOutputStream& operator<<(char object)
340 {
341 _out.put(object);
342 return *this;
343 }
344
345 explicit operator bool() const
346 {
347 // test if _Ipfx succeeded
348 return !_out.eof();
349 }
350
351 private:
352 std::ostream& _out;
353 };
354
355 // ----------------------------------------------------------------------------
356
357 /**
358 * This class implements the streambuf interface to write data to a QByteArray.
359 * This class can only be used for writing but not for reading purposes.
360 * @author Werner Mayer
361 */
362 class BaseExport ByteArrayOStreambuf: public std::streambuf
363 {
364 public:
365 explicit ByteArrayOStreambuf(QByteArray& ba);
366 ~ByteArrayOStreambuf() override;
367
368 protected:
369 int_type overflow(std::streambuf::int_type c) override;
370 std::streamsize xsputn(const char* s, std::streamsize num) override;
371 pos_type seekoff(
372 std::streambuf::off_type off,
373 std::ios_base::seekdir way,
374 std::ios_base::openmode which = std::ios::in | std::ios::out
375 ) override;
376 pos_type seekpos(
377 std::streambuf::pos_type pos,
378 std::ios_base::openmode which = std::ios::in | std::ios::out
379 ) override;
380
381 public:
382 ByteArrayOStreambuf(const ByteArrayOStreambuf&) = delete;
383 ByteArrayOStreambuf(ByteArrayOStreambuf&&) = delete;
384 ByteArrayOStreambuf& operator=(const ByteArrayOStreambuf&) = delete;
385 ByteArrayOStreambuf& operator=(ByteArrayOStreambuf&&) = delete;
386
387 private:
388 QBuffer* _buffer;
389 };
390
391 /**
392 * This class implements the streambuf interface to read data from a QByteArray.
393 * This class can only be used for reading but not for writing purposes.
394 * @author Werner Mayer
395 */
396 class BaseExport ByteArrayIStreambuf: public std::streambuf
397 {
398 public:
399 explicit ByteArrayIStreambuf(const QByteArray& data);
400 ~ByteArrayIStreambuf() override;
401
402 protected:
403 int_type uflow() override;
404 int_type underflow() override;
405 int_type pbackfail(int_type ch) override;
406 std::streamsize showmanyc() override;
407 pos_type seekoff(
408 std::streambuf::off_type off,
409 std::ios_base::seekdir way,
410 std::ios_base::openmode which = std::ios::in | std::ios::out
411 ) override;
412 pos_type seekpos(
413 std::streambuf::pos_type pos,
414 std::ios_base::openmode which = std::ios::in | std::ios::out
415 ) override;
416
417 public:
418 ByteArrayIStreambuf(const ByteArrayIStreambuf&) = delete;
419 ByteArrayIStreambuf(ByteArrayIStreambuf&&) = delete;
420 ByteArrayIStreambuf& operator=(const ByteArrayIStreambuf&) = delete;
421 ByteArrayIStreambuf& operator=(ByteArrayIStreambuf&&) = delete;
422
423 private:
424 const QByteArray& _buffer;
425 int _beg, _end, _cur;
426 };
427
428 /**
429 * Simple class to write data directly into Qt's QIODevice.
430 * This class can only be used for writing but not reading purposes.
431 * @author Werner Mayer
432 */
433 class BaseExport IODeviceOStreambuf: public std::streambuf
434 {
435 public:
436 explicit IODeviceOStreambuf(QIODevice* dev);
437 ~IODeviceOStreambuf() override;
438
439 protected:
440 int_type overflow(std::streambuf::int_type c) override;
441 std::streamsize xsputn(const char* s, std::streamsize num) override;
442 pos_type seekoff(
443 std::streambuf::off_type off,
444 std::ios_base::seekdir way,
445 std::ios_base::openmode which = std::ios::in | std::ios::out
446 ) override;
447 pos_type seekpos(
448 std::streambuf::pos_type pos,
449 std::ios_base::openmode which = std::ios::in | std::ios::out
450 ) override;
451
452 public:
453 IODeviceOStreambuf(const IODeviceOStreambuf&) = delete;
454 IODeviceOStreambuf(IODeviceOStreambuf&&) = delete;
455 IODeviceOStreambuf& operator=(const IODeviceOStreambuf&) = delete;
456 IODeviceOStreambuf& operator=(IODeviceOStreambuf&&) = delete;
457
458 private:
459 QIODevice* device;
460 };
461
462 /**
463 * Simple class to read data directly from Qt's QIODevice.
464 * This class can only be used for readihg but not writing purposes.
465 * @author Werner Mayer
466 */
467 class BaseExport IODeviceIStreambuf: public std::streambuf
468 {
469 public:
470 explicit IODeviceIStreambuf(QIODevice* dev);
471 ~IODeviceIStreambuf() override;
472
473 protected:
474 int_type underflow() override;
475 pos_type seekoff(
476 std::streambuf::off_type off,
477 std::ios_base::seekdir way,
478 std::ios_base::openmode which = std::ios::in | std::ios::out
479 ) override;
480 pos_type seekpos(
481 std::streambuf::pos_type pos,
482 std::ios_base::openmode which = std::ios::in | std::ios::out
483 ) override;
484
485 public:
486 IODeviceIStreambuf(const IODeviceIStreambuf&) = delete;
487 IODeviceIStreambuf(IODeviceIStreambuf&&) = delete;
488 IODeviceIStreambuf& operator=(const IODeviceIStreambuf&) = delete;
489 IODeviceIStreambuf& operator=(IODeviceIStreambuf&&) = delete;
490
491 private:
492 QIODevice* device;
493 /* data buffer:
494 * - at most, pbSize characters in putback area plus
495 * - at most, bufSize characters in ordinary read buffer
496 */
497 static const int pbSize = 4; // size of putback area
498 static const int bufSize = 1024; // size of the data buffer
499 char buffer[bufSize + pbSize] {}; // data buffer
500 };
501
502 class BaseExport PyStreambuf: public std::streambuf
503 {
504 using int_type = std::streambuf::int_type;
505 using pos_type = std::streambuf::pos_type;
506 using off_type = std::streambuf::off_type;
507 using seekdir = std::ios::seekdir;
508 using openmode = std::ios::openmode;
509
510 public:
511 enum Type
512 {
513 StringIO,
514 BytesIO,
515 Unknown
516 };
517
518 explicit PyStreambuf(PyObject* o, std::size_t buf_size = 256, std::size_t put_back = 8);
519 ~PyStreambuf() override;
520 void setType(Type t)
521 {
522 type = t;
523 }
524
525 protected:
526 int_type underflow() override;
527 int_type overflow(int_type c = EOF) override;
528 std::streamsize xsputn(const char* s, std::streamsize num) override;
529 int sync() override;
530 pos_type seekoff(off_type offset, seekdir dir, openmode mode) override;
531 pos_type seekpos(pos_type offset, openmode mode) override;
532
533 private:
534 bool flushBuffer();
535 bool writeStr(const char* s, std::streamsize num);
536
537 public:
538 PyStreambuf(const PyStreambuf&) = delete;
539 PyStreambuf(PyStreambuf&&) = delete;
540 PyStreambuf& operator=(const PyStreambuf&) = delete;
541 PyStreambuf& operator=(PyStreambuf&&) = delete;
542
543 private:
544 PyObject* inp;
545 Type type {Unknown};
546 const std::size_t put_back;
547 std::vector<char> buffer;
548 };
549
550 class BaseExport Streambuf: public std::streambuf
551 {
552 public:
553 explicit Streambuf(const std::string& data);
554 ~Streambuf() override;
555
556 protected:
557 int_type uflow() override;
558 int_type underflow() override;
559 int_type pbackfail(int_type ch) override;
560 std::streamsize showmanyc() override;
561 pos_type seekoff(
562 std::streambuf::off_type off,
563 std::ios_base::seekdir way,
564 std::ios_base::openmode which = std::ios::in | std::ios::out
565 ) override;
566 pos_type seekpos(
567 std::streambuf::pos_type pos,
568 std::ios_base::openmode which = std::ios::in | std::ios::out
569 ) override;
570
571 public:
572 Streambuf(const Streambuf&) = delete;
573 Streambuf(Streambuf&&) = delete;
574 Streambuf& operator=(const Streambuf&) = delete;
575 Streambuf& operator=(Streambuf&&) = delete;
576
577 private:
578 std::string::const_iterator _beg;
579 std::string::const_iterator _end;
580 std::string::const_iterator _cur;
581 };
582
583 // ----------------------------------------------------------------------------
584
585 class FileInfo;
586
587 /**
588 * The ofstream class is provided for convenience. On Windows
589 * platforms it opens a stream with UCS-2 encoded file name
590 * while on Linux platforms the file name is UTF-8 encoded.
591 * @author Werner Mayer
592 */
593 class ofstream: public std::ofstream
594 {
595 public:
596 ofstream() = default;
597 ofstream(const ofstream&) = delete;
598 ofstream(ofstream&&) = delete;
599 explicit ofstream(const FileInfo& fi, ios_base::openmode mode = std::ios::out | std::ios::trunc)
600 #ifdef _MSC_VER
601 : std::ofstream(fi.toStdWString().c_str(), mode) {}
602 #else
603 : std::ofstream(fi.filePath().c_str(), mode)
604 {
605 }
606 #endif
607 ~ofstream() override
608 = default;
609 void open(const FileInfo& fi, ios_base::openmode mode = std::ios::out | std::ios::trunc)
610 {
611 #ifdef _MSC_VER
612 std::ofstream::open(fi.toStdWString().c_str(), mode);
613 #else
614 std::ofstream::open(fi.filePath().c_str(), mode);
615 #endif
616 }
617
618 ofstream& operator=(const ofstream&) = delete;
619 ofstream& operator=(ofstream&&) = delete;
620 };
621
622 /**
623 * The ofstream class is provided for convenience. On Windows
624 * platforms it opens a stream with UCS-2 encoded file name
625 * while on Linux platforms the file name is UTF-8 encoded.
626 * @author Werner Mayer
627 */
628 class ifstream: public std::ifstream
629 {
630 public:
631 ifstream() = default;
632 ifstream(const ifstream&) = delete;
633 ifstream(ifstream&&) = delete;
634 explicit ifstream(const FileInfo& fi, ios_base::openmode mode = std::ios::in)
635 #ifdef _MSC_VER
636 : std::ifstream(fi.toStdWString().c_str(), mode) {}
637 #else
638 : std::ifstream(fi.filePath().c_str(), mode)
639 {
640 }
641 #endif
642 ~ifstream() override
643 = default;
644 void open(const FileInfo& fi, ios_base::openmode mode = std::ios::in)
645 {
646 #ifdef _MSC_VER
647 std::ifstream::open(fi.toStdWString().c_str(), mode);
648 #else
649 std::ifstream::open(fi.filePath().c_str(), mode);
650 #endif
651 }
652
653 ifstream& operator=(const ifstream&) = delete;
654 ifstream& operator=(ifstream&&) = delete;
655 };
656
657 } // namespace Base
658
659 #endif // BASE_STREAM_H