1 // $Id: SString.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #include "ace/Malloc_T.h"
4 #include "ace/OS_Memory.h"
5 #if !defined (ACE_HAS_WINCE)
6 //# include "ace/Service_Config.h"
7 #endif /* !ACE_HAS_WINCE */
8 #include "ace/SString.h"
9 #include "ace/Auto_Ptr.h"
10 #include "ace/OS_NS_string.h"
11 #include "ace/Numeric_Limits.h"
13 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
14 // FUZZ: disable check_for_streams_include
15 # include "ace/streams.h"
16 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
18 #if !defined (__ACE_INLINE__)
19 #include "ace/SString.inl"
20 #endif /* __ACE_INLINE__ */
25 "SString.cpp,v 4.61 2001/03/04 00:55:30 brunsch Exp")
28 // ************************************************************
30 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
32 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
34 operator<< (ACE_OSTREAM_TYPE
&os
, const ACE_CString
&cs
)
36 if (cs
.fast_rep () != 0)
42 operator<< (ACE_OSTREAM_TYPE
&os
, const ACE_WString
&ws
)
44 // @@ Need to figure out how to print the "wide" string
45 // on platforms that don't support "wide" strings.
46 #if defined (ACE_HAS_WCHAR)
47 os
<< ACE_Wide_To_Ascii (ws
.fast_rep ()).char_rep ();
50 os
<< "(*non-printable string*)";
56 operator<< (ACE_OSTREAM_TYPE
&os
, const ACE_SString
&ss
)
58 if (ss
.fast_rep () != 0)
62 #endif /* !ACE_LACKS_IOSTREAM_TOTALLY */
64 // *****************************************************************
67 ACE_NS_WString::char_rep (void) const
69 ACE_TRACE ("ACE_NS_WString::char_rep");
80 for (size_type i
= 0; i
< this->len_
; ++i
)
81 // Note that this cast may lose data if wide chars are
83 t
[i
] = char (this->rep_
[i
]);
91 ACE_NS_WString::ushort_rep (void) const
93 ACE_TRACE ("ACE_NS_WString::ushort_rep");
101 ACE_USHORT16
[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_USHORT16
)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_USHORT16
*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 (void) 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 ACE_Tokenizer::ACE_Tokenizer (ACE_TCHAR
*buffer
)
342 preserves_index_ (0),
348 ACE_Tokenizer::delimiter (ACE_TCHAR d
)
350 if (delimiter_index_
== MAX_DELIMITERS
)
353 delimiters_
[delimiter_index_
].delimiter_
= d
;
354 delimiters_
[delimiter_index_
].replace_
= 0;
360 ACE_Tokenizer::delimiter_replace (ACE_TCHAR d
,
361 ACE_TCHAR replacement
)
363 // Make it possible to replace delimiters on-the-fly, e.g., parse
364 // string until certain token count and then copy rest of the
366 for (int i
= 0; i
< delimiter_index_
; i
++)
367 if (delimiters_
[i
].delimiter_
== d
)
369 delimiters_
[i
].replacement_
= replacement
;
370 delimiters_
[i
].replace_
= 1;
374 if (delimiter_index_
>= MAX_DELIMITERS
)
377 delimiters_
[delimiter_index_
].delimiter_
= d
;
378 delimiters_
[delimiter_index_
].replacement_
= replacement
;
379 delimiters_
[delimiter_index_
].replace_
= 1;
385 ACE_Tokenizer::preserve_designators (ACE_TCHAR start
,
389 if (preserves_index_
== MAX_PRESERVES
)
392 preserves_
[preserves_index_
].start_
= start
;
393 preserves_
[preserves_index_
].stop_
= stop
;
394 preserves_
[preserves_index_
].strip_
= strip
;
400 ACE_Tokenizer::is_delimiter (ACE_TCHAR d
,
406 for (int x
= 0; x
< delimiter_index_
; x
++)
407 if (delimiters_
[x
].delimiter_
== d
)
409 if (delimiters_
[x
].replace_
)
411 r
= delimiters_
[x
].replacement_
;
421 ACE_Tokenizer::is_preserve_designator (ACE_TCHAR start
,
425 for (int x
= 0; x
< preserves_index_
; x
++)
426 if (preserves_
[x
].start_
== start
)
428 stop
= preserves_
[x
].stop_
;
429 strip
= preserves_
[x
].strip_
;
437 ACE_Tokenizer::next (void)
439 // Check if the previous pass was the last one in the buffer.
446 ACE_TCHAR replacement
= 0;
448 ACE_TCHAR
*next_token
;
450 // Skip all leading delimiters.
453 // Check for end of string.
454 if (buffer_
[index_
] == '\0')
456 // If we hit EOS at the start, return 0.
461 if (this->is_delimiter (buffer_
[index_
],
469 // When we reach this point, buffer_[index_] is a non-delimiter and
470 // not EOS - the start of our next_token.
471 next_token
= buffer_
+ index_
;
473 // A preserved region is it's own token.
476 if (this->is_preserve_designator (buffer_
[index_
],
482 if (buffer_
[index_
] == '\0')
488 if (buffer_
[index_
] == stop
)
494 // Skip start preserve designator.
496 // Zap the stop preserve designator.
497 buffer_
[index_
] = '\0';
498 // Increment to the next token.
505 // Step through finding the next delimiter or EOS.
511 // Check for delimiter.
512 if (this->is_delimiter (buffer_
[index_
],
516 // Replace the delimiter.
518 buffer_
[index_
] = replacement
;
520 // Move the pointer up and return.
525 // A preserve designator signifies the end of this token.
526 if (this->is_preserve_designator (buffer_
[index_
],
531 // Check for end of string.
532 if (buffer_
[index_
] == '\0')
543 // *************************************************************
545 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
546 template char ACE_String_Base
<char>::NULL_String_
;
547 template ACE_WSTRING_TYPE ACE_String_Base
<ACE_WSTRING_TYPE
>::NULL_String_
;
548 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */
550 ACE_END_VERSIONED_NAMESPACE_DECL