Fixed typos
[ACE_TAO.git] / ACE / ace / IOStream_T.cpp
blob21b8e66263d015ba60bbcd0abee1da3c77f3f515
1 #ifndef ACE_IOSTREAM_T_CPP
2 #define ACE_IOSTREAM_T_CPP
4 #include "ace/IOStream_T.h"
5 #include "ace/OS_Memory.h"
7 #if !defined (ACE_LACKS_PRAGMA_ONCE)
8 # pragma once
9 #endif /* ACE_LACKS_PRAGMA_ONCE */
11 #if !defined (ACE_LACKS_ACE_IOSTREAM)
13 #if !defined (__ACE_INLINE__)
14 #include "ace/IOStream_T.inl"
15 #endif /* !__ACE_INLINE__ */
17 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 // We will be given a STREAM by the iostream object which creates us.
20 // See the ACE_IOStream template for how that works. Like other
21 // streambuf objects, we can be input-only, output-only or both.
23 template <class STREAM>
24 ACE_Streambuf_T<STREAM>::ACE_Streambuf_T (STREAM *peer,
25 u_int streambuf_size,
26 int io_mode)
27 : ACE_Streambuf (streambuf_size, io_mode),
28 peer_ (peer)
30 // A streambuf allows for unbuffered IO where every character is
31 // read as requested and written as provided. To me, this seems
32 // terribly inefficient for socket-type operations, so I've disabled
33 // it. All of the work would be done by the underflow/overflow
34 // functions anyway and I haven't implemented anything there to
35 // support unbuffered IO.
37 #if !defined (ACE_LACKS_UNBUFFERED_STREAMBUF)
38 this->unbuffered (0);
39 #endif /* ! ACE_LACKS_UNBUFFERED_STREAMBUF */
41 // Linebuffered is similar to unbuffered. Again, I don't have any
42 // need for this and I don't see the advantage. I believe this
43 // would have to be supported by underflow/overflow to be effective.
44 #if !defined (ACE_LACKS_LINEBUFFERED_STREAMBUF)
45 this->linebuffered (0);
46 #endif /* ! ACE_LACKS_LINEBUFFERED_STREAMBUF */
49 template <class STREAM> ssize_t
50 ACE_Streambuf_T<STREAM>::send (char *buf, ssize_t len)
52 return peer_->send_n (buf,len);
55 template <class STREAM> ssize_t
56 ACE_Streambuf_T<STREAM>::recv (char *buf,
57 ssize_t len,
58 ACE_Time_Value *tv)
60 return this->recv (buf, len, 0, tv);
63 template <class STREAM> ssize_t
64 ACE_Streambuf_T<STREAM>::recv (char *buf,
65 ssize_t len,
66 int flags,
67 ACE_Time_Value * tv)
69 this->timeout_ = 0;
70 errno = ESUCCESS;
71 ssize_t rval = peer_->recv (buf, len, flags, tv);
72 if (errno == ETIME)
73 this->timeout_ = 1;
74 return rval;
77 template <class STREAM> ssize_t
78 ACE_Streambuf_T<STREAM>::recv_n (char *buf,
79 ssize_t len,
80 int flags,
81 ACE_Time_Value *tv)
83 this->timeout_ = 0;
84 errno = ESUCCESS;
85 ssize_t rval = peer_->recv_n (buf, len, flags, tv);
86 if (errno == ETIME)
87 this->timeout_ = 1;
88 return rval;
91 template <class STREAM> ACE_HANDLE
92 ACE_Streambuf_T<STREAM>::get_handle (void)
94 return peer_ ? peer_->get_handle () : 0;
97 // The typical constructor. This will initiailze your STREAM and then
98 // setup the iostream baseclass to use a custom streambuf based on
99 // STREAM.
101 template <class STREAM>
102 ACE_IOStream<STREAM>::ACE_IOStream (STREAM &stream,
103 u_int streambuf_size)
104 : iostream (0),
105 STREAM (stream)
107 ACE_NEW (streambuf_,
108 ACE_Streambuf_T<STREAM> ((STREAM *) this,
109 streambuf_size));
110 iostream::init (this->streambuf_);
113 template <class STREAM>
114 ACE_IOStream<STREAM>::ACE_IOStream (u_int streambuf_size)
115 : iostream (0)
117 ACE_NEW (this->streambuf_,
118 ACE_Streambuf_T<STREAM> ((STREAM *) this,
119 streambuf_size));
120 iostream::init (this->streambuf_);
123 // We have to get rid of the streambuf_ ourselves since we gave it to
124 // iostream ()
126 template <class STREAM>
127 ACE_IOStream<STREAM>::~ACE_IOStream (void)
129 delete this->streambuf_;
132 // The only ambituity in the multiple inheritance is the close ()
133 // function.
135 template <class STREAM> int
136 ACE_IOStream<STREAM>::close (void)
138 return STREAM::close ();
141 template <class STREAM> ACE_IOStream<STREAM> &
142 ACE_IOStream<STREAM>::operator>> (ACE_Time_Value *&tv)
144 ACE_Time_Value *old_tv = this->streambuf_->recv_timeout (tv);
145 tv = old_tv;
146 return *this;
149 #if defined (ACE_HAS_STRING_CLASS)
151 // A simple string operator. The base iostream has 'em for char* but
152 // that isn't always the best thing for a String. If we don't provide
153 // our own here, we may not get what we want.
155 template <class STREAM> ACE_IOStream<STREAM> &
156 ACE_IOStream<STREAM>::operator>> (ACE_IOStream_String &v)
158 if (ipfx0 ())
160 char c;
161 this->get (c);
163 for (v = c;
164 this->get (c) && !isspace (c);
165 v += c)
166 continue;
169 isfx ();
171 return *this;
174 template <class STREAM> ACE_IOStream<STREAM> &
175 ACE_IOStream<STREAM>::operator<< (ACE_IOStream_String &v)
177 if (opfx ())
179 #if defined (ACE_WIN32) && defined (_MSC_VER)
180 for (int i = 0; i < v.GetLength (); ++i)
181 #else
182 for (u_int i = 0; i < (u_int) v.length (); ++i)
183 #endif /* ACE_WIN32 && defined (_MSC_VER) */
184 this->put (v[i]);
187 osfx ();
189 return *this;
192 // A more clever put operator for strings that knows how to deal with
193 // quoted strings containing back-quoted quotes.
195 template <class STREAM> STREAM &
196 operator>> (STREAM &stream,
197 ACE_Quoted_String &str)
199 char c;
201 if (!(stream >> c)) // eat space up to the first char
202 // stream.set (ios::eofbit|ios::failbit);
203 return stream;
205 str = ""; // Initialize the string
207 // if we don't have a quote, append until we see space
208 if (c != '"')
209 for (str = c; stream.get (c) && !isspace (c); str += c)
210 continue;
211 else
212 for (; stream.get (c) && c != '"'; str += c)
213 if (c == '\\')
215 stream.get (c);
216 if (c != '"')
217 str += '\\';
220 return stream;
223 template <class STREAM> STREAM &
224 operator<< (STREAM &stream,
225 ACE_Quoted_String &str)
227 stream.put ('"');
229 for (u_int i = 0; i < str.length (); ++i)
231 if (str[i] == '"')
232 stream.put ('\\');
233 stream.put (str[i]);
236 stream.put ('"');
238 return stream;
241 ACE_END_VERSIONED_NAMESPACE_DECL
243 #endif /* ACE_HAS_STRING_CLASS */
244 #endif /* ACE_LACKS_ACE_IOSTREAM */
245 #endif /* ACE_IOSTREAM_T_CPP */