Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
inkscapestream.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Our base input/output stream classes. These are is directly
4 * inherited from iostreams, and includes any extra
5 * functionality that we might need.
6 *
7 * Authors:
8 * Bob Jamison <rjamison@titan.com>
9 *
10 * Copyright (C) 2004 Inkscape.org
11 *
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13 */
14
15#include <cstdlib>
16#include "inkscapestream.h"
17
18namespace Inkscape
19{
20namespace IO
21{
22
23//#########################################################################
24//# U T I L I T Y
25//#########################################################################
26
28{
29 for (;;)
30 {
31 int ch = source.get();
32 if (ch<0)
33 break;
34 dest.put(ch);
35 }
36 dest.flush();
37}
38
39//#########################################################################
40//# B A S I C I N P U T S T R E A M
41//#########################################################################
42
43
48 : source(sourceStream)
49{
50 closed = false;
51}
52
59{
60 if (closed)
61 return 0;
62 return source.available();
63}
64
65
71{
72 if (closed)
73 return;
74 source.close();
75 closed = true;
76}
77
82{
83 if (closed)
84 return -1;
85 return source.get();
86}
87
88
89
90//#########################################################################
91//# B A S I C O U T P U T S T R E A M
92//#########################################################################
93
98 : destination(destinationStream)
99{
100 closed = false;
101}
102
108{
109 if (closed)
110 return;
112 closed = true;
113}
114
120{
121 if (closed)
122 return;
124}
125
130{
131 if (closed)
132 return -1;
133 destination.put(ch);
134 return 1;
135}
136
137
138
139//#########################################################################
140//# B A S I C R E A D E R
141//#########################################################################
146{
147 source = &sourceReader;
148}
149
156{
157 if (source)
158 return source->available();
159 else
160 return 0;
161}
162
163
169{
170 if (source)
171 source->close();
172}
173
178{
179 if (source)
180 return source->get();
181 else
182 return (char)-1;
183}
184
185
190{
191 Glib::ustring str;
192 while (available() > 0)
193 {
194 char ch = get();
195 if (ch == '\n')
196 break;
197 str.push_back(ch);
198 }
199 return str;
200}
201
206{
207 Glib::ustring str;
208 while (available() > 0)
209 {
210 char ch = get();
211 if (!std::isprint(ch))
212 break;
213 str.push_back(ch);
214 }
215 return str;
216}
217
218
219static bool getLong(Glib::ustring &str, long *val)
220{
221 const char *begin = str.raw().c_str();
222 char *end;
223 long ival = strtol(begin, &end, 10);
224 if (str == end)
225 return false;
226 *val = ival;
227 return true;
228}
229
230static bool getULong(Glib::ustring &str, unsigned long *val)
231{
232 const char *begin = str.raw().c_str();
233 char *end;
234 unsigned long ival = strtoul(begin, &end, 10);
235 if (str == end)
236 return false;
237 *val = ival;
238 return true;
239}
240
241static bool getDouble(Glib::ustring &str, double *val)
242{
243 const char *begin = str.raw().c_str();
244 char *end;
245 double ival = strtod(begin, &end);
246 if (str == end)
247 return false;
248 *val = ival;
249 return true;
250}
251
252
253
254const Reader &BasicReader::readBool (bool& val )
255{
256 Glib::ustring buf = readWord();
257 if (buf == "true")
258 val = true;
259 else
260 val = false;
261 return *this;
262}
263
264const Reader &BasicReader::readShort (short& val )
265{
266 Glib::ustring buf = readWord();
267 long ival;
268 if (getLong(buf, &ival))
269 val = (short) ival;
270 return *this;
271}
272
273const Reader &BasicReader::readUnsignedShort (unsigned short& val )
274{
275 Glib::ustring buf = readWord();
276 unsigned long ival;
277 if (getULong(buf, &ival))
278 val = (unsigned short) ival;
279 return *this;
280}
281
282const Reader &BasicReader::readInt (int& val )
283{
284 Glib::ustring buf = readWord();
285 long ival;
286 if (getLong(buf, &ival))
287 val = (int) ival;
288 return *this;
289}
290
291const Reader &BasicReader::readUnsignedInt (unsigned int& val )
292{
293 Glib::ustring buf = readWord();
294 unsigned long ival;
295 if (getULong(buf, &ival))
296 val = (unsigned int) ival;
297 return *this;
298}
299
300const Reader &BasicReader::readLong (long& val )
301{
302 Glib::ustring buf = readWord();
303 long ival;
304 if (getLong(buf, &ival))
305 val = ival;
306 return *this;
307}
308
309const Reader &BasicReader::readUnsignedLong (unsigned long& val )
310{
311 Glib::ustring buf = readWord();
312 unsigned long ival;
313 if (getULong(buf, &ival))
314 val = ival;
315 return *this;
316}
317
318const Reader &BasicReader::readFloat (float& val )
319{
320 Glib::ustring buf = readWord();
321 double ival;
322 if (getDouble(buf, &ival))
323 val = (float)ival;
324 return *this;
325}
326
327const Reader &BasicReader::readDouble (double& val )
328{
329 Glib::ustring buf = readWord();
330 double ival;
331 if (getDouble(buf, &ival))
332 val = ival;
333 return *this;
334}
335
336
337
338//#########################################################################
339//# I N P U T S T R E A M R E A D E R
340//#########################################################################
341
342
344 : inputStream(inputStreamSource)
345{
346}
347
348
349
357
365
371{
372 char ch = inputStream.get();
373 return ch;
374}
375
376
377
378//#########################################################################
379//# S T D R E A D E R
380//#########################################################################
381
382
390
395{
396 delete inputStream;
397}
398
399
400
405{
407}
408
413{
414 return inputStream->available();
415}
416
422{
423 char ch = inputStream->get();
424 return ch;
425}
426
427
428
429
430
431//#########################################################################
432//# B A S I C W R I T E R
433//#########################################################################
434
439{
440 destination = &destinationWriter;
441}
442
448{
449 if (destination)
451}
452
458{
459 if (destination)
461}
462
466void BasicWriter::put(char ch)
467{
468 if (destination)
469 destination->put(ch);
470}
471
476{
477 va_list args;
478 va_start(args, fmt);
479 gchar *buf = g_strdup_vprintf(fmt, args);
480 va_end(args);
481 if (buf) {
483 g_free(buf);
484 }
485 return *this;
486}
491{
492 put(ch);
493 return *this;
494}
495
496
500Writer &BasicWriter::writeUString(const Glib::ustring &str)
501{
502 writeStdString(str.raw());
503 return *this;
504}
505
509Writer &BasicWriter::writeStdString(const std::string &str)
510{
511 for (char it : str) {
512 put(it);
513 }
514 return *this;
515}
516
521{
522 std::string tmp;
523 if (str)
524 tmp = str;
525 else
526 tmp = "null";
527 writeStdString(tmp);
528 return *this;
529}
530
531
532
533
538{
539 if (val)
540 writeString("true");
541 else
542 writeString("false");
543 return *this;
544}
545
546
551{
552 gchar *buf = g_strdup_printf("%d", val);
553 if (buf) {
555 g_free(buf);
556 }
557 return *this;
558}
559
560
561
566{
567 gchar *buf = g_strdup_printf("%u", val);
568 if (buf) {
570 g_free(buf);
571 }
572 return *this;
573}
574
579{
580 gchar *buf = g_strdup_printf("%d", val);
581 if (buf) {
583 g_free(buf);
584 }
585 return *this;
586}
587
592{
593 gchar *buf = g_strdup_printf("%u", val);
594 if (buf) {
596 g_free(buf);
597 }
598 return *this;
599}
600
605{
606 gchar *buf = g_strdup_printf("%ld", val);
607 if (buf) {
609 g_free(buf);
610 }
611 return *this;
612}
613
618{
619 gchar *buf = g_strdup_printf("%lu", val);
620 if (buf) {
622 g_free(buf);
623 }
624 return *this;
625}
626
631{
632#if 1
633 gchar *buf = g_strdup_printf("%8.3f", val);
634 if (buf) {
636 g_free(buf);
637 }
638#else
639 std::string tmp = ftos(val, 'g', 8, 3, 0);
640 writeStdString(tmp);
641#endif
642 return *this;
643}
644
649{
650#if 1
651 gchar *buf = g_strdup_printf("%8.3f", val);
652 if (buf) {
654 g_free(buf);
655 }
656#else
657 std::string tmp = ftos(val, 'g', 8, 3, 0);
658 writeStdString(tmp);
659#endif
660 return *this;
661}
662
663
664Writer& operator<< (Writer &writer, char val)
665 { return writer.writeChar(val); }
666
667Writer& operator<< (Writer &writer, Glib::ustring &val)
668 { return writer.writeUString(val); }
669
670Writer& operator<< (Writer &writer, std::string &val)
671 { return writer.writeStdString(val); }
672
673Writer& operator<< (Writer &writer, char const *val)
674 { return writer.writeString(val); }
675
676Writer& operator<< (Writer &writer, bool val)
677 { return writer.writeBool(val); }
678
679Writer& operator<< (Writer &writer, short val)
680 { return writer.writeShort(val); }
681
682Writer& operator<< (Writer &writer, unsigned short val)
683 { return writer.writeUnsignedShort(val); }
684
685Writer& operator<< (Writer &writer, int val)
686 { return writer.writeInt(val); }
687
688Writer& operator<< (Writer &writer, unsigned int val)
689 { return writer.writeUnsignedInt(val); }
690
691Writer& operator<< (Writer &writer, long val)
692 { return writer.writeLong(val); }
693
694Writer& operator<< (Writer &writer, unsigned long val)
695 { return writer.writeUnsignedLong(val); }
696
697Writer& operator<< (Writer &writer, float val)
698 { return writer.writeFloat(val); }
699
700Writer& operator<< (Writer &writer, double val)
701 { return writer.writeDouble(val); }
702
703
704
705//#########################################################################
706//# O U T P U T S T R E A M W R I T E R
707//#########################################################################
708
709
711 : outputStream(outputStreamDest)
712{
713}
714
715
716
721{
722 flush();
724}
725
733
739{
740 outputStream.put(ch);
741}
742
743//#########################################################################
744//# S T D W R I T E R
745//#########################################################################
746
747
755
756
761{
762 delete outputStream;
763}
764
765
766
771{
772 flush();
774}
775
780{
782}
783
788void StdWriter::put(char ch)
789{
790 outputStream->put(ch);
791}
792
793
794} // namespace IO
795} // namespace Inkscape
796
797
798//#########################################################################
799//# E N D O F F I L E
800//#########################################################################
int available() override
Returns the number of bytes that can be read (or skipped over) from this input stream without blockin...
BasicInputStream(InputStream &sourceStream)
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.
BasicOutputStream(OutputStream &destinationStream)
void flush() override
Flushes this output stream and forces any buffered output bytes to be written out.
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.
const Reader & readBool(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
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.
Writer & writeUnsignedShort(unsigned short val) override
Writer & writeBool(bool val) override
Writer & writeFloat(float val) override
Writer & printf(char const *fmt,...) override G_GNUC_PRINTF(2
Provide printf()-like formatting.
void flush() override
Flushes this output stream and forces any buffered output bytes to be written out.
Writer & writeLong(long val) override
void close() override
Closes this writer and releases any system resources associated with this writer.
Writer & writeString(const char *str) override
Writes the specified character string to this output writer.
void put(char ch) override
Writes the specified byte to this output writer.
Writer & writeUnsignedInt(unsigned int val) override
Writer & writeDouble(double val) override
Writer & writeUnsignedLong(unsigned long val) override
Writer Writer & writeChar(char val) override
Writes the specified character to this output writer.
Writer & writeShort(short val) override
Writer & writeUString(const Glib::ustring &val) override
Writes the specified unicode string to this output writer.
Writer & writeStdString(const std::string &val) override
Writes the specified standard string to this output writer.
Writer & writeInt(int val) override
char get() override
Overloaded to receive its bytes from an InputStream rather than a Reader.
void close() override
Close the underlying OutputStream.
InputStreamReader(InputStream &inputStreamSource)
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 ...
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.
OutputStreamWriter(OutputStream &outputStreamDest)
void put(char ch) override
Overloaded to redirect the output chars from the next Writer in the chain to an OutputStream instead.
void close() override
Close the underlying OutputStream.
void flush() override
Flush the underlying OutputStream.
This interface is the base of all input stream classes.
virtual int put(char ch)=0
Send one byte to the destination stream.
virtual void close()=0
This call should.
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 char get()=0
virtual void close()=0
Convenience class for reading from standard input.
Convenience class for writing to standard output.
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.
void put(char ch) override
Overloaded to redirect the output chars from the next Writer in the chain to an OutputStream instead.
void close() override
Close the underlying OutputStream.
OutputStream * outputStream
void flush() override
Flush the underlying OutputStream.
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 & 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 & writeFloat(float val)=0
virtual void put(char ch)=0
virtual void close()=0
virtual Writer & writeUnsignedInt(unsigned int val)=0
Geom::Point end
static bool getULong(Glib::ustring &str, unsigned long *val)
static bool getLong(Glib::ustring &str, long *val)
static bool getDouble(Glib::ustring &str, double *val)
void pipeStream(InputStream &source, OutputStream &dest)
Helper class to stream background task notifications as a series of messages.
int buf
va_end(args)
int const char * fmt
Definition safe-printf.h:18
int const char va_start(args, fmt)