Correct feature names
[ACE_TAO.git] / ACE / ace / SString.cpp
blob1d678159c6345754ed90a7ed235993909fbaef2b
1 #include "ace/Malloc_T.h"
2 #include "ace/OS_Memory.h"
3 #include "ace/SString.h"
4 #include "ace/OS_NS_string.h"
5 #include "ace/Numeric_Limits.h"
7 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
8 // FUZZ: disable check_for_streams_include
9 # include "ace/streams.h"
10 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
12 #if !defined (__ACE_INLINE__)
13 #include "ace/SString.inl"
14 #endif /* __ACE_INLINE__ */
17 // ************************************************************
19 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
21 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
22 ACE_OSTREAM_TYPE &
23 operator<< (ACE_OSTREAM_TYPE &os, const ACE_CString &cs)
25 if (cs.fast_rep () != 0)
26 os << cs.fast_rep ();
27 return os;
30 ACE_OSTREAM_TYPE &
31 operator<< (ACE_OSTREAM_TYPE &os, const ACE_WString &ws)
33 // @@ Need to figure out how to print the "wide" string
34 // on platforms that don't support "wide" strings.
35 #if defined (ACE_HAS_WCHAR)
36 os << ACE_Wide_To_Ascii (ws.fast_rep ()).char_rep ();
37 #else
38 ACE_UNUSED_ARG (ws);
39 os << "(*non-printable string*)";
40 #endif
41 return os;
44 ACE_OSTREAM_TYPE &
45 operator<< (ACE_OSTREAM_TYPE &os, const ACE_SString &ss)
47 if (ss.fast_rep () != 0)
48 os << ss.fast_rep ();
49 return os;
51 #endif /* !ACE_LACKS_IOSTREAM_TOTALLY */
53 // *****************************************************************
55 char *
56 ACE_NS_WString::char_rep (void) const
58 ACE_TRACE ("ACE_NS_WString::char_rep");
59 if (this->len_ == 0)
60 return 0;
61 else
63 char *t = 0;
65 #if defined (ACE_HAS_ALLOC_HOOKS)
66 ACE_ALLOCATOR_RETURN (t,
67 static_cast<char*>(ACE_Allocator::instance()->malloc (sizeof (char) * (this->len_ + 1))),
68 0);
69 #else
70 ACE_NEW_RETURN (t,
71 char[this->len_ + 1],
72 0);
73 #endif
75 for (size_type i = 0; i < this->len_; ++i)
76 // Note that this cast may lose data if wide chars are
77 // actually used!
78 t[i] = char (this->rep_[i]);
80 t[this->len_] = '\0';
81 return t;
85 ACE_UINT16 *
86 ACE_NS_WString::ushort_rep (void) const
88 ACE_TRACE ("ACE_NS_WString::ushort_rep");
89 if (this->len_ <= 0)
90 return 0;
91 else
93 ACE_UINT16 *t = 0;
95 #if defined (ACE_HAS_ALLOC_HOOKS)
96 ACE_ALLOCATOR_RETURN (t,
97 static_cast<ACE_UINT16*> (ACE_Allocator::instance()->malloc(sizeof(ACE_UINT16) * (this->len_ + 1))),
98 0);
99 #else
100 ACE_NEW_RETURN (t,
101 ACE_UINT16[this->len_ + 1],
103 #endif
105 for (size_type i = 0; i < this->len_; ++i)
106 // Note that this cast may lose data if wide chars are
107 // actually used!
108 t[i] = (ACE_UINT16)this->rep_[i];
110 t[this->len_] = 0;
111 return t;
115 ACE_NS_WString::ACE_NS_WString (const char *s,
116 ACE_Allocator *alloc)
117 : ACE_WString (alloc)
119 if (s == 0)
120 return;
122 this->len_ = this->buf_len_ = ACE_OS::strlen (s);
124 if (this->buf_len_ == 0)
125 return;
127 ACE_ALLOCATOR (this->rep_,
128 (ACE_WSTRING_TYPE *)
129 this->allocator_->malloc ((this->buf_len_ + 1) *
130 sizeof (ACE_WSTRING_TYPE)));
131 this->release_ = 1;
132 for (size_type i = 0; i <= this->buf_len_; ++i)
133 this->rep_[i] = s[i];
136 #if defined (ACE_WSTRING_HAS_USHORT_SUPPORT)
137 ACE_NS_WString::ACE_NS_WString (const ACE_UINT16 *s,
138 size_type len,
139 ACE_Allocator *alloc)
140 : ACE_WString (alloc)
142 if (s == 0)
143 return;
145 this->buf_len_ = len;
147 if (this->buf_len_ == 0)
148 return;
150 ACE_ALLOCATOR (this->rep_,
151 (ACE_WSTRING_TYPE *)
152 this->allocator_->malloc ((this->buf_len_) *
153 sizeof (ACE_WSTRING_TYPE)));
154 this->release_ = 1;
155 for (size_type i = 0; i < this->buf_len_; ++i)
156 this->rep_[i] = s[i];
158 #endif /* ACE_WSTRING_HAS_USHORT_SUPPORT */
160 // *****************************************************************
162 ACE_SString::size_type const ACE_SString::npos =
163 ACE_Numeric_Limits<ACE_SString::size_type>::max ();
165 ACE_ALLOC_HOOK_DEFINE(ACE_SString)
167 void
168 ACE_SString::dump (void) const
170 #if defined (ACE_HAS_DUMP)
171 ACE_TRACE ("ACE_SString::dump");
172 #endif /* ACE_HAS_DUMP */
175 // Copy constructor.
177 ACE_SString::ACE_SString (const ACE_SString &s)
178 : allocator_ (s.allocator_),
179 len_ (s.len_)
181 ACE_TRACE ("ACE_SString::ACE_SString");
183 if (this->allocator_ == 0)
184 this->allocator_ = ACE_Allocator::instance ();
186 this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1);
187 ACE_OS::memcpy ((void *) this->rep_,
188 (const void *) s.rep_,
189 this->len_);
190 this->rep_[this->len_] = '\0';
193 // Default constructor.
195 ACE_SString::ACE_SString (ACE_Allocator *alloc)
196 : allocator_ (alloc),
197 len_ (0),
198 rep_ (0)
201 ACE_TRACE ("ACE_SString::ACE_SString");
203 if (this->allocator_ == 0)
204 this->allocator_ = ACE_Allocator::instance ();
206 this->len_ = 0;
207 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
208 this->rep_[this->len_] = '\0';
211 // Set the underlying pointer (does not copy memory).
213 void
214 ACE_SString::rep (char *s)
216 ACE_TRACE ("ACE_SString::rep");
218 this->rep_ = s;
220 if (s == 0)
221 this->len_ = 0;
222 else
223 this->len_ = ACE_OS::strlen (s);
226 // Constructor that actually copies memory.
228 ACE_SString::ACE_SString (const char *s,
229 ACE_Allocator *alloc)
230 : allocator_ (alloc)
232 ACE_TRACE ("ACE_SString::ACE_SString");
234 if (this->allocator_ == 0)
235 this->allocator_ = ACE_Allocator::instance ();
237 if (s == 0)
239 this->len_ = 0;
240 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
241 this->rep_[this->len_] = '\0';
243 else
245 this->len_ = ACE_OS::strlen (s);
246 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
247 ACE_OS::strcpy (this->rep_, s);
251 ACE_SString::ACE_SString (char c,
252 ACE_Allocator *alloc)
253 : allocator_ (alloc)
255 ACE_TRACE ("ACE_SString::ACE_SString");
257 if (this->allocator_ == 0)
258 this->allocator_ = ACE_Allocator::instance ();
260 this->len_ = 1;
261 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
262 this->rep_[0] = c;
263 this->rep_[this->len_] = '\0';
266 // Constructor that actually copies memory.
268 ACE_SString::ACE_SString (const char *s,
269 size_type len,
270 ACE_Allocator *alloc)
271 : allocator_ (alloc)
273 ACE_TRACE ("ACE_SString::ACE_SString");
275 if (this->allocator_ == 0)
276 this->allocator_ = ACE_Allocator::instance ();
278 if (s == 0)
280 this->len_ = 0;
281 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
282 this->rep_[this->len_] = '\0';
284 else
286 this->len_ = len;
287 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
288 ACE_OS::memcpy (this->rep_, s, len);
289 this->rep_[len] = '\0'; // Make sure to NUL terminate this!
293 // Assignment operator (does copy memory).
295 ACE_SString &
296 ACE_SString::operator= (const ACE_SString &s)
298 ACE_TRACE ("ACE_SString::operator=");
299 // Check for identify.
301 if (this != &s)
303 // Only reallocate if we don't have enough space...
304 if (this->len_ < s.len_)
306 this->allocator_->free (this->rep_);
307 this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1);
309 this->len_ = s.len_;
310 ACE_OS::strcpy (this->rep_, s.rep_);
313 return *this;
316 // Return substring.
317 ACE_SString
318 ACE_SString::substring (size_type offset,
319 size_type length) const
321 size_t count = length;
323 // case 1. empty string
324 if (len_ == 0)
325 return ACE_SString ();
327 // case 2. start pos l
328 if (offset >= len_)
329 return ACE_SString ();
331 // get all remaining bytes
332 if (length == npos || count > (this->len_ - offset))
333 count = len_ - offset;
335 return ACE_SString (&rep_[offset], count, this->allocator_);
338 // ************************************************************
340 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
341 template char ACE_String_Base<char>::NULL_String_;
342 template ACE_WSTRING_TYPE ACE_String_Base<ACE_WSTRING_TYPE>::NULL_String_;
343 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */
345 ACE_END_VERSIONED_NAMESPACE_DECL