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)
23 operator<< (ACE_OSTREAM_TYPE
&os
, const ACE_CString
&cs
)
25 if (cs
.fast_rep () != 0)
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 ();
39 os
<< "(*non-printable string*)";
45 operator<< (ACE_OSTREAM_TYPE
&os
, const ACE_SString
&ss
)
47 if (ss
.fast_rep () != 0)
51 #endif /* !ACE_LACKS_IOSTREAM_TOTALLY */
53 // *****************************************************************
56 ACE_NS_WString::char_rep (void) const
58 ACE_TRACE ("ACE_NS_WString::char_rep");
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))),
75 for (size_type i
= 0; i
< this->len_
; ++i
)
76 // Note that this cast may lose data if wide chars are
78 t
[i
] = char (this->rep_
[i
]);
86 ACE_NS_WString::ushort_rep (void) const
88 ACE_TRACE ("ACE_NS_WString::ushort_rep");
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))),
101 ACE_UINT16
[this->len_
+ 1],
105 for (size_type i
= 0; i
< this->len_
; ++i
)
106 // Note that this cast may lose data if wide chars are
108 t
[i
] = (ACE_UINT16
)this->rep_
[i
];
115 ACE_NS_WString::ACE_NS_WString (const char *s
,
116 ACE_Allocator
*alloc
)
117 : ACE_WString (alloc
)
122 this->len_
= this->buf_len_
= ACE_OS::strlen (s
);
124 if (this->buf_len_
== 0)
127 ACE_ALLOCATOR (this->rep_
,
129 this->allocator_
->malloc ((this->buf_len_
+ 1) *
130 sizeof (ACE_WSTRING_TYPE
)));
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
,
139 ACE_Allocator
*alloc
)
140 : ACE_WString (alloc
)
145 this->buf_len_
= len
;
147 if (this->buf_len_
== 0)
150 ACE_ALLOCATOR (this->rep_
,
152 this->allocator_
->malloc ((this->buf_len_
) *
153 sizeof (ACE_WSTRING_TYPE
)));
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
)
168 ACE_SString::dump (void) const
170 #if defined (ACE_HAS_DUMP)
171 ACE_TRACE ("ACE_SString::dump");
172 #endif /* ACE_HAS_DUMP */
177 ACE_SString::ACE_SString (const ACE_SString
&s
)
178 : allocator_ (s
.allocator_
),
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_
,
190 this->rep_
[this->len_
] = '\0';
193 // Default constructor.
195 ACE_SString::ACE_SString (ACE_Allocator
*alloc
)
196 : allocator_ (alloc
),
201 ACE_TRACE ("ACE_SString::ACE_SString");
203 if (this->allocator_
== 0)
204 this->allocator_
= ACE_Allocator::instance ();
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).
214 ACE_SString::rep (char *s
)
216 ACE_TRACE ("ACE_SString::rep");
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
)
232 ACE_TRACE ("ACE_SString::ACE_SString");
234 if (this->allocator_
== 0)
235 this->allocator_
= ACE_Allocator::instance ();
240 this->rep_
= (char *) this->allocator_
->malloc (this->len_
+ 1);
241 this->rep_
[this->len_
] = '\0';
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
)
255 ACE_TRACE ("ACE_SString::ACE_SString");
257 if (this->allocator_
== 0)
258 this->allocator_
= ACE_Allocator::instance ();
261 this->rep_
= (char *) this->allocator_
->malloc (this->len_
+ 1);
263 this->rep_
[this->len_
] = '\0';
266 // Constructor that actually copies memory.
268 ACE_SString::ACE_SString (const char *s
,
270 ACE_Allocator
*alloc
)
273 ACE_TRACE ("ACE_SString::ACE_SString");
275 if (this->allocator_
== 0)
276 this->allocator_
= ACE_Allocator::instance ();
281 this->rep_
= (char *) this->allocator_
->malloc (this->len_
+ 1);
282 this->rep_
[this->len_
] = '\0';
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).
296 ACE_SString::operator= (const ACE_SString
&s
)
298 ACE_TRACE ("ACE_SString::operator=");
299 // Check for identify.
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);
310 ACE_OS::strcpy (this->rep_
, s
.rep_
);
318 ACE_SString::substring (size_type offset
,
319 size_type length
) const
321 size_t count
= length
;
323 // case 1. empty string
325 return ACE_SString ();
327 // case 2. start pos l
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