Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
inkscapestream.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2#ifndef SEEN_INKSCAPE_IO_INKSCAPESTREAM_H
3#define SEEN_INKSCAPE_IO_INKSCAPESTREAM_H
4/*
5 * Authors:
6 * Bob Jamison <rjamison@titan.com>
7 *
8 * Copyright (C) 2004 Inkscape.org
9 *
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#include <cstdio>
14#include <glibmm/ustring.h>
15
16#ifdef printf
17#undef printf
18#endif
19
20namespace Inkscape
21{
22namespace IO
23{
24
25class StreamException : public std::exception
26{
27public:
28 StreamException(Glib::ustring theReason) noexcept
29 : reason(std::move(theReason))
30 {}
31 ~StreamException() noexcept override
32 = default;
33 char const *what() const noexcept override
34 { return reason.c_str(); }
35
36private:
37 Glib::ustring reason;
38
39};
40
41//#########################################################################
42//# I N P U T S T R E A M
43//#########################################################################
44
53{
54
55public:
56
60 InputStream() = default;
61
65 virtual ~InputStream() = default;
66
71 virtual int available() = 0;
72
78 virtual void close() = 0;
79
89 virtual int get() = 0;
90
91}; // class InputStream
92
93
94
95
102{
103
104public:
105
106 BasicInputStream(InputStream &sourceStream);
107
108 ~BasicInputStream() override = default;
109
110 int available() override;
111
112 void close() override;
113
114 int get() override;
115
116protected:
117
118 bool closed;
119
121
122private:
123
124
125}; // class BasicInputStream
126
127
128
133{
134public:
135
136 int available() override
137 { return 0; }
138
139 void close() override
140 { /* do nothing */ }
141
142 int get() override
143 { return getchar(); }
144
145};
146
147
148
149
150
151
152//#########################################################################
153//# O U T P U T S T R E A M
154//#########################################################################
155
163{
164
165public:
166
170 OutputStream() = default;
171
175 virtual ~OutputStream() = default;
176
183 virtual void close() = 0;
184
190 virtual void flush() = 0;
191
195 virtual int put(char ch) = 0;
196
197
198}; // class OutputStream
199
200
206{
207
208public:
209
210 BasicOutputStream(OutputStream &destinationStream);
211
212 ~BasicOutputStream() override = default;
213
214 void close() override;
215
216 void flush() override;
217
218 int put(char ch) override;
219
220protected:
221
222 bool closed;
223
225
226
227}; // class BasicOutputStream
228
229
230
235{
236public:
237
238 void close() override
239 { }
240
241 void flush() override
242 { }
243
244 int put(char ch) override
245 {return putchar(ch); }
246
247};
248
249
250
251
252//#########################################################################
253//# R E A D E R
254//#########################################################################
255
256
262{
263
264public:
265
269 Reader() = default;
270
274 virtual ~Reader() = default;
275
276
277 virtual int available() = 0;
278
279 virtual void close() = 0;
280
281 virtual char get() = 0;
282
283 virtual Glib::ustring readLine() = 0;
284
285 virtual Glib::ustring readWord() = 0;
286
287 /* Input formatting */
288 virtual const Reader& readBool (bool& val ) = 0;
289 virtual const Reader& operator>> (bool& val ) = 0;
290
291 virtual const Reader& readShort (short &val) = 0;
292 virtual const Reader& operator>> (short &val) = 0;
293
294 virtual const Reader& readUnsignedShort (unsigned short &val) = 0;
295 virtual const Reader& operator>> (unsigned short &val) = 0;
296
297 virtual const Reader& readInt (int &val) = 0;
298 virtual const Reader& operator>> (int &val) = 0;
299
300 virtual const Reader& readUnsignedInt (unsigned int &val) = 0;
301 virtual const Reader& operator>> (unsigned int &val) = 0;
302
303 virtual const Reader& readLong (long &val) = 0;
304 virtual const Reader& operator>> (long &val) = 0;
305
306 virtual const Reader& readUnsignedLong (unsigned long &val) = 0;
307 virtual const Reader& operator>> (unsigned long &val) = 0;
308
309 virtual const Reader& readFloat (float &val) = 0;
310 virtual const Reader& operator>> (float &val) = 0;
311
312 virtual const Reader& readDouble (double &val) = 0;
313 virtual const Reader& operator>> (double &val) = 0;
314
315}; // interface Reader
316
317
318
323class BasicReader : public Reader
324{
325
326public:
327
328 BasicReader(Reader &sourceStream);
329
330 ~BasicReader() override = default;
331
332 int available() override;
333
334 void close() override;
335
336 char get() override;
337
338 Glib::ustring readLine() override;
339
340 Glib::ustring readWord() override;
341
342 /* Input formatting */
343 const Reader& readBool (bool& val ) override;
344 const Reader& operator>> (bool& val ) override
345 { return readBool(val); }
346
347 const Reader& readShort (short &val) override;
348 const Reader& operator>> (short &val) override
349 { return readShort(val); }
350
351 const Reader& readUnsignedShort (unsigned short &val) override;
352 const Reader& operator>> (unsigned short &val) override
353 { return readUnsignedShort(val); }
354
355 const Reader& readInt (int &val) override;
356 const Reader& operator>> (int &val) override
357 { return readInt(val); }
358
359 const Reader& readUnsignedInt (unsigned int &val) override;
360 const Reader& operator>> (unsigned int &val) override
361 { return readUnsignedInt(val); }
362
363 const Reader& readLong (long &val) override;
364 const Reader& operator>> (long &val) override
365 { return readLong(val); }
366
367 const Reader& readUnsignedLong (unsigned long &val) override;
368 const Reader& operator>> (unsigned long &val) override
369 { return readUnsignedLong(val); }
370
371 const Reader& readFloat (float &val) override;
372 const Reader& operator>> (float &val) override
373 { return readFloat(val); }
374
375 const Reader& readDouble (double &val) override;
376 const Reader& operator>> (double &val) override
377 { return readDouble(val); }
378
379
380protected:
381
383
385 { source = nullptr; }
386
387private:
388
389}; // class BasicReader
390
391
392
398{
399public:
400
401 InputStreamReader(InputStream &inputStreamSource);
402
403 /*Overload these 3 for your implementation*/
404 int available() override;
405
406 void close() override;
407
408 char get() override;
409
410
411private:
412
414
415
416};
417
422class StdReader : public BasicReader
423{
424public:
425
426 StdReader();
427
428 ~StdReader() override;
429
430 /*Overload these 3 for your implementation*/
431 int available() override;
432
433 void close() override;
434
435 char get() override;
436
437
438private:
439
441
442
443};
444
445
446
447
448
449//#########################################################################
450//# W R I T E R
451//#########################################################################
452
458{
459
460public:
461
465 Writer() = default;
466
470 virtual ~Writer() = default;
471
472 virtual void close() = 0;
473
474 virtual void flush() = 0;
475
476 virtual void put(char ch) = 0;
477
478 /* Formatted output */
479 virtual Writer& printf(char const *fmt, ...) G_GNUC_PRINTF(2,3) = 0;
480
481 virtual Writer& writeChar(char val) = 0;
482
483 virtual Writer& writeUString(const Glib::ustring &val) = 0;
484
485 virtual Writer& writeStdString(const std::string &val) = 0;
486
487 virtual Writer& writeString(const char *str) = 0;
488
489 virtual Writer& writeBool (bool val ) = 0;
490
491 virtual Writer& writeShort (short val ) = 0;
492
493 virtual Writer& writeUnsignedShort (unsigned short val ) = 0;
494
495 virtual Writer& writeInt (int val ) = 0;
496
497 virtual Writer& writeUnsignedInt (unsigned int val ) = 0;
498
499 virtual Writer& writeLong (long val ) = 0;
500
501 virtual Writer& writeUnsignedLong (unsigned long val ) = 0;
502
503 virtual Writer& writeFloat (float val ) = 0;
504
505 virtual Writer& writeDouble (double val ) = 0;
506
507
508
509}; // interface Writer
510
511
516class BasicWriter : public Writer
517{
518
519public:
520
521 BasicWriter(Writer &destinationWriter);
522
523 ~BasicWriter() override = default;
524
525 /*Overload these 3 for your implementation*/
526 void close() override;
527
528 void flush() override;
529
530 void put(char ch) override;
531
532
533
534 /* Formatted output */
535 Writer &printf(char const *fmt, ...) override G_GNUC_PRINTF(2,3);
536
537 Writer& writeChar(char val) override;
538
539 Writer& writeUString(const Glib::ustring &val) override;
540
541 Writer& writeStdString(const std::string &val) override;
542
543 Writer& writeString(const char *str) override;
544
545 Writer& writeBool (bool val ) override;
546
547 Writer& writeShort (short val ) override;
548
549 Writer& writeUnsignedShort (unsigned short val ) override;
550
551 Writer& writeInt (int val ) override;
552
553 Writer& writeUnsignedInt (unsigned int val ) override;
554
555 Writer& writeLong (long val ) override;
556
557 Writer& writeUnsignedLong (unsigned long val ) override;
558
559 Writer& writeFloat (float val ) override;
560
561 Writer& writeDouble (double val ) override;
562
563
564protected:
565
566 Writer *destination;
567
569 { destination = nullptr; }
570
571private:
572
573}; // class BasicWriter
574
575
576
577Writer& operator<< (Writer &writer, char val);
578
579Writer& operator<< (Writer &writer, Glib::ustring &val);
580
581Writer& operator<< (Writer &writer, std::string &val);
582
583Writer& operator<< (Writer &writer, char const *val);
584
585Writer& operator<< (Writer &writer, bool val);
586
587Writer& operator<< (Writer &writer, short val);
588
589Writer& operator<< (Writer &writer, unsigned short val);
590
591Writer& operator<< (Writer &writer, int val);
592
593Writer& operator<< (Writer &writer, unsigned int val);
594
595Writer& operator<< (Writer &writer, long val);
596
597Writer& operator<< (Writer &writer, unsigned long val);
598
599Writer& operator<< (Writer &writer, float val);
600
601Writer& operator<< (Writer &writer, double val);
602
603
604
605
611{
612public:
613
614 OutputStreamWriter(OutputStream &outputStreamDest);
615
616 /*Overload these 3 for your implementation*/
617 void close() override;
618
619 void flush() override;
620
621 void put(char ch) override;
622
623
624private:
625
627
628
629};
630
631
635class StdWriter : public BasicWriter
636{
637public:
638 StdWriter();
639
640 ~StdWriter() override;
641
642
643 void close() override;
644
645
646 void flush() override;
647
648
649 void put(char ch) override;
650
651
652private:
653
655
656};
657
658//#########################################################################
659//# U T I L I T Y
660//#########################################################################
661
662void pipeStream(InputStream &source, OutputStream &dest);
663
664
665
666} // namespace IO
667} // namespace Inkscape
668
669
670#endif // SEEN_INKSCAPE_IO_INKSCAPESTREAM_H
This is the class that most users should inherit, to provide their own streams.
int available() override
Returns the number of bytes that can be read (or skipped over) from this input stream without blockin...
~BasicInputStream() override=default
int get() override
Reads the next byte of data from the input stream.
void close() override
Closes this input stream and releases any system resources associated with the stream.
This is the class that most users should inherit, to provide their own output streams.
void flush() override
Flushes this output stream and forces any buffered output bytes to be written out.
~BasicOutputStream() override=default
void close() override
Closes this output stream and releases any system resources associated with this stream.
int put(char ch) override
Writes the specified byte to this output stream.
This class and its descendants are for unicode character-oriented input.
const Reader & readBool(bool &val) override
const Reader & operator>>(bool &val) override
const Reader & readShort(short &val) override
int available() override
Returns the number of bytes that can be read (or skipped over) from this reader without blocking by t...
const Reader & readLong(long &val) override
const Reader & readUnsignedInt(unsigned int &val) override
const Reader & readFloat(float &val) override
const Reader & readInt(int &val) override
~BasicReader() override=default
const Reader & readUnsignedLong(unsigned long &val) override
const Reader & readUnsignedShort(unsigned short &val) override
const Reader & readDouble(double &val) override
void close() override
Closes this reader and releases any system resources associated with the reader.
char get() override
Reads the next byte of data from the reader.
Glib::ustring readLine() override
Reads a line of data from the reader.
Glib::ustring readWord() override
Reads a line of data from the reader.
This class and its descendants are for unicode character-oriented output.
~BasicWriter() override=default
Class for placing a Reader on an open InputStream.
char get() override
Overloaded to receive its bytes from an InputStream rather than a Reader.
void close() override
Close the underlying OutputStream.
int available() override
Flush the underlying OutputStream.
This interface is the base of all input stream classes.
virtual void close()=0
Do whatever it takes to 'close' this input stream The most likely implementation of this method will ...
InputStream()=default
Constructor.
virtual int get()=0
Read one byte from this input stream.
virtual int available()=0
Return the number of bytes that are currently available to be read.
virtual ~InputStream()=default
Destructor.
Class for placing a Writer on an open OutputStream.
This interface is the base of all input stream classes.
virtual int put(char ch)=0
Send one byte to the destination stream.
OutputStream()=default
Constructor.
virtual void close()=0
This call should.
virtual ~OutputStream()=default
Destructor.
virtual void flush()=0
This call should push any pending data it might have to the destination stream.
This interface and its descendants are for unicode character-oriented input.
virtual int available()=0
virtual const Reader & readUnsignedLong(unsigned long &val)=0
virtual char get()=0
virtual const Reader & readUnsignedShort(unsigned short &val)=0
Reader()=default
Constructor.
virtual Glib::ustring readWord()=0
virtual const Reader & readFloat(float &val)=0
virtual const Reader & readDouble(double &val)=0
virtual const Reader & readShort(short &val)=0
virtual ~Reader()=default
Destructor.
virtual const Reader & readInt(int &val)=0
virtual void close()=0
virtual const Reader & operator>>(bool &val)=0
virtual Glib::ustring readLine()=0
virtual const Reader & readBool(bool &val)=0
virtual const Reader & readUnsignedInt(unsigned int &val)=0
virtual const Reader & readLong(long &val)=0
Convenience class for reading from standard input.
int get() override
Read one byte from this input stream.
void close() override
Do whatever it takes to 'close' this input stream The most likely implementation of this method will ...
int available() override
Return the number of bytes that are currently available to be read.
Convenience class for writing to standard output.
int put(char ch) override
Send one byte to the destination stream.
void flush() override
This call should push any pending data it might have to the destination stream.
void close() override
This call should.
Convenience class for reading formatted from standard input.
int available() override
Flush the underlying OutputStream.
char get() override
Overloaded to receive its bytes from an InputStream rather than a Reader.
void close() override
Close the underlying OutputStream.
Convenience class for writing to standard output.
OutputStream * outputStream
char const * what() const noexcept override
StreamException(Glib::ustring theReason) noexcept
~StreamException() noexcept override=default
This interface and its descendants are for unicode character-oriented output.
virtual Writer & writeDouble(double val)=0
virtual Writer & writeInt(int val)=0
virtual void flush()=0
virtual Writer & writeBool(bool val)=0
virtual Writer & printf(char const *fmt,...) G_GNUC_PRINTF(2
virtual Writer & writeUnsignedLong(unsigned long val)=0
virtual Writer & writeLong(long val)=0
virtual Writer & writeShort(short val)=0
virtual Writer & writeString(const char *str)=0
virtual Writer & writeUnsignedShort(unsigned short val)=0
virtual Writer & writeStdString(const std::string &val)=0
virtual Writer virtual Writer & writeChar(char val)=0
virtual Writer & writeUString(const Glib::ustring &val)=0
virtual ~Writer()=default
Destructor.
Writer()=default
Constructor.
virtual Writer & writeFloat(float val)=0
virtual void put(char ch)=0
virtual void close()=0
virtual Writer & writeUnsignedInt(unsigned int val)=0
void pipeStream(InputStream &source, OutputStream &dest)
Helper class to stream background task notifications as a series of messages.
STL namespace.
int G_GNUC_PRINTF(2, 3) safeprintf(char(&buf)[N]
int const char * fmt
Definition safe-printf.h:18