UDK 3.2.7 C/C++ API Reference
rtl/strbuf.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*************************************************************************
00003  *
00004  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00005  *
00006  * Copyright 2000, 2010 Oracle and/or its affiliates.
00007  *
00008  * OpenOffice.org - a multi-platform office productivity suite
00009  *
00010  * This file is part of OpenOffice.org.
00011  *
00012  * OpenOffice.org is free software: you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 3
00014  * only, as published by the Free Software Foundation.
00015  *
00016  * OpenOffice.org is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License version 3 for more details
00020  * (a copy is included in the LICENSE file that accompanied this code).
00021  *
00022  * You should have received a copy of the GNU Lesser General Public License
00023  * version 3 along with OpenOffice.org.  If not, see
00024  * <http://www.openoffice.org/license.html>
00025  * for a copy of the LGPLv3 License.
00026  *
00027  ************************************************************************/
00028 
00029 #ifndef _RTL_STRBUF_HXX_
00030 #define _RTL_STRBUF_HXX_
00031 
00032 #include "sal/config.h"
00033 
00034 #include <cassert>
00035 
00036 #include <rtl/strbuf.h>
00037 #include <rtl/string.hxx>
00038 #include <rtl/stringutils.hxx>
00039 
00040 #ifdef __cplusplus
00041 
00042 // The unittest uses slightly different code to help check that the proper
00043 // calls are made. The class is put into a different namespace to make
00044 // sure the compiler generates a different (if generating also non-inline)
00045 // copy of the function and does not merge them together. The class
00046 // is "brought" into the proper rtl namespace by a typedef below.
00047 #ifdef RTL_STRING_UNITTEST
00048 #define rtl rtlunittest
00049 #endif
00050 
00051 namespace rtl
00052 {
00053 
00054 #ifdef RTL_STRING_UNITTEST
00055 #undef rtl
00056 // helper macro to make functions appear more readable
00057 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
00058 #else
00059 #define RTL_STRING_CONST_FUNCTION
00060 #endif
00061 
00100 class OStringBuffer
00101 {
00102 public:
00107     OStringBuffer()
00108         : pData(NULL)
00109         , nCapacity( 16 )
00110     {
00111         rtl_string_new_WithLength( &pData, nCapacity );
00112     }
00113 
00120     OStringBuffer( const OStringBuffer & value )
00121         : pData(NULL)
00122         , nCapacity( value.nCapacity )
00123     {
00124         rtl_stringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
00125     }
00126 
00133     explicit OStringBuffer(int length)
00134         : pData(NULL)
00135         , nCapacity( length )
00136     {
00137         rtl_string_new_WithLength( &pData, length );
00138     }
00139 
00150     OStringBuffer(OString value)
00151         : pData(NULL)
00152         , nCapacity( value.getLength() + 16 )
00153     {
00154         rtl_stringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
00155     }
00156 
00161 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN // see the OString ctors
00162     OStringBuffer( const char* value )
00163         : pData(NULL)
00164     {
00165         sal_Int32 length = rtl_str_getLength( value );
00166         nCapacity = length + 16;
00167         rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
00168     }
00169 #else
00170     template< typename T >
00171     OStringBuffer( const T& value, typename internal::CharPtrDetector< T, internal::Dummy >::Type = internal::Dummy())
00172         : pData(NULL)
00173     {
00174         sal_Int32 length = rtl_str_getLength( value );
00175         nCapacity = length + 16;
00176         rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
00177     }
00178 
00179     template< typename T >
00180     OStringBuffer( T& value, typename internal::NonConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy())
00181         : pData(NULL)
00182     {
00183         sal_Int32 length = rtl_str_getLength( value );
00184         nCapacity = length + 16;
00185         rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
00186     }
00187 
00199     template< typename T >
00200     OStringBuffer( T& literal, typename internal::ConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy())
00201         : pData(NULL)
00202         , nCapacity( internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
00203     {
00204         rtl_string_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 16 );
00205 #ifdef RTL_STRING_UNITTEST
00206         rtl_string_unittest_const_literal = true;
00207 #endif
00208     }
00209 #endif // HAVE_SFINAE_ANONYMOUS_BROKEN
00210 
00223     OStringBuffer(const sal_Char * value, sal_Int32 length)
00224         : pData(NULL)
00225         , nCapacity( length + 16 )
00226     {
00227         rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
00228     }
00229 
00232     OStringBuffer& operator = ( const OStringBuffer& value )
00233     {
00234         if (this != &value)
00235         {
00236             rtl_stringbuffer_newFromStringBuffer(&pData,
00237                                                   value.nCapacity,
00238                                                   value.pData);
00239             nCapacity = value.nCapacity;
00240         }
00241         return *this;
00242     }
00243 
00247     ~OStringBuffer()
00248     {
00249         rtl_string_release( pData );
00250     }
00251 
00260     OString makeStringAndClear()
00261     {
00262         OString aRet( pData );
00263         rtl_string_new(&pData);
00264         nCapacity = 0;
00265         return aRet;
00266     }
00267 
00273     sal_Int32 getLength() const
00274     {
00275         return pData->length;
00276     }
00277 
00288     sal_Int32 getCapacity() const
00289     {
00290         return nCapacity;
00291     }
00292 
00304     void ensureCapacity(sal_Int32 minimumCapacity)
00305     {
00306         rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
00307     }
00308 
00327     void setLength(sal_Int32 newLength)
00328     {
00329         assert(newLength >= 0);
00330         // Avoid modifications if pData points to const empty string:
00331         if( newLength != pData->length )
00332         {
00333             if( newLength > nCapacity )
00334                 rtl_stringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
00335             else
00336                 pData->buffer[newLength] = '\0';
00337             pData->length = newLength;
00338         }
00339     }
00340 
00354     SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
00355     sal_Char charAt( sal_Int32 index )
00356     {
00357         assert(index >= 0 && index < pData->length);
00358         return pData->buffer[ index ];
00359     }
00360 
00371     SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
00372     OStringBuffer & setCharAt(sal_Int32 index, sal_Char ch)
00373     {
00374         assert(index >= 0 && index < pData->length);
00375         pData->buffer[ index ] = ch;
00376         return *this;
00377     }
00378 
00382     const sal_Char* getStr() const { return pData->buffer; }
00383 
00393     sal_Char & operator [](sal_Int32 index) { return pData->buffer[index]; }
00394 
00399     const OString toString() const
00400     {
00401         return OString(pData->buffer, pData->length);
00402     }
00403 
00414     OStringBuffer & append(const OString &str)
00415     {
00416         return append( str.getStr(), str.getLength() );
00417     }
00418 
00430 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
00431     OStringBuffer & append( const sal_Char * str )
00432     {
00433         return append( str, rtl_str_getLength( str ) );
00434     }
00435 #else
00436     template< typename T >
00437     typename internal::CharPtrDetector< T, OStringBuffer& >::Type append( const T& str )
00438     {
00439         return append( str, rtl_str_getLength( str ) );
00440     }
00441 
00442     template< typename T >
00443     typename internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type append( T& str )
00444     {
00445         return append( str, rtl_str_getLength( str ) );
00446     }
00447 
00453     template< typename T >
00454     typename internal::ConstCharArrayDetector< T, OStringBuffer& >::Type append( T& literal )
00455     {
00456         RTL_STRING_CONST_FUNCTION
00457         rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
00458         return *this;
00459     }
00460 #endif
00461 
00475     OStringBuffer & append( const sal_Char * str, sal_Int32 len)
00476     {
00477         // insert behind the last character
00478         rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
00479         return *this;
00480     }
00481 
00493     OStringBuffer & append(sal_Bool b)
00494     {
00495         sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
00496         return append( sz, rtl_str_valueOfBoolean( sz, b ) );
00497     }
00498 
00509     OStringBuffer & append(sal_Char c)
00510     {
00511         return append( &c, 1 );
00512     }
00513 
00525     OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
00526     {
00527         sal_Char sz[RTL_STR_MAX_VALUEOFINT32];
00528         return append( sz, rtl_str_valueOfInt32( sz, i, radix ) );
00529     }
00530 
00542     OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
00543     {
00544         sal_Char sz[RTL_STR_MAX_VALUEOFINT64];
00545         return append( sz, rtl_str_valueOfInt64( sz, l, radix ) );
00546     }
00547 
00559     OStringBuffer & append(float f)
00560     {
00561         sal_Char sz[RTL_STR_MAX_VALUEOFFLOAT];
00562         return append( sz, rtl_str_valueOfFloat( sz, f ) );
00563     }
00564 
00576     OStringBuffer & append(double d)
00577     {
00578         sal_Char sz[RTL_STR_MAX_VALUEOFDOUBLE];
00579         return append( sz, rtl_str_valueOfDouble( sz, d ) );
00580     }
00581 
00597     OStringBuffer & insert(sal_Int32 offset, const OString & str)
00598     {
00599         return insert( offset, str.getStr(), str.getLength() );
00600     }
00601 
00619 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
00620     OStringBuffer & insert( sal_Int32 offset, const sal_Char * str )
00621     {
00622         return insert( offset, str, rtl_str_getLength( str ) );
00623     }
00624 #else
00625     template< typename T >
00626     typename internal::CharPtrDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, const T& str )
00627     {
00628         return insert( offset, str, rtl_str_getLength( str ) );
00629     }
00630 
00631     template< typename T >
00632     typename internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& str )
00633     {
00634         return insert( offset, str, rtl_str_getLength( str ) );
00635     }
00636 
00642     template< typename T >
00643     typename internal::ConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
00644     {
00645         RTL_STRING_CONST_FUNCTION
00646         rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
00647         return *this;
00648     }
00649 #endif
00650 
00669     OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len)
00670     {
00671         // insert behind the last character
00672         rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
00673         return *this;
00674     }
00675 
00693     OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
00694     {
00695         sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
00696         return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
00697     }
00698 
00715     OStringBuffer & insert(sal_Int32 offset, sal_Char c)
00716     {
00717         return insert( offset, &c, 1 );
00718     }
00719 
00737     OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
00738     {
00739         sal_Char sz[RTL_STR_MAX_VALUEOFINT32];
00740         return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) );
00741     }
00742 
00760     OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
00761     {
00762         sal_Char sz[RTL_STR_MAX_VALUEOFINT64];
00763         return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) );
00764     }
00765 
00783     OStringBuffer insert(sal_Int32 offset, float f)
00784     {
00785         sal_Char sz[RTL_STR_MAX_VALUEOFFLOAT];
00786         return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) );
00787     }
00788 
00806     OStringBuffer & insert(sal_Int32 offset, double d)
00807     {
00808         sal_Char sz[RTL_STR_MAX_VALUEOFDOUBLE];
00809         return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) );
00810     }
00811 
00824     OStringBuffer & remove( sal_Int32 start, sal_Int32 len )
00825     {
00826         rtl_stringbuffer_remove( &pData, start, len );
00827         return *this;
00828     }
00829 
00830 private:
00834     rtl_String * pData;
00835 
00839     sal_Int32       nCapacity;
00840 };
00841 
00842 }
00843 
00844 #ifdef RTL_STRING_UNITTEST
00845 namespace rtl
00846 {
00847 typedef rtlunittest::OStringBuffer OStringBuffer;
00848 }
00849 #undef RTL_STRING_CONST_FUNCTION
00850 #endif
00851 
00852 #endif  /* __cplusplus */
00853 #endif  /* _RTL_STRBUF_HXX_ */
00854 
00855 
00856 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines