1 // Copyright (c) 1997 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident	"%Z%%M%	%I%	%E% SMI"
4 
5 #ifndef OutputByteStream_INCLUDED
6 #define OutputByteStream_INCLUDED 1
7 
8 #include "StringOf.h"
9 #include "Boolean.h"
10 
11 #ifdef SP_NAMESPACE
12 namespace SP_NAMESPACE {
13 #endif
14 
15 class SP_API OutputByteStream {
16 public:
17   OutputByteStream();
18   virtual ~OutputByteStream();
19   virtual void flush() = 0;
20   void sputc(char c);
21   void sputn(const char *, size_t);
22   OutputByteStream &operator<<(char);
23   OutputByteStream &operator<<(unsigned char);
24   OutputByteStream &operator<<(const char *);
25   OutputByteStream &operator<<(int);
26   OutputByteStream &operator<<(unsigned);
27   OutputByteStream &operator<<(long);
28   OutputByteStream &operator<<(unsigned long);
29   OutputByteStream &operator<<(const String<char> &);
30   char *getBufferPtr() const;
31   size_t getBufferSize() const;
32   void usedBuffer(size_t);
33   virtual void flushBuf(char) = 0;
34 protected:
35   char *ptr_;
36   char *end_;
37 };
38 
39 inline
getBufferPtr()40 char *OutputByteStream::getBufferPtr() const
41 {
42   return ptr_;
43 }
44 
45 inline
getBufferSize()46 size_t OutputByteStream::getBufferSize() const
47 {
48   return end_ - ptr_;
49 }
50 
51 inline
usedBuffer(size_t n)52 void OutputByteStream::usedBuffer(size_t n)
53 {
54   ptr_ += n;
55 }
56 
57 inline
sputc(char c)58 void OutputByteStream::sputc(char c)
59 {
60   if (ptr_ < end_)
61     *ptr_++ = c;
62   else
63     flushBuf(c);
64 }
65 
66 inline
67 OutputByteStream &OutputByteStream::operator<<(char c)
68 {
69   sputc(c);
70   return *this;
71 }
72 
73 inline
74 OutputByteStream &OutputByteStream::operator<<(unsigned char c)
75 {
76   sputc(char(c));
77   return *this;
78 }
79 
80 inline
81 OutputByteStream &OutputByteStream::operator<<(int n)
82 {
83   return *this << long(n);
84 }
85 
86 inline
87 OutputByteStream &OutputByteStream::operator<<(unsigned n)
88 {
89   return *this << (unsigned long)n;
90 }
91 
92 inline
93 OutputByteStream &OutputByteStream::operator<<(const String<char> &s)
94 {
95   sputn(s.data(), s.size());
96   return *this;
97 }
98 
99 class SP_API StrOutputByteStream : public OutputByteStream {
100 public:
101   StrOutputByteStream();
102   void extractString(String<char> &);
103 protected:
104   StrOutputByteStream(const StrOutputByteStream &); // undefined
105   void operator=(const StrOutputByteStream &); // undefined
106   void flush();
107   void flushBuf(char);
108   String<char> buf_;
109 };
110 
111 class SP_API FileOutputByteStream : public OutputByteStream {
112 public:
113   FileOutputByteStream();
114   FileOutputByteStream(int fd, Boolean closeFd = 1);
115   ~FileOutputByteStream();
116 #ifdef SP_WIDE_SYSTEM
117   Boolean open(const wchar_t *);
118 #else
119   Boolean open(const char *);
120 #endif
121   Boolean attach(int fd, Boolean closeFd = 1);
122   Boolean close();
123 private:
124   FileOutputByteStream(const FileOutputByteStream &); // undefined
125   void operator=(const FileOutputByteStream &); // undefined
126   void flush();
127   void flushBuf(char);
128   String<char> buf_;
129   int fd_;
130   Boolean closeFd_;
131 };
132 
133 #ifdef SP_NAMESPACE
134 }
135 #endif
136 
137 #endif /* not OutputByteStream_INCLUDED */
138