TIVI-153: Add as dependency for iputils
[profile/ivi/opensp.git] / include / OutputByteStream.h
1 // Copyright (c) 1997 James Clark
2 // See the file COPYING for copying permission.
3
4 #ifndef OutputByteStream_INCLUDED
5 #define OutputByteStream_INCLUDED 1
6
7 #include "Link.h"
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 : public Link {
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
40 char *OutputByteStream::getBufferPtr() const
41 {
42   return ptr_;
43 }
44
45 inline
46 size_t OutputByteStream::getBufferSize() const
47 {
48   return end_ - ptr_;
49 }
50
51 inline
52 void OutputByteStream::usedBuffer(size_t n)
53 {
54   ptr_ += n;
55 }
56
57 inline
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   virtual ~StrOutputByteStream() { /* no-op */ };
103   void extractString(String<char> &);
104 protected:
105   StrOutputByteStream(const StrOutputByteStream &); // undefined
106   void operator=(const StrOutputByteStream &); // undefined
107   void flush();
108   void flushBuf(char);
109   String<char> buf_;
110 };
111
112 class SP_API FileOutputByteStream : public OutputByteStream {
113 public:
114   FileOutputByteStream();
115   FileOutputByteStream(int fd, Boolean closeFd = 1);
116   virtual ~FileOutputByteStream();
117 #ifdef SP_WIDE_SYSTEM
118   Boolean open(const wchar_t *);
119 #else
120   Boolean open(const char *);
121 #endif
122   Boolean attach(int fd, Boolean closeFd = 1);
123   Boolean close();
124 private:
125   FileOutputByteStream(const FileOutputByteStream &); // undefined
126   void operator=(const FileOutputByteStream &); // undefined
127   void flush();
128   void flushBuf(char);
129   String<char> buf_;
130   int fd_;
131   Boolean closeFd_;
132 };
133
134 #ifdef SP_NAMESPACE
135 }
136 #endif
137
138 #endif /* not OutputByteStream_INCLUDED */