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__ */
16 // ************************************************************
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
20 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
22 operator<< (ACE_OSTREAM_TYPE
&os
, const ACE_CString
&cs
)
24 if (cs
.fast_rep () != 0)
30 operator<< (ACE_OSTREAM_TYPE
&os
, const ACE_WString
&ws
)
32 // @@ Need to figure out how to print the "wide" string
33 // on platforms that don't support "wide" strings.
34 #if defined (ACE_HAS_WCHAR)
35 os
<< ACE_Wide_To_Ascii (ws
.fast_rep ()).char_rep ();
38 os
<< "(*non-printable string*)";
44 operator<< (ACE_OSTREAM_TYPE
&os
, const ACE_SString
&ss
)
46 if (ss
.fast_rep () != 0)
50 #endif /* !ACE_LACKS_IOSTREAM_TOTALLY */
52 // *****************************************************************
55 ACE_NS_WString::char_rep () const
57 ACE_TRACE ("ACE_NS_WString::char_rep");
64 #if defined (ACE_HAS_ALLOC_HOOKS)
65 ACE_ALLOCATOR_RETURN (t
,
66 static_cast<char*>(ACE_Allocator::instance()->malloc (sizeof (char) * (this->len_
+ 1))),
74 for (size_type i
= 0; i
< this->len_
; ++i
)
75 // Note that this cast may lose data if wide chars are
77 t
[i
] = char (this->rep_
[i
]);
85 ACE_NS_WString::ushort_rep () const
87 ACE_TRACE ("ACE_NS_WString::ushort_rep");
94 #if defined (ACE_HAS_ALLOC_HOOKS)
95 ACE_ALLOCATOR_RETURN (t
,
96 static_cast<ACE_UINT16
*> (ACE_Allocator::instance()->malloc(sizeof(ACE_UINT16
) * (this->len_
+ 1))),
100 ACE_UINT16
[this->len_
+ 1],
104 for (size_type i
= 0; i
< this->len_
; ++i
)
105 // Note that this cast may lose data if wide chars are
107 t
[i
] = (ACE_UINT16
)this->rep_
[i
];
114 ACE_NS_WString::ACE_NS_WString (const char *s
,
115 ACE_Allocator
*alloc
)
116 : ACE_WString (alloc
)
121 this->len_
= this->buf_len_
= ACE_OS::strlen (s
);
123 if (this->buf_len_
== 0)
126 ACE_ALLOCATOR (this->rep_
,
128 this->allocator_
->malloc ((this->buf_len_
+ 1) *
129 sizeof (ACE_WSTRING_TYPE
)));
131 for (size_type i
= 0; i
<= this->buf_len_
; ++i
)
132 this->rep_
[i
] = s
[i
];
135 #if defined (ACE_WSTRING_HAS_USHORT_SUPPORT)
136 ACE_NS_WString::ACE_NS_WString (const ACE_UINT16
*s
,
138 ACE_Allocator
*alloc
)
139 : ACE_WString (alloc
)
144 this->buf_len_
= len
;
146 if (this->buf_len_
== 0)
149 ACE_ALLOCATOR (this->rep_
,
151 this->allocator_
->malloc ((this->buf_len_
) *
152 sizeof (ACE_WSTRING_TYPE
)));
154 for (size_type i
= 0; i
< this->buf_len_
; ++i
)
155 this->rep_
[i
] = s
[i
];
157 #endif /* ACE_WSTRING_HAS_USHORT_SUPPORT */
159 // *****************************************************************
161 ACE_SString::size_type
const ACE_SString::npos
=
162 ACE_Numeric_Limits
<ACE_SString::size_type
>::max ();
164 ACE_ALLOC_HOOK_DEFINE(ACE_SString
)
167 ACE_SString::dump () const
169 #if defined (ACE_HAS_DUMP)
170 ACE_TRACE ("ACE_SString::dump");
171 #endif /* ACE_HAS_DUMP */
176 ACE_SString::ACE_SString (const ACE_SString
&s
)
177 : allocator_ (s
.allocator_
),
180 ACE_TRACE ("ACE_SString::ACE_SString");
182 if (this->allocator_
== 0)
183 this->allocator_
= ACE_Allocator::instance ();
185 this->rep_
= (char *) this->allocator_
->malloc (s
.len_
+ 1);
186 ACE_OS::memcpy ((void *) this->rep_
,
187 (const void *) s
.rep_
,
189 this->rep_
[this->len_
] = '\0';
192 // Default constructor.
194 ACE_SString::ACE_SString (ACE_Allocator
*alloc
)
195 : allocator_ (alloc
),
200 ACE_TRACE ("ACE_SString::ACE_SString");
202 if (this->allocator_
== 0)
203 this->allocator_
= ACE_Allocator::instance ();
206 this->rep_
= (char *) this->allocator_
->malloc (this->len_
+ 1);
207 this->rep_
[this->len_
] = '\0';
210 // Set the underlying pointer (does not copy memory).
213 ACE_SString::rep (char *s
)
215 ACE_TRACE ("ACE_SString::rep");
222 this->len_
= ACE_OS::strlen (s
);
225 // Constructor that actually copies memory.
227 ACE_SString::ACE_SString (const char *s
,
228 ACE_Allocator
*alloc
)
231 ACE_TRACE ("ACE_SString::ACE_SString");
233 if (this->allocator_
== 0)
234 this->allocator_
= ACE_Allocator::instance ();
239 this->rep_
= (char *) this->allocator_
->malloc (this->len_
+ 1);
240 this->rep_
[this->len_
] = '\0';
244 this->len_
= ACE_OS::strlen (s
);
245 this->rep_
= (char *) this->allocator_
->malloc (this->len_
+ 1);
246 ACE_OS::strcpy (this->rep_
, s
);
250 ACE_SString::ACE_SString (char c
,
251 ACE_Allocator
*alloc
)
254 ACE_TRACE ("ACE_SString::ACE_SString");
256 if (this->allocator_
== 0)
257 this->allocator_
= ACE_Allocator::instance ();
260 this->rep_
= (char *) this->allocator_
->malloc (this->len_
+ 1);
262 this->rep_
[this->len_
] = '\0';
265 // Constructor that actually copies memory.
267 ACE_SString::ACE_SString (const char *s
,
269 ACE_Allocator
*alloc
)
272 ACE_TRACE ("ACE_SString::ACE_SString");
274 if (this->allocator_
== 0)
275 this->allocator_
= ACE_Allocator::instance ();
280 this->rep_
= (char *) this->allocator_
->malloc (this->len_
+ 1);
281 this->rep_
[this->len_
] = '\0';
286 this->rep_
= (char *) this->allocator_
->malloc (this->len_
+ 1);
287 ACE_OS::memcpy (this->rep_
, s
, len
);
288 this->rep_
[len
] = '\0'; // Make sure to NUL terminate this!
292 // Assignment operator (does copy memory).
295 ACE_SString::operator= (const ACE_SString
&s
)
297 ACE_TRACE ("ACE_SString::operator=");
298 // Check for identify.
302 // Only reallocate if we don't have enough space...
303 if (this->len_
< s
.len_
)
305 this->allocator_
->free (this->rep_
);
306 this->rep_
= (char *) this->allocator_
->malloc (s
.len_
+ 1);
309 ACE_OS::strcpy (this->rep_
, s
.rep_
);
317 ACE_SString::substring (size_type offset
,
318 size_type length
) const
320 size_t count
= length
;
322 // case 1. empty string
324 return ACE_SString ();
326 // case 2. start pos l
328 return ACE_SString ();
330 // get all remaining bytes
331 if (length
== npos
|| count
> (this->len_
- offset
))
332 count
= len_
- offset
;
334 return ACE_SString (&rep_
[offset
], count
, this->allocator_
);
337 // ************************************************************
339 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
340 template char ACE_String_Base
<char>::NULL_String_
;
341 template ACE_WSTRING_TYPE ACE_String_Base
<ACE_WSTRING_TYPE
>::NULL_String_
;
342 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */
344 ACE_END_VERSIONED_NAMESPACE_DECL