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 <glibmm.h>
17 namespace Inkscape
18 {
19 namespace IO
20 {
22 class StreamException : public std::exception
23 {
24 public:
26 StreamException(const char *theReason) throw()
27 { reason = theReason; }
28 StreamException(Glib::ustring &theReason) throw()
29 { reason = theReason; }
30 virtual ~StreamException() throw()
31 { }
32 char const *what() const throw()
33 { return reason.c_str(); }
35 private:
36 Glib::ustring reason;
38 };
40 //#########################################################################
41 //# I N P U T S T R E A M
42 //#########################################################################
44 /**
45 * This interface is the base of all input stream classes. Users who wish
46 * to make an InputStream that is part of a chain should inherit from
47 * BasicInputStream. Inherit from this class to make a source endpoint,
48 * such as a URI or buffer.
49 *
50 */
51 class InputStream
52 {
54 public:
56 /**
57 * Constructor.
58 */
59 InputStream() {}
61 /**
62 * Destructor
63 */
64 virtual ~InputStream() {}
66 /**
67 * Return the number of bytes that are currently available
68 * to be read
69 */
70 virtual int available() = 0;
72 /**
73 * Do whatever it takes to 'close' this input stream
74 * The most likely implementation of this method will be
75 * for endpoints that use a resource for their data.
76 */
77 virtual void close() = 0;
79 /**
80 * Read one byte from this input stream. This is a blocking
81 * call. If no data is currently available, this call will
82 * not return until it exists. If the user does not want
83 * their code to block, then the usual solution is:
84 * if (available() > 0)
85 * myChar = get();
86 * This call returns -1 on end-of-file.
87 */
88 virtual int get() = 0;
90 }; // class InputStream
95 /**
96 * This is the class that most users should inherit, to provide
97 * their own streams.
98 *
99 */
100 class BasicInputStream : public InputStream
101 {
103 public:
105 BasicInputStream(InputStream &sourceStream);
107 virtual ~BasicInputStream() {}
109 virtual int available();
111 virtual void close();
113 virtual int get();
115 protected:
117 bool closed;
119 InputStream &source;
121 private:
124 }; // class BasicInputStream
128 /**
129 * Convenience class for reading from standard input
130 */
131 class StdInputStream : public InputStream
132 {
133 public:
135 int available()
136 { return 0; }
138 void close()
139 { /* do nothing */ }
141 int get()
142 { return getchar(); }
144 };
151 //#########################################################################
152 //# O U T P U T S T R E A M
153 //#########################################################################
155 /**
156 * This interface is the base of all input stream classes. Users who wish
157 * to make an OutputStream that is part of a chain should inherit from
158 * BasicOutputStream. Inherit from this class to make a destination endpoint,
159 * such as a URI or buffer.
160 */
161 class OutputStream
162 {
164 public:
166 /**
167 * Constructor.
168 */
169 OutputStream() {}
171 /**
172 * Destructor
173 */
174 virtual ~OutputStream() {}
176 /**
177 * This call should
178 * 1. flush itself
179 * 2. close itself
180 * 3. close the destination stream
181 */
182 virtual void close() = 0;
184 /**
185 * This call should push any pending data it might have to
186 * the destination stream. It should NOT call flush() on
187 * the destination stream.
188 */
189 virtual void flush() = 0;
191 /**
192 * Send one byte to the destination stream.
193 */
194 virtual void put(int ch) = 0;
197 }; // class OutputStream
200 /**
201 * This is the class that most users should inherit, to provide
202 * their own output streams.
203 */
204 class BasicOutputStream : public OutputStream
205 {
207 public:
209 BasicOutputStream(OutputStream &destinationStream);
211 virtual ~BasicOutputStream() {}
213 virtual void close();
215 virtual void flush();
217 virtual void put(int ch);
219 protected:
221 bool closed;
223 OutputStream &destination;
226 }; // class BasicOutputStream
230 /**
231 * Convenience class for writing to standard output
232 */
233 class StdOutputStream : public OutputStream
234 {
235 public:
237 void close()
238 { }
240 void flush()
241 { }
243 void put(int ch)
244 { putchar(ch); }
246 };
251 //#########################################################################
252 //# R E A D E R
253 //#########################################################################
256 /**
257 * This interface and its descendants are for unicode character-oriented input
258 *
259 */
260 class Reader
261 {
263 public:
265 /**
266 * Constructor.
267 */
268 Reader() {}
270 /**
271 * Destructor
272 */
273 virtual ~Reader() {}
276 virtual int available() = 0;
278 virtual void close() = 0;
280 virtual gunichar get() = 0;
282 virtual Glib::ustring readLine() = 0;
284 virtual Glib::ustring readWord() = 0;
286 /* Input formatting */
287 virtual const Reader& readBool (bool& val ) = 0;
288 virtual const Reader& operator>> (bool& val ) = 0;
290 virtual const Reader& readShort (short &val) = 0;
291 virtual const Reader& operator>> (short &val) = 0;
293 virtual const Reader& readUnsignedShort (unsigned short &val) = 0;
294 virtual const Reader& operator>> (unsigned short &val) = 0;
296 virtual const Reader& readInt (int &val) = 0;
297 virtual const Reader& operator>> (int &val) = 0;
299 virtual const Reader& readUnsignedInt (unsigned int &val) = 0;
300 virtual const Reader& operator>> (unsigned int &val) = 0;
302 virtual const Reader& readLong (long &val) = 0;
303 virtual const Reader& operator>> (long &val) = 0;
305 virtual const Reader& readUnsignedLong (unsigned long &val) = 0;
306 virtual const Reader& operator>> (unsigned long &val) = 0;
308 virtual const Reader& readFloat (float &val) = 0;
309 virtual const Reader& operator>> (float &val) = 0;
311 virtual const Reader& readDouble (double &val) = 0;
312 virtual const Reader& operator>> (double &val) = 0;
314 }; // interface Reader
318 /**
319 * This class and its descendants are for unicode character-oriented input
320 *
321 */
322 class BasicReader : public Reader
323 {
325 public:
327 BasicReader(Reader &sourceStream);
329 virtual ~BasicReader() {}
331 virtual int available();
333 virtual void close();
335 virtual gunichar get();
337 virtual Glib::ustring readLine();
339 virtual Glib::ustring readWord();
341 /* Input formatting */
342 virtual const Reader& readBool (bool& val );
343 virtual const Reader& operator>> (bool& val )
344 { return readBool(val); }
346 virtual const Reader& readShort (short &val);
347 virtual const Reader& operator>> (short &val)
348 { return readShort(val); }
350 virtual const Reader& readUnsignedShort (unsigned short &val);
351 virtual const Reader& operator>> (unsigned short &val)
352 { return readUnsignedShort(val); }
354 virtual const Reader& readInt (int &val);
355 virtual const Reader& operator>> (int &val)
356 { return readInt(val); }
358 virtual const Reader& readUnsignedInt (unsigned int &val);
359 virtual const Reader& operator>> (unsigned int &val)
360 { return readUnsignedInt(val); }
362 virtual const Reader& readLong (long &val);
363 virtual const Reader& operator>> (long &val)
364 { return readLong(val); }
366 virtual const Reader& readUnsignedLong (unsigned long &val);
367 virtual const Reader& operator>> (unsigned long &val)
368 { return readUnsignedLong(val); }
370 virtual const Reader& readFloat (float &val);
371 virtual const Reader& operator>> (float &val)
372 { return readFloat(val); }
374 virtual const Reader& readDouble (double &val);
375 virtual const Reader& operator>> (double &val)
376 { return readDouble(val); }
379 protected:
381 Reader *source;
383 BasicReader()
384 { source = NULL; }
386 private:
388 }; // class BasicReader
392 /**
393 * Class for placing a Reader on an open InputStream
394 *
395 */
396 class InputStreamReader : public BasicReader
397 {
398 public:
400 InputStreamReader(InputStream &inputStreamSource);
402 /*Overload these 3 for your implementation*/
403 virtual int available();
405 virtual void close();
407 virtual gunichar get();
410 private:
412 InputStream &inputStream;
415 };
417 /**
418 * Convenience class for reading formatted from standard input
419 *
420 */
421 class StdReader : public BasicReader
422 {
423 public:
425 StdReader();
427 virtual ~StdReader();
429 /*Overload these 3 for your implementation*/
430 virtual int available();
432 virtual void close();
434 virtual gunichar get();
437 private:
439 InputStream *inputStream;
442 };
448 //#########################################################################
449 //# W R I T E R
450 //#########################################################################
452 /**
453 * This interface and its descendants are for unicode character-oriented output
454 *
455 */
456 class Writer
457 {
459 public:
461 /**
462 * Constructor.
463 */
464 Writer() {}
466 /**
467 * Destructor
468 */
469 virtual ~Writer() {}
471 virtual void close() = 0;
473 virtual void flush() = 0;
475 virtual void put(gunichar ch) = 0;
477 /* Formatted output */
478 virtual Writer& printf(char const *fmt, ...) G_GNUC_PRINTF(2,3) = 0;
480 virtual Writer& writeChar(char val) = 0;
482 virtual Writer& writeUString(Glib::ustring &val) = 0;
484 virtual Writer& writeStdString(std::string &val) = 0;
486 virtual Writer& writeString(const char *str) = 0;
488 virtual Writer& writeBool (bool val ) = 0;
490 virtual Writer& writeShort (short val ) = 0;
492 virtual Writer& writeUnsignedShort (unsigned short val ) = 0;
494 virtual Writer& writeInt (int val ) = 0;
496 virtual Writer& writeUnsignedInt (unsigned int val ) = 0;
498 virtual Writer& writeLong (long val ) = 0;
500 virtual Writer& writeUnsignedLong (unsigned long val ) = 0;
502 virtual Writer& writeFloat (float val ) = 0;
504 virtual Writer& writeDouble (double val ) = 0;
508 }; // interface Writer
511 /**
512 * This class and its descendants are for unicode character-oriented output
513 *
514 */
515 class BasicWriter : public Writer
516 {
518 public:
520 BasicWriter(Writer &destinationWriter);
522 virtual ~BasicWriter() {}
524 /*Overload these 3 for your implementation*/
525 virtual void close();
527 virtual void flush();
529 virtual void put(gunichar ch);
533 /* Formatted output */
534 virtual Writer &printf(char const *fmt, ...) G_GNUC_PRINTF(2,3);
536 virtual Writer& writeChar(char val);
538 virtual Writer& writeUString(Glib::ustring &val);
540 virtual Writer& writeStdString(std::string &val);
542 virtual Writer& writeString(const char *str);
544 virtual Writer& writeBool (bool val );
546 virtual Writer& writeShort (short val );
548 virtual Writer& writeUnsignedShort (unsigned short val );
550 virtual Writer& writeInt (int val );
552 virtual Writer& writeUnsignedInt (unsigned int val );
554 virtual Writer& writeLong (long val );
556 virtual Writer& writeUnsignedLong (unsigned long val );
558 virtual Writer& writeFloat (float val );
560 virtual Writer& writeDouble (double val );
563 protected:
565 Writer *destination;
567 BasicWriter()
568 { destination = NULL; }
570 private:
572 }; // class BasicWriter
576 Writer& operator<< (Writer &writer, char val);
578 Writer& operator<< (Writer &writer, Glib::ustring &val);
580 Writer& operator<< (Writer &writer, std::string &val);
582 Writer& operator<< (Writer &writer, char const *val);
584 Writer& operator<< (Writer &writer, bool val);
586 Writer& operator<< (Writer &writer, short val);
588 Writer& operator<< (Writer &writer, unsigned short val);
590 Writer& operator<< (Writer &writer, int val);
592 Writer& operator<< (Writer &writer, unsigned int val);
594 Writer& operator<< (Writer &writer, long val);
596 Writer& operator<< (Writer &writer, unsigned long val);
598 Writer& operator<< (Writer &writer, float val);
600 Writer& operator<< (Writer &writer, double val);
605 /**
606 * Class for placing a Writer on an open OutputStream
607 *
608 */
609 class OutputStreamWriter : public BasicWriter
610 {
611 public:
613 OutputStreamWriter(OutputStream &outputStreamDest);
615 /*Overload these 3 for your implementation*/
616 virtual void close();
618 virtual void flush();
620 virtual void put(gunichar ch);
623 private:
625 OutputStream &outputStream;
628 };
631 /**
632 * Convenience class for writing to standard output
633 */
634 class StdWriter : public BasicWriter
635 {
636 public:
637 StdWriter();
639 virtual ~StdWriter();
642 virtual void close();
645 virtual void flush();
648 virtual void put(gunichar ch);
651 private:
653 OutputStream *outputStream;
655 };
657 //#########################################################################
658 //# U T I L I T Y
659 //#########################################################################
661 void pipeStream(InputStream &source, OutputStream &dest);
665 } // namespace IO
666 } // namespace Inkscape
669 #endif /* __INKSCAPE_IO_INKSCAPESTREAM_H__ */