1 // Copyright (c) 1994 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident	"%Z%%M%	%I%	%E% SMI"
4 
5 #ifndef OutputCharStream_INCLUDED
6 #define OutputCharStream_INCLUDED 1
7 
8 #include "types.h"
9 #include <stddef.h>
10 #include "StringC.h"
11 #include "Owner.h"
12 #include "CodingSystem.h"
13 #include "OutputByteStream.h"
14 
15 #ifdef SP_NAMESPACE
16 namespace SP_NAMESPACE {
17 #endif
18 
19 class SP_API OutputCharStream {
20 public:
21   enum Newline { newline };
22   typedef void (*Escaper)(OutputCharStream &, Char);
23   OutputCharStream();
24   virtual ~OutputCharStream();
25   OutputCharStream &put(Char);
26   OutputCharStream &write(const Char *, size_t);
27   virtual void flush() = 0;
28   virtual void setEscaper(Escaper);
29 
30   OutputCharStream &operator<<(char);
31   OutputCharStream &operator<<(const char *);
32   OutputCharStream &operator<<(const StringC &);
33   OutputCharStream &operator<<(unsigned long);
34   OutputCharStream &operator<<(int);
35   OutputCharStream &operator<<(Newline);
36 private:
37   OutputCharStream(const OutputCharStream &);	// undefined
38   void operator=(const OutputCharStream &);	// undefined
39 
40   virtual void flushBuf(Char) = 0;
41 protected:
42   Char *ptr_;
43   Char *end_;
44 };
45 
46 class SP_API EncodeOutputCharStream : public OutputCharStream,
47                             private Encoder::Handler {
48 public:
49   EncodeOutputCharStream();
50   // the OutputByteStream will not be deleted
51   EncodeOutputCharStream(OutputByteStream *, const OutputCodingSystem *);
52   ~EncodeOutputCharStream();
53   void open(OutputByteStream *, const OutputCodingSystem *);
54   void flush();
55   void setEscaper(Escaper);
56 private:
57   EncodeOutputCharStream(const EncodeOutputCharStream &); // undefined
58   void operator=(const EncodeOutputCharStream &);	    // undefined
59   EncodeOutputCharStream(OutputByteStream *, Encoder *);
60   void allocBuf(int bytesPerChar);
61   void flushBuf(Char);
62   void handleUnencodable(Char c, OutputByteStream *);
63   Char *buf_;
64   OutputByteStream *byteStream_;
65   Encoder *encoder_;
66   Owner<Encoder> ownedEncoder_;
67   Escaper escaper_;
68 };
69 
70 class SP_API StrOutputCharStream : public OutputCharStream {
71 public:
72   StrOutputCharStream();
73   ~StrOutputCharStream();
74   void extractString(StringC &);
75   void flush();
76 private:
77   void flushBuf(Char);
78   void sync(size_t);
79   StrOutputCharStream(const StrOutputCharStream &); // undefined
80   void operator=(const StrOutputCharStream &);	    // undefined
81   Char *buf_;
82   size_t bufSize_;
83 };
84 
85 class SP_API RecordOutputCharStream : public OutputCharStream {
86 public:
87   RecordOutputCharStream(OutputCharStream *);
88   ~RecordOutputCharStream();
89   void flush();
90   void setEscaper(Escaper);
91 private:
92   RecordOutputCharStream(const RecordOutputCharStream &); // undefined
93   void operator=(const RecordOutputCharStream &);	  // undefined
94   void flushBuf(Char);
95   void outputBuf();
96 
97   OutputCharStream *os_;
98   enum { bufSize_ = 1024 };
99   Char buf_[bufSize_];
100 };
101 
102 inline
put(Char c)103 OutputCharStream &OutputCharStream::put(Char c)
104 {
105   if (ptr_ < end_)
106     *ptr_++ = c;
107   else
108     flushBuf(c);
109   return *this;
110 }
111 
112 inline
113 OutputCharStream &OutputCharStream::operator<<(char c)
114 {
115   return put(Char(c));
116 }
117 
118 inline
119 OutputCharStream &OutputCharStream::operator<<(Newline)
120 {
121   put(Char(SP_LINE_TERM1));
122 #ifdef SP_LINE_TERM2
123   put(Char(SP_LINE_TERM2));
124 #endif
125   return *this;
126 }
127 
128 inline
129 OutputCharStream &OutputCharStream::operator<<(const StringC &str)
130 {
131   return write(str.data(), str.size());
132 }
133 
134 #ifdef SP_NAMESPACE
135 }
136 #endif
137 
138 #endif /* not OutputCharStream_INCLUDED */
139