00001
00002
00003 #ifndef ACE_IOS_STRING_IOSTREAM_CPP
00004 #define ACE_IOS_STRING_IOSTREAM_CPP
00005
00006 #include "ace/INet/String_IOStream.h"
00007 #include "ace/INet/IOS_util.h"
00008 #include "ace/Truncate.h"
00009
00010 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00011
00012 namespace ACE
00013 {
00014 namespace IOS
00015 {
00016
00017 template <class ACE_CHAR_T, class TR>
00018 String_StreamBufferBase<ACE_CHAR_T, TR>::String_StreamBufferBase (openmode mode)
00019 : BasicBufferedStreamBuffer<ACE_CHAR_T, TR> (BUFFER_SIZE, mode),
00020 string_ref_ (&string_),
00021 rd_ptr_ (0)
00022 {
00023 }
00024
00025 template <class ACE_CHAR_T, class TR>
00026 String_StreamBufferBase<ACE_CHAR_T, TR>::String_StreamBufferBase (string_type& string, openmode mode)
00027 : BasicBufferedStreamBuffer<ACE_CHAR_T, TR> (BUFFER_SIZE, mode),
00028 string_ref_ (&string),
00029 rd_ptr_ (0)
00030 {
00031 }
00032
00033 template <class ACE_CHAR_T, class TR>
00034 String_StreamBufferBase<ACE_CHAR_T, TR>::~String_StreamBufferBase ()
00035 {
00036 }
00037
00038 template <class ACE_CHAR_T, class TR>
00039 typename String_StreamBufferBase<ACE_CHAR_T, TR>::pos_type
00040 String_StreamBufferBase<ACE_CHAR_T, TR>::seekoff (
00041 off_type off,
00042 seekdir way,
00043 openmode which)
00044 {
00045
00046 if (which != this->get_mode () || which == std::ios::out)
00047 return pos_type (-1);
00048
00049 size_type spos = 0;
00050 if (way == std::ios::cur)
00051 spos = this->rd_ptr_;
00052 else if (way == std::ios::end)
00053 spos = this->string_ref_->length ();
00054 spos += off;
00055 if (spos < this->string_ref_->length ())
00056 this->rd_ptr_ = spos;
00057 else
00058 this->rd_ptr_ = this->string_ref_->length ();
00059
00060 this->setg (this->eback (), this->eback (), this->eback ());
00061 return pos_type (this->rd_ptr_);
00062 }
00063
00064 template <class ACE_CHAR_T, class TR>
00065 typename String_StreamBufferBase<ACE_CHAR_T, TR>::pos_type
00066 String_StreamBufferBase<ACE_CHAR_T, TR>::seekpos (
00067 pos_type pos,
00068 openmode which)
00069 {
00070 return this->seekoff (pos_type (pos), std::ios::beg, which);
00071 }
00072
00073 template <class ACE_CHAR_T, class TR>
00074 const typename String_StreamBufferBase<ACE_CHAR_T, TR>::string_type&
00075 String_StreamBufferBase<ACE_CHAR_T, TR>::str () const
00076 {
00077 const_cast<String_StreamBufferBase<ACE_CHAR_T, TR>*> (this)->sync ();
00078 return *this->string_ref_;
00079 }
00080
00081 template <class ACE_CHAR_T, class TR>
00082 void String_StreamBufferBase<ACE_CHAR_T, TR>::close_string ()
00083 {
00084 this->sync ();
00085 this->string_ref_ = 0;
00086 }
00087
00088 template <class ACE_CHAR_T, class TR>
00089 void String_StreamBufferBase<ACE_CHAR_T, TR>::clear_string ()
00090 {
00091 this->sync ();
00092 this->string_ref_->clear ();
00093 }
00094
00095 template <class ACE_CHAR_T, class TR>
00096 int String_StreamBufferBase<ACE_CHAR_T, TR>::read_from_stream (char_type* buffer, std::streamsize length)
00097 {
00098 int n = 0;
00099 if (this->string_ref_)
00100 {
00101 if ((this->rd_ptr_ + length) > this->string_ref_->length ())
00102 {
00103 length = this->string_ref_->length () - this->rd_ptr_;
00104 }
00105 ACE_OS::memmove (buffer, &(*this->string_ref_)[this->rd_ptr_], length * sizeof (char_type));
00106 this->rd_ptr_ += length;
00107 n = ACE_Utils::truncate_cast<int> (length);
00108 }
00109 return n;
00110 }
00111
00112 template <class ACE_CHAR_T, class TR>
00113 int String_StreamBufferBase<ACE_CHAR_T, TR>::write_to_stream (const char_type* buffer, std::streamsize length)
00114 {
00115 int n = 0;
00116 if (this->string_ref_)
00117 {
00118 this->string_ref_->append (buffer, length);
00119 n = ACE_Utils::truncate_cast<int> (length);
00120 }
00121 return n;
00122 }
00123
00124 template <class ACE_CHAR_T, class TR>
00125 String_IOSBase<ACE_CHAR_T, TR>::String_IOSBase (openmode mode)
00126 : streambuf_ (mode)
00127 {
00128 ace_ios_init (&this->streambuf_);
00129 }
00130
00131 template <class ACE_CHAR_T, class TR>
00132 String_IOSBase<ACE_CHAR_T, TR>::String_IOSBase (string_type& string, openmode mode)
00133 : streambuf_ (string, mode)
00134 {
00135 ace_ios_init (&this->streambuf_);
00136 }
00137
00138 template <class ACE_CHAR_T, class TR>
00139 String_IOSBase<ACE_CHAR_T, TR>::~String_IOSBase ()
00140 {
00141 this->close ();
00142 }
00143
00144 template <class ACE_CHAR_T, class TR>
00145 typename String_IOSBase<ACE_CHAR_T, TR>::buffer_type*
00146 String_IOSBase<ACE_CHAR_T, TR>::rdbuf ()
00147 {
00148 return &this->streambuf_;
00149 }
00150
00151 template <class ACE_CHAR_T, class TR>
00152 void String_IOSBase<ACE_CHAR_T, TR>::close ()
00153 {
00154 this->streambuf_.close_string ();
00155 }
00156
00157 template <class ACE_CHAR_T, class TR>
00158 const typename String_IOSBase<ACE_CHAR_T, TR>::buffer_type&
00159 String_IOSBase<ACE_CHAR_T, TR>::stream () const
00160 {
00161 return this->streambuf_;
00162 }
00163
00164 template <class ACE_CHAR_T, class TR>
00165 String_OStreamBase<ACE_CHAR_T, TR>::String_OStreamBase()
00166 : String_IOSBase<ACE_CHAR_T, TR> (std::ios::out),
00167 std::basic_ostream<ACE_CHAR_T, TR> (String_IOSBase<ACE_CHAR_T, TR>::rdbuf ())
00168 {
00169 }
00170
00171 template <class ACE_CHAR_T, class TR>
00172 String_OStreamBase<ACE_CHAR_T, TR>::String_OStreamBase(string_type& string)
00173 : String_IOSBase<ACE_CHAR_T, TR> (string, std::ios::out),
00174 std::basic_ostream<ACE_CHAR_T, TR> (String_IOSBase<ACE_CHAR_T, TR>::rdbuf ())
00175 {
00176 }
00177
00178 template <class ACE_CHAR_T, class TR>
00179 String_OStreamBase<ACE_CHAR_T, TR>::~String_OStreamBase()
00180 {
00181 }
00182
00183 template <class ACE_CHAR_T, class TR>
00184 const typename String_OStreamBase<ACE_CHAR_T, TR>::string_type&
00185 String_OStreamBase<ACE_CHAR_T, TR>::str () const
00186 {
00187 return this->stream ().str ();
00188 }
00189
00190 template <class ACE_CHAR_T, class TR>
00191 void String_OStreamBase<ACE_CHAR_T, TR>::clear ()
00192 {
00193 return this->rdbuf ()->clear_string ();
00194 }
00195
00196 template <class ACE_CHAR_T, class TR>
00197 String_IStreamBase<ACE_CHAR_T, TR>::String_IStreamBase()
00198 : String_IOSBase<ACE_CHAR_T, TR> (std::ios::in),
00199 std::basic_istream<ACE_CHAR_T, TR> (String_IOSBase<ACE_CHAR_T, TR>::rdbuf ())
00200 {
00201 }
00202
00203 template <class ACE_CHAR_T, class TR>
00204 String_IStreamBase<ACE_CHAR_T, TR>::String_IStreamBase(const string_type& string)
00205 : String_IOSBase<ACE_CHAR_T, TR> (const_cast<string_type&> (string),
00206 std::ios::in),
00207 std::basic_istream<ACE_CHAR_T, TR> (String_IOSBase<ACE_CHAR_T, TR>::rdbuf ())
00208 {
00209 }
00210
00211 template <class ACE_CHAR_T, class TR>
00212 String_IStreamBase<ACE_CHAR_T, TR>::~String_IStreamBase()
00213 {
00214 }
00215
00216 template <class ACE_CHAR_T, class TR>
00217 String_IStreamBase<ACE_CHAR_T, TR>&
00218 String_IStreamBase<ACE_CHAR_T, TR>::rewind ()
00219 {
00220 this->rdbuf ()->pubseekpos (0, std::ios::in);
00221 this->clear ();
00222 return *this;
00223 }
00224 }
00225 }
00226
00227 ACE_END_VERSIONED_NAMESPACE_DECL
00228
00229 #endif