My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
strbuf.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #ifndef _RTL_STRBUF_HXX_
30 #define _RTL_STRBUF_HXX_
31 
32 #include "sal/config.h"
33 
34 #include <cassert>
35 
36 #include <rtl/strbuf.h>
37 #include <rtl/string.hxx>
38 #include <rtl/stringutils.hxx>
39 
40 #ifdef __cplusplus
41 
42 // The unittest uses slightly different code to help check that the proper
43 // calls are made. The class is put into a different namespace to make
44 // sure the compiler generates a different (if generating also non-inline)
45 // copy of the function and does not merge them together. The class
46 // is "brought" into the proper rtl namespace by a typedef below.
47 #ifdef RTL_STRING_UNITTEST
48 #define rtl rtlunittest
49 #endif
50 
51 namespace rtl
52 {
53 
54 #ifdef RTL_STRING_UNITTEST
55 #undef rtl
56 // helper macro to make functions appear more readable
57 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
58 #else
59 #define RTL_STRING_CONST_FUNCTION
60 #endif
61 
101 {
102 public:
108  : pData(NULL)
109  , nCapacity( 16 )
110  {
111  rtl_string_new_WithLength( &pData, nCapacity );
112  }
113 
120  OStringBuffer( const OStringBuffer & value )
121  : pData(NULL)
122  , nCapacity( value.nCapacity )
123  {
124  rtl_stringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
125  }
126 
133  explicit OStringBuffer(int length)
134  : pData(NULL)
135  , nCapacity( length )
136  {
137  rtl_string_new_WithLength( &pData, length );
138  }
139 
151  : pData(NULL)
152  , nCapacity( value.getLength() + 16 )
153  {
154  rtl_stringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
155  }
156 
161 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN // see the OString ctors
162  OStringBuffer( const char* value )
163  : pData(NULL)
164  {
165  sal_Int32 length = rtl_str_getLength( value );
166  nCapacity = length + 16;
167  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
168  }
169 #else
170  template< typename T >
172  : pData(NULL)
173  {
174  sal_Int32 length = rtl_str_getLength( value );
175  nCapacity = length + 16;
176  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
177  }
178 
179  template< typename T >
181  : pData(NULL)
182  {
183  sal_Int32 length = rtl_str_getLength( value );
184  nCapacity = length + 16;
185  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
186  }
187 
199  template< typename T >
201  : pData(NULL)
202  , nCapacity( internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
203  {
205 #ifdef RTL_STRING_UNITTEST
206  rtl_string_unittest_const_literal = true;
207 #endif
208  }
209 #endif // HAVE_SFINAE_ANONYMOUS_BROKEN
210 
223  OStringBuffer(const sal_Char * value, sal_Int32 length)
224  : pData(NULL)
225  , nCapacity( length + 16 )
226  {
227  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
228  }
229 
233  {
234  if (this != &value)
235  {
237  value.nCapacity,
238  value.pData);
239  nCapacity = value.nCapacity;
240  }
241  return *this;
242  }
243 
248  {
249  rtl_string_release( pData );
250  }
251 
261  {
262  OString aRet( pData );
263  rtl_string_new(&pData);
264  nCapacity = 0;
265  return aRet;
266  }
267 
273  sal_Int32 getLength() const
274  {
275  return pData->length;
276  }
277 
288  sal_Int32 getCapacity() const
289  {
290  return nCapacity;
291  }
292 
304  void ensureCapacity(sal_Int32 minimumCapacity)
305  {
306  rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
307  }
308 
327  void setLength(sal_Int32 newLength)
328  {
329  assert(newLength >= 0);
330  // Avoid modifications if pData points to const empty string:
331  if( newLength != pData->length )
332  {
333  if( newLength > nCapacity )
334  rtl_stringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
335  else
336  pData->buffer[newLength] = '\0';
337  pData->length = newLength;
338  }
339  }
340 
354  SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
355  sal_Char charAt( sal_Int32 index )
356  {
357  assert(index >= 0 && index < pData->length);
358  return pData->buffer[ index ];
359  }
360 
371  SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
372  OStringBuffer & setCharAt(sal_Int32 index, sal_Char ch)
373  {
374  assert(index >= 0 && index < pData->length);
375  pData->buffer[ index ] = ch;
376  return *this;
377  }
378 
382  const sal_Char* getStr() const { return pData->buffer; }
383 
393  sal_Char & operator [](sal_Int32 index) { return pData->buffer[index]; }
394 
399  const OString toString() const
400  {
401  return OString(pData->buffer, pData->length);
402  }
403 
415  {
416  return append( str.getStr(), str.getLength() );
417  }
418 
430 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
431  OStringBuffer & append( const sal_Char * str )
432  {
433  return append( str, rtl_str_getLength( str ) );
434  }
435 #else
436  template< typename T >
438  {
439  return append( str, rtl_str_getLength( str ) );
440  }
441 
442  template< typename T >
444  {
445  return append( str, rtl_str_getLength( str ) );
446  }
447 
453  template< typename T >
455  {
458  return *this;
459  }
460 #endif
461 
475  OStringBuffer & append( const sal_Char * str, sal_Int32 len)
476  {
477  // insert behind the last character
478  rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
479  return *this;
480  }
481 
494  {
496  return append( sz, rtl_str_valueOfBoolean( sz, b ) );
497  }
498 
510  {
511  return append( &c, 1 );
512  }
513 
525  OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
526  {
528  return append( sz, rtl_str_valueOfInt32( sz, i, radix ) );
529  }
530 
542  OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
543  {
545  return append( sz, rtl_str_valueOfInt64( sz, l, radix ) );
546  }
547 
560  {
562  return append( sz, rtl_str_valueOfFloat( sz, f ) );
563  }
564 
576  OStringBuffer & append(double d)
577  {
579  return append( sz, rtl_str_valueOfDouble( sz, d ) );
580  }
581 
597  OStringBuffer & insert(sal_Int32 offset, const OString & str)
598  {
599  return insert( offset, str.getStr(), str.getLength() );
600  }
601 
619 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
620  OStringBuffer & insert( sal_Int32 offset, const sal_Char * str )
621  {
622  return insert( offset, str, rtl_str_getLength( str ) );
623  }
624 #else
625  template< typename T >
626  typename internal::CharPtrDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, const T& str )
627  {
628  return insert( offset, str, rtl_str_getLength( str ) );
629  }
630 
631  template< typename T >
633  {
634  return insert( offset, str, rtl_str_getLength( str ) );
635  }
636 
642  template< typename T >
644  {
646  rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
647  return *this;
648  }
649 #endif
650 
669  OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len)
670  {
671  // insert behind the last character
672  rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
673  return *this;
674  }
675 
693  OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
694  {
696  return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
697  }
698 
715  OStringBuffer & insert(sal_Int32 offset, sal_Char c)
716  {
717  return insert( offset, &c, 1 );
718  }
719 
737  OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
738  {
740  return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) );
741  }
742 
760  OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
761  {
763  return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) );
764  }
765 
783  OStringBuffer insert(sal_Int32 offset, float f)
784  {
786  return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) );
787  }
788 
806  OStringBuffer & insert(sal_Int32 offset, double d)
807  {
809  return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) );
810  }
811 
824  OStringBuffer & remove( sal_Int32 start, sal_Int32 len )
825  {
826  rtl_stringbuffer_remove( &pData, start, len );
827  return *this;
828  }
829 
830 private:
834  rtl_String * pData;
835 
839  sal_Int32 nCapacity;
840 };
841 
842 }
843 
844 #ifdef RTL_STRING_UNITTEST
845 namespace rtl
846 {
847 typedef rtlunittest::OStringBuffer OStringBuffer;
848 }
849 #undef RTL_STRING_CONST_FUNCTION
850 #endif
851 
852 #endif /* __cplusplus */
853 #endif /* _RTL_STRBUF_HXX_ */
854 
855 
856 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */