1 #ifndef __INKSCAPE_IO_INKSCAPESTREAM_H__
2 #define __INKSCAPE_IO_INKSCAPESTREAM_H__
3 /**
4 * Our base basic stream classes.
5 *
6 * Authors:
7 * Bob Jamison <rjamison@titan.com>
8 *
9 * Copyright (C) 2004 Inkscape.org
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
15 #include <cstdio>
16 #include <glibmm.h>
18 namespace Inkscape
19 {
20 namespace IO
21 {
23 class StreamException : public std::exception
24 {
25 public:
27 StreamException(const char *theReason) throw()
28 { reason = theReason; }
29 StreamException(Glib::ustring &theReason) throw()
30 { reason = theReason; }
31 virtual ~StreamException() throw()
32 { }
33 char const *what() const throw()
34 { return reason.c_str(); }
36 private:
37 Glib::ustring reason;
39 };
41 //#########################################################################
42 //# I N P U T S T R E A M
43 //#########################################################################
45 /**
46 * This interface is the base of all input stream classes. Users who wish
47 * to make an InputStream that is part of a chain should inherit from
48 * BasicInputStream. Inherit from this class to make a source endpoint,
49 * such as a URI or buffer.
50 *
51 */
52 class InputStream
53 {
55 public:
57 /**
58 * Constructor.
59 */
60 InputStream() {}
62 /**
63 * Destructor
64 */
65 virtual ~InputStream() {}
67 /**
68 * Return the number of bytes that are currently available
69 * to be read
70 */
71 virtual int available() = 0;
73 /**
74 * Do whatever it takes to 'close' this input stream
75 * The most likely implementation of this method will be
76 * for endpoints that use a resource for their data.
77 */
78 virtual void close() = 0;
80 /**
81 * Read one byte from this input stream. This is a blocking
82 * call. If no data is currently available, this call will
83 * not return until it exists. If the user does not want
84 * their code to block, then the usual solution is:
85 * if (available() > 0)
86 * myChar = get();
87 * This call returns -1 on end-of-file.
88 */
89 virtual int get() = 0;
91 }; // class InputStream
96 /**
97 * This is the class that most users should inherit, to provide
98 * their own streams.
99 *
100 */
101 class BasicInputStream : public InputStream
102 {
104 public:
106 BasicInputStream(InputStream &sourceStream);
108 virtual ~BasicInputStream() {}
110 virtual int available();
112 virtual void close();
114 virtual int get();
116 protected:
118 bool closed;
120 InputStream &source;
122 private:
125 }; // class BasicInputStream
129 /**
130 * Convenience class for reading from standard input
131 */
132 class StdInputStream : public InputStream
133 {
134 public:
136 int available()
137 { return 0; }
139 void close()
140 { /* do nothing */ }
142 int get()
143 { return getchar(); }
145 };
152 //#########################################################################
153 //# O U T P U T S T R E A M
154 //#########################################################################
156 /**
157 * This interface is the base of all input stream classes. Users who wish
158 * to make an OutputStream that is part of a chain should inherit from
159 * BasicOutputStream. Inherit from this class to make a destination endpoint,
160 * such as a URI or buffer.
161 */
162 class OutputStream
163 {
165 public:
167 /**
168 * Constructor.
169 */
170 OutputStream() {}
172 /**
173 * Destructor
174 */
175 virtual ~OutputStream() {}
177 /**
178 * This call should
179 * 1. flush itself
180 * 2. close itself
181 * 3. close the destination stream
182 */
183 virtual void close() = 0;
185 /**
186 * This call should push any pending data it might have to
187 * the destination stream. It should NOT call flush() on
188 * the destination stream.
189 */
190 virtual void flush() = 0;
192 /**
193 * Send one byte to the destination stream.
194 */
195 virtual void put(int ch) = 0;
198 }; // class OutputStream
201 /**
202 * This is the class that most users should inherit, to provide
203 * their own output streams.
204 */
205 class BasicOutputStream : public OutputStream
206 {
208 public:
210 BasicOutputStream(OutputStream &destinationStream);
212 virtual ~BasicOutputStream() {}
214 virtual void close();
216 virtual void flush();
218 virtual void put(int ch);
220 protected:
222 bool closed;
224 OutputStream &destination;
227 }; // class BasicOutputStream
231 /**
232 * Convenience class for writing to standard output
233 */
234 class StdOutputStream : public OutputStream
235 {
236 public:
238 void close()
239 { }
241 void flush()
242 { }
244 void put(int ch)
245 { putchar(ch); }
247 };
252 //#########################################################################
253 //# R E A D E R
254 //#########################################################################
257 /**
258 * This interface and its descendants are for unicode character-oriented input
259 *
260 */
261 class Reader
262 {
264 public:
266 /**
267 * Constructor.
268 */
269 Reader() {}
271 /**
272 * Destructor
273 */
274 virtual ~Reader() {}
277 virtual int available() = 0;
279 virtual void close() = 0;
281 virtual gunichar get() = 0;
283 virtual Glib::ustring readLine() = 0;
285 virtual Glib::ustring readWord() = 0;
287 /* Input formatting */
288 virtual const Reader& readBool (bool& val ) = 0;
289 virtual const Reader& operator>> (bool& val ) = 0;
291 virtual const Reader& readShort (short &val) = 0;
292 virtual const Reader& operator>> (short &val) = 0;
294 virtual const Reader& readUnsignedShort (unsigned short &val) = 0;
295 virtual const Reader& operator>> (unsigned short &val) = 0;
297 virtual const Reader& readInt (int &val) = 0;
298 virtual const Reader& operator>> (int &val) = 0;
300 virtual const Reader& readUnsignedInt (unsigned int &val) = 0;
301 virtual const Reader& operator>> (unsigned int &val) = 0;
303 virtual const Reader& readLong (long &val) = 0;
304 virtual const Reader& operator>> (long &val) = 0;
306 virtual const Reader& readUnsignedLong (unsigned long &val) = 0;
307 virtual const Reader& operator>> (unsigned long &val) = 0;
309 virtual const Reader& readFloat (float &val) = 0;
310 virtual const Reader& operator>> (float &val) = 0;
312 virtual const Reader& readDouble (double &val) = 0;
313 virtual const Reader& operator>> (double &val) = 0;
315 }; // interface Reader
319 /**
320 * This class and its descendants are for unicode character-oriented input
321 *
322 */
323 class BasicReader : public Reader
324 {
326 public:
328 BasicReader(Reader &sourceStream);
330 virtual ~BasicReader() {}
332 virtual int available();
334 virtual void close();
336 virtual gunichar get();
338 virtual Glib::ustring readLine();
340 virtual Glib::ustring readWord();
342 /* Input formatting */
343 virtual const Reader& readBool (bool& val );
344 virtual const Reader& operator>> (bool& val )
345 { return readBool(val); }
347 virtual const Reader& readShort (short &val);
348 virtual const Reader& operator>> (short &val)
349 { return readShort(val); }
351 virtual const Reader& readUnsignedShort (unsigned short &val);
352 virtual const Reader& operator>> (unsigned short &val)
353 { return readUnsignedShort(val); }
355 virtual const Reader& readInt (int &val);
356 virtual const Reader& operator>> (int &val)
357 { return readInt(val); }
359 virtual const Reader& readUnsignedInt (unsigned int &val);
360 virtual const Reader& operator>> (unsigned int &val)
361 { return readUnsignedInt(val); }
363 virtual const Reader& readLong (long &val);
364 virtual const Reader& operator>> (long &val)
365 { return readLong(val); }
367 virtual const Reader& readUnsignedLong (unsigned long &val);
368 virtual const Reader& operator>> (unsigned long &val)
369 { return readUnsignedLong(val); }
371 virtual const Reader& readFloat (float &val);
372 virtual const Reader& operator>> (float &val)
373 { return readFloat(val); }
375 virtual const Reader& readDouble (double &val);
376 virtual const Reader& operator>> (double &val)
377 { return readDouble(val); }
380 protected:
382 Reader *source;
384 BasicReader()
385 { source = NULL; }
387 private:
389 }; // class BasicReader
393 /**
394 * Class for placing a Reader on an open InputStream
395 *
396 */
397 class InputStreamReader : public BasicReader
398 {
399 public:
401 InputStreamReader(InputStream &inputStreamSource);
403 /*Overload these 3 for your implementation*/
404 virtual int available();
406 virtual void close();
408 virtual gunichar get();
411 private:
413 InputStream &inputStream;
416 };
418 /**
419 * Convenience class for reading formatted from standard input
420 *
421 */
422 class StdReader : public BasicReader
423 {
424 public:
426 StdReader();
428 virtual ~StdReader();
430 /*Overload these 3 for your implementation*/
431 virtual int available();
433 virtual void close();
435 virtual gunichar get();
438 private:
440 InputStream *inputStream;
443 };
449 //#########################################################################
450 //# W R I T E R
451 //#########################################################################
453 /**
454 * This interface and its descendants are for unicode character-oriented output
455 *
456 */
457 class Writer
458 {
460 public:
462 /**
463 * Constructor.
464 */
465 Writer() {}
467 /**
468 * Destructor
469 */
470 virtual ~Writer() {}
472 virtual void close() = 0;
474 virtual void flush() = 0;
476 virtual void put(gunichar ch) = 0;
478 /* Formatted output */
479 virtual Writer& printf(char const *fmt, ...) G_GNUC_PRINTF(2,3) = 0;
481 virtual Writer& writeChar(char val) = 0;
483 virtual Writer& writeUString(Glib::ustring &val) = 0;
485 virtual Writer& writeStdString(std::string &val) = 0;
487 virtual Writer& writeString(const char *str) = 0;
489 virtual Writer& writeBool (bool val ) = 0;
491 virtual Writer& writeShort (short val ) = 0;
493 virtual Writer& writeUnsignedShort (unsigned short val ) = 0;
495 virtual Writer& writeInt (int val ) = 0;
497 virtual Writer& writeUnsignedInt (unsigned int val ) = 0;
499 virtual Writer& writeLong (long val ) = 0;
501 virtual Writer& writeUnsignedLong (unsigned long val ) = 0;
503 virtual Writer& writeFloat (float val ) = 0;
505 virtual Writer& writeDouble (double val ) = 0;
509 }; // interface Writer
512 /**
513 * This class and its descendants are for unicode character-oriented output
514 *
515 */
516 class BasicWriter : public Writer
517 {
519 public:
521 BasicWriter(Writer &destinationWriter);
523 virtual ~BasicWriter() {}
525 /*Overload these 3 for your implementation*/
526 virtual void close();
528 virtual void flush();
530 virtual void put(gunichar ch);
534 /* Formatted output */
535 virtual Writer &printf(char const *fmt, ...) G_GNUC_PRINTF(2,3);
537 virtual Writer& writeChar(char val);
539 virtual Writer& writeUString(Glib::ustring &val);
541 virtual Writer& writeStdString(std::string &val);
543 virtual Writer& writeString(const char *str);
545 virtual Writer& writeBool (bool val );
547 virtual Writer& writeShort (short val );
549 virtual Writer& writeUnsignedShort (unsigned short val );
551 virtual Writer& writeInt (int val );
553 virtual Writer& writeUnsignedInt (unsigned int val );
555 virtual Writer& writeLong (long val );
557 virtual Writer& writeUnsignedLong (unsigned long val );
559 virtual Writer& writeFloat (float val );
561 virtual Writer& writeDouble (double val );
564 protected:
566 Writer *destination;
568 BasicWriter()
569 { destination = NULL; }
571 private:
573 }; // class BasicWriter
577 Writer& operator<< (Writer &writer, char val);
579 Writer& operator<< (Writer &writer, Glib::ustring &val);
581 Writer& operator<< (Writer &writer, std::string &val);
583 Writer& operator<< (Writer &writer, char const *val);
585 Writer& operator<< (Writer &writer, bool val);
587 Writer& operator<< (Writer &writer, short val);
589 Writer& operator<< (Writer &writer, unsigned short val);
591 Writer& operator<< (Writer &writer, int val);
593 Writer& operator<< (Writer &writer, unsigned int val);
595 Writer& operator<< (Writer &writer, long val);
597 Writer& operator<< (Writer &writer, unsigned long val);
599 Writer& operator<< (Writer &writer, float val);
601 Writer& operator<< (Writer &writer, double val);
606 /**
607 * Class for placing a Writer on an open OutputStream
608 *
609 */
610 class OutputStreamWriter : public BasicWriter
611 {
612 public:
614 OutputStreamWriter(OutputStream &outputStreamDest);
616 /*Overload these 3 for your implementation*/
617 virtual void close();
619 virtual void flush();
621 virtual void put(gunichar ch);
624 private:
626 OutputStream &outputStream;
629 };
632 /**
633 * Convenience class for writing to standard output
634 */
635 class StdWriter : public BasicWriter
636 {
637 public:
638 StdWriter();
640 virtual ~StdWriter();
643 virtual void close();
646 virtual void flush();
649 virtual void put(gunichar ch);
652 private:
654 OutputStream *outputStream;
656 };
658 //#########################################################################
659 //# U T I L I T Y
660 //#########################################################################
662 void pipeStream(InputStream &source, OutputStream &dest);
666 } // namespace IO
667 } // namespace Inkscape
670 #endif /* __INKSCAPE_IO_INKSCAPESTREAM_H__ */