Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / ACE_wrappers / ace / IOStream_T.cpp
blob3e7817d7c6108b9cd6fe58f7b1dfdf57d671cd5c
1 // $Id: IOStream_T.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #ifndef ACE_IOSTREAM_T_CPP
4 #define ACE_IOSTREAM_T_CPP
6 #include "ace/IOStream_T.h"
7 #include "ace/OS_Memory.h"
9 #if !defined (ACE_LACKS_PRAGMA_ONCE)
10 # pragma once
11 #endif /* ACE_LACKS_PRAGMA_ONCE */
13 #if !defined (ACE_LACKS_ACE_IOSTREAM)
15 #if !defined (__ACE_INLINE__)
16 #include "ace/IOStream_T.inl"
17 #endif /* !__ACE_INLINE__ */
19 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
21 // We will be given a STREAM by the iostream object which creates us.
22 // See the ACE_IOStream template for how that works. Like other
23 // streambuf objects, we can be input-only, output-only or both.
25 template <class STREAM>
26 ACE_Streambuf_T<STREAM>::ACE_Streambuf_T (STREAM *peer,
27 u_int streambuf_size,
28 int io_mode)
29 : ACE_Streambuf (streambuf_size, io_mode),
30 peer_ (peer)
32 // A streambuf allows for unbuffered IO where every character is
33 // read as requested and written as provided. To me, this seems
34 // terribly inefficient for socket-type operations, so I've disabled
35 // it. All of the work would be done by the underflow/overflow
36 // functions anyway and I haven't implemented anything there to
37 // support unbuffered IO.
39 #if !defined (ACE_LACKS_UNBUFFERED_STREAMBUF)
40 this->unbuffered (0);
41 #endif /* ! ACE_LACKS_UNBUFFERED_STREAMBUF */
43 // Linebuffered is similar to unbuffered. Again, I don't have any
44 // need for this and I don't see the advantage. I believe this
45 // would have to be supported by underflow/overflow to be effective.
46 #if !defined (ACE_LACKS_LINEBUFFERED_STREAMBUF)
47 this->linebuffered (0);
48 #endif /* ! ACE_LACKS_LINEBUFFERED_STREAMBUF */
51 template <class STREAM> ssize_t
52 ACE_Streambuf_T<STREAM>::send (char *buf, ssize_t len)
54 return peer_->send_n (buf,len);
57 template <class STREAM> ssize_t
58 ACE_Streambuf_T<STREAM>::recv (char *buf,
59 ssize_t len,
60 ACE_Time_Value *tv)
62 return this->recv (buf, len, 0, tv);
65 template <class STREAM> ssize_t
66 ACE_Streambuf_T<STREAM>::recv (char *buf,
67 ssize_t len,
68 int flags,
69 ACE_Time_Value * tv)
71 this->timeout_ = 0;
72 errno = ESUCCESS;
73 ssize_t rval = peer_->recv (buf, len, flags, tv);
74 if (errno == ETIME)
75 this->timeout_ = 1;
76 return rval;
79 template <class STREAM> ssize_t
80 ACE_Streambuf_T<STREAM>::recv_n (char *buf,
81 ssize_t len,
82 int flags,
83 ACE_Time_Value *tv)
85 this->timeout_ = 0;
86 errno = ESUCCESS;
87 ssize_t rval = peer_->recv_n (buf, len, flags, tv);
88 if (errno == ETIME)
89 this->timeout_ = 1;
90 return rval;
93 template <class STREAM> ACE_HANDLE
94 ACE_Streambuf_T<STREAM>::get_handle (void)
96 return peer_ ? peer_->get_handle () : 0;
99 // The typical constructor. This will initiailze your STREAM and then
100 // setup the iostream baseclass to use a custom streambuf based on
101 // STREAM.
103 template <class STREAM>
104 ACE_IOStream<STREAM>::ACE_IOStream (STREAM &stream,
105 u_int streambuf_size)
106 : iostream (0),
107 STREAM (stream)
109 ACE_NEW (streambuf_,
110 ACE_Streambuf_T<STREAM> ((STREAM *) this,
111 streambuf_size));
112 iostream::init (this->streambuf_);
115 template <class STREAM>
116 ACE_IOStream<STREAM>::ACE_IOStream (u_int streambuf_size)
117 : iostream (0)
119 ACE_NEW (this->streambuf_,
120 ACE_Streambuf_T<STREAM> ((STREAM *) this,
121 streambuf_size));
122 iostream::init (this->streambuf_);
125 // We have to get rid of the streambuf_ ourselves since we gave it to
126 // iostream ()
128 template <class STREAM>
129 ACE_IOStream<STREAM>::~ACE_IOStream (void)
131 delete this->streambuf_;
134 // The only ambituity in the multiple inheritance is the close ()
135 // function.
137 template <class STREAM> int
138 ACE_IOStream<STREAM>::close (void)
140 return STREAM::close ();
143 template <class STREAM> ACE_IOStream<STREAM> &
144 ACE_IOStream<STREAM>::operator>> (ACE_Time_Value *&tv)
146 ACE_Time_Value *old_tv = this->streambuf_->recv_timeout (tv);
147 tv = old_tv;
148 return *this;
151 #if defined (ACE_HAS_STRING_CLASS)
153 // A simple string operator. The base iostream has 'em for char* but
154 // that isn't always the best thing for a String. If we don't provide
155 // our own here, we may not get what we want.
157 template <class STREAM> ACE_IOStream<STREAM> &
158 ACE_IOStream<STREAM>::operator>> (ACE_IOStream_String &v)
160 if (ipfx0 ())
162 char c;
163 this->get (c);
165 for (v = c;
166 this->get (c) && !isspace (c);
167 v += c)
168 continue;
171 isfx ();
173 return *this;
176 template <class STREAM> ACE_IOStream<STREAM> &
177 ACE_IOStream<STREAM>::operator<< (ACE_IOStream_String &v)
179 if (opfx ())
181 #if defined (ACE_WIN32) && defined (_MSC_VER)
182 for (int i = 0; i < v.GetLength (); ++i)
183 #else
184 for (u_int i = 0; i < (u_int) v.length (); ++i)
185 #endif /* ACE_WIN32 && defined (_MSC_VER) */
186 this->put (v[i]);
189 osfx ();
191 return *this;
194 // A more clever put operator for strings that knows how to deal with
195 // quoted strings containing back-quoted quotes.
197 template <class STREAM> STREAM &
198 operator>> (STREAM &stream,
199 ACE_Quoted_String &str)
201 char c;
203 if (!(stream >> c)) // eat space up to the first char
204 // stream.set (ios::eofbit|ios::failbit);
205 return stream;
207 str = ""; // Initialize the string
209 // if we don't have a quote, append until we see space
210 if (c != '"')
211 for (str = c; stream.get (c) && !isspace (c); str += c)
212 continue;
213 else
214 for (; stream.get (c) && c != '"'; str += c)
215 if (c == '\\')
217 stream.get (c);
218 if (c != '"')
219 str += '\\';
222 return stream;
225 template <class STREAM> STREAM &
226 operator<< (STREAM &stream,
227 ACE_Quoted_String &str)
229 stream.put ('"');
231 for (u_int i = 0; i < str.length (); ++i)
233 if (str[i] == '"')
234 stream.put ('\\');
235 stream.put (str[i]);
238 stream.put ('"');
240 return stream;
243 ACE_END_VERSIONED_NAMESPACE_DECL
245 #endif /* ACE_HAS_STRING_CLASS */
246 #endif /* ACE_LACKS_ACE_IOSTREAM */
247 #endif /* ACE_IOSTREAM_T_CPP */