Go to the documentation of this file.00001
00002
00003 #include "ace/Malloc_T.h"
00004 #include "ace/OS_Memory.h"
00005 #include "ace/SString.h"
00006 #include "ace/Auto_Ptr.h"
00007 #include "ace/OS_NS_string.h"
00008 #include "ace/Numeric_Limits.h"
00009
00010 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
00011
00012 # include "ace/streams.h"
00013 #endif
00014
00015 #if !defined (__ACE_INLINE__)
00016 #include "ace/SString.inl"
00017 #endif
00018
00019
00020 ACE_RCSID (ace,
00021 SString,
00022 "SString.cpp,v 4.61 2001/03/04 00:55:30 brunsch Exp")
00023
00024
00025
00026
00027 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00028
00029 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
00030 ACE_OSTREAM_TYPE &
00031 operator<< (ACE_OSTREAM_TYPE &os, const ACE_CString &cs)
00032 {
00033 if (cs.fast_rep () != 0)
00034 os << cs.fast_rep ();
00035 return os;
00036 }
00037
00038 ACE_OSTREAM_TYPE &
00039 operator<< (ACE_OSTREAM_TYPE &os, const ACE_WString &ws)
00040 {
00041
00042
00043 #if defined (ACE_HAS_WCHAR)
00044 os << ACE_Wide_To_Ascii (ws.fast_rep ()).char_rep ();
00045 #else
00046 ACE_UNUSED_ARG (ws);
00047 os << "(*non-printable string*)";
00048 #endif
00049 return os;
00050 }
00051
00052 ACE_OSTREAM_TYPE &
00053 operator<< (ACE_OSTREAM_TYPE &os, const ACE_SString &ss)
00054 {
00055 if (ss.fast_rep () != 0)
00056 os << ss.fast_rep ();
00057 return os;
00058 }
00059 #endif
00060
00061
00062
00063 char *
00064 ACE_NS_WString::char_rep (void) const
00065 {
00066 ACE_TRACE ("ACE_NS_WString::char_rep");
00067 if (this->len_ == 0)
00068 return 0;
00069 else
00070 {
00071 char *t = 0;
00072
00073 ACE_NEW_RETURN (t,
00074 char[this->len_ + 1],
00075 0);
00076
00077 for (size_type i = 0; i < this->len_; ++i)
00078
00079
00080 t[i] = char (this->rep_[i]);
00081
00082 t[this->len_] = '\0';
00083 return t;
00084 }
00085 }
00086
00087 ACE_USHORT16 *
00088 ACE_NS_WString::ushort_rep (void) const
00089 {
00090 ACE_TRACE ("ACE_NS_WString::ushort_rep");
00091 if (this->len_ <= 0)
00092 return 0;
00093 else
00094 {
00095 ACE_USHORT16 *t = 0;
00096
00097 ACE_NEW_RETURN (t,
00098 ACE_USHORT16[this->len_ + 1],
00099 0);
00100
00101 for (size_type i = 0; i < this->len_; ++i)
00102
00103
00104 t[i] = (ACE_USHORT16)this->rep_[i];
00105
00106 t[this->len_] = 0;
00107 return t;
00108 }
00109 }
00110
00111 ACE_NS_WString::ACE_NS_WString (const char *s,
00112 ACE_Allocator *alloc)
00113 : ACE_WString (alloc)
00114 {
00115 if (s == 0)
00116 return;
00117
00118 this->len_ = this->buf_len_ = ACE_OS::strlen (s);
00119
00120 if (this->buf_len_ == 0)
00121 return;
00122
00123 ACE_ALLOCATOR (this->rep_,
00124 (ACE_WSTRING_TYPE *)
00125 this->allocator_->malloc ((this->buf_len_ + 1) *
00126 sizeof (ACE_WSTRING_TYPE)));
00127 this->release_ = 1;
00128 for (size_type i = 0; i <= this->buf_len_; ++i)
00129 this->rep_[i] = s[i];
00130 }
00131
00132 #if defined (ACE_WSTRING_HAS_USHORT_SUPPORT)
00133 ACE_NS_WString::ACE_NS_WString (const ACE_USHORT16 *s,
00134 size_type len,
00135 ACE_Allocator *alloc)
00136 : ACE_WString (alloc)
00137 {
00138 if (s == 0)
00139 return;
00140
00141 this->buf_len_ = len;
00142
00143 if (this->buf_len_ == 0)
00144 return;
00145
00146 ACE_ALLOCATOR (this->rep_,
00147 (ACE_WSTRING_TYPE *)
00148 this->allocator_->malloc ((this->buf_len_) *
00149 sizeof (ACE_WSTRING_TYPE)));
00150 this->release_ = 1;
00151 for (size_type i = 0; i < this->buf_len_; ++i)
00152 this->rep_[i] = s[i];
00153 }
00154 #endif
00155
00156
00157
00158 ACE_SString::size_type const ACE_SString::npos =
00159 ACE_Numeric_Limits<ACE_SString::size_type>::max ();
00160
00161 ACE_ALLOC_HOOK_DEFINE(ACE_SString)
00162
00163 void
00164 ACE_SString::dump (void) const
00165 {
00166 #if defined (ACE_HAS_DUMP)
00167 ACE_TRACE ("ACE_SString::dump");
00168 #endif
00169 }
00170
00171
00172
00173 ACE_SString::ACE_SString (const ACE_SString &s)
00174 : allocator_ (s.allocator_),
00175 len_ (s.len_)
00176 {
00177 ACE_TRACE ("ACE_SString::ACE_SString");
00178
00179 if (this->allocator_ == 0)
00180 this->allocator_ = ACE_Allocator::instance ();
00181
00182 this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1);
00183 ACE_OS::memcpy ((void *) this->rep_,
00184 (const void *) s.rep_,
00185 this->len_);
00186 this->rep_[this->len_] = '\0';
00187 }
00188
00189
00190
00191 ACE_SString::ACE_SString (ACE_Allocator *alloc)
00192 : allocator_ (alloc),
00193 len_ (0),
00194 rep_ (0)
00195
00196 {
00197 ACE_TRACE ("ACE_SString::ACE_SString");
00198
00199 if (this->allocator_ == 0)
00200 this->allocator_ = ACE_Allocator::instance ();
00201
00202 this->len_ = 0;
00203 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00204 this->rep_[this->len_] = '\0';
00205 }
00206
00207
00208
00209 void
00210 ACE_SString::rep (char *s)
00211 {
00212 ACE_TRACE ("ACE_SString::rep");
00213
00214 this->rep_ = s;
00215
00216 if (s == 0)
00217 this->len_ = 0;
00218 else
00219 this->len_ = ACE_OS::strlen (s);
00220 }
00221
00222
00223
00224 ACE_SString::ACE_SString (const char *s,
00225 ACE_Allocator *alloc)
00226 : allocator_ (alloc)
00227 {
00228 ACE_TRACE ("ACE_SString::ACE_SString");
00229
00230 if (this->allocator_ == 0)
00231 this->allocator_ = ACE_Allocator::instance ();
00232
00233 if (s == 0)
00234 {
00235 this->len_ = 0;
00236 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00237 this->rep_[this->len_] = '\0';
00238 }
00239 else
00240 {
00241 this->len_ = ACE_OS::strlen (s);
00242 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00243 ACE_OS::strcpy (this->rep_, s);
00244 }
00245 }
00246
00247 ACE_SString::ACE_SString (char c,
00248 ACE_Allocator *alloc)
00249 : allocator_ (alloc)
00250 {
00251 ACE_TRACE ("ACE_SString::ACE_SString");
00252
00253 if (this->allocator_ == 0)
00254 this->allocator_ = ACE_Allocator::instance ();
00255
00256 this->len_ = 1;
00257 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00258 this->rep_[0] = c;
00259 this->rep_[this->len_] = '\0';
00260 }
00261
00262
00263
00264 ACE_SString::ACE_SString (const char *s,
00265 size_type len,
00266 ACE_Allocator *alloc)
00267 : allocator_ (alloc)
00268 {
00269 ACE_TRACE ("ACE_SString::ACE_SString");
00270
00271 if (this->allocator_ == 0)
00272 this->allocator_ = ACE_Allocator::instance ();
00273
00274 if (s == 0)
00275 {
00276 this->len_ = 0;
00277 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00278 this->rep_[this->len_] = '\0';
00279 }
00280 else
00281 {
00282 this->len_ = len;
00283 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00284 ACE_OS::memcpy (this->rep_, s, len);
00285 this->rep_[len] = '\0';
00286 }
00287 }
00288
00289
00290
00291 ACE_SString &
00292 ACE_SString::operator= (const ACE_SString &s)
00293 {
00294 ACE_TRACE ("ACE_SString::operator=");
00295
00296
00297 if (this != &s)
00298 {
00299
00300 if (this->len_ < s.len_)
00301 {
00302 this->allocator_->free (this->rep_);
00303 this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1);
00304 }
00305 this->len_ = s.len_;
00306 ACE_OS::strcpy (this->rep_, s.rep_);
00307 }
00308
00309 return *this;
00310 }
00311
00312
00313 ACE_SString
00314 ACE_SString::substring (size_type offset,
00315 size_type length) const
00316 {
00317 size_t count = length;
00318
00319
00320 if (len_ == 0)
00321 return ACE_SString ();
00322
00323
00324 if (offset >= len_)
00325 return ACE_SString ();
00326
00327
00328 if (length == npos || count > (this->len_ - offset))
00329 count = len_ - offset;
00330
00331 return ACE_SString (&rep_[offset], count, this->allocator_);
00332 }
00333
00334
00335
00336 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
00337 template char ACE_String_Base<char>::NULL_String_;
00338 template ACE_WSTRING_TYPE ACE_String_Base<ACE_WSTRING_TYPE>::NULL_String_;
00339 #endif
00340
00341 ACE_END_VERSIONED_NAMESPACE_DECL