String.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (c) 2003-2006 by Autodesk, Inc.
00003 //
00004 //  By using this code, you are agreeing to the terms and conditions of
00005 //  the License Agreement included in the documentation for this code.
00006 //
00007 //  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED,
00008 //  AS TO THE CORRECTNESS OF THIS CODE OR ANY DERIVATIVE
00009 //  WORKS WHICH INCORPORATE IT.
00010 //
00011 //  AUTODESK PROVIDES THE CODE ON AN "AS-IS" BASIS
00012 //  AND EXPLICITLY DISCLAIMS ANY LIABILITY, INCLUDING
00013 //  CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS,
00014 //  OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
00015 //
00016 //  Use, duplication, or disclosure by the U.S. Government is subject to
00017 //  restrictions set forth in FAR 52.227-19 (Commercial Computer Software
00018 //  Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical
00019 //  Data and Computer Software), as applicable.
00020 //
00021 
00022 #ifndef _DWFCORE_STRING_H
00023 #define _DWFCORE_STRING_H
00024 
00029 
00042 
00043 
00044 #include "dwfcore/Core.h"
00045 #include "dwfcore/STL.h"
00046 #include "dwfcore/Exception.h"
00047 #include "dwfcore/Pointer.h"
00048 
00049 
00050 
00051 #ifndef DWFCORE_STRING_PREFER_HEAP_LIMIT_IN_BYTES
00061 #define DWFCORE_STRING_PREFER_HEAP_LIMIT_IN_BYTES       128
00062 #define _DWFCORE_STRING_PREFER_HEAP_LIMIT_IN_WCHARS     (DWFCORE_STRING_PREFER_HEAP_LIMIT_IN_BYTES>>(sizeof(wchar_t)>>1))
00063 #endif
00064 
00065 
00066 
00067 namespace DWFCore
00068 {
00069 
00070 //
00071 // fwd decl
00072 //
00073 class DWFThreadMutex;
00074 
00075 
00093 class DWFString : virtual public DWFCoreMemory
00094 {
00095 
00096 public:
00097 
00113     _DWFCORE_API
00114     static size_t EncodeUTF8( const UCS2_char_t*    zUCS2String,
00115                               size_t                nUCS2StringBytes,
00116                               ASCII_char_t*         pUTF8Buffer,
00117                               size_t                nUTF8BufferBytes )
00118         throw( DWFOverflowException );
00119 
00135     _DWFCORE_API
00136     static size_t EncodeUTF8( const UCS4_char_t*    zUCS4String,
00137                               size_t                nUCS4StringBytes,
00138                               ASCII_char_t*         pUTF8Buffer,
00139                               size_t                nUTF8BufferBytes )
00140         throw( DWFOverflowException );
00141 
00142 
00156     _DWFCORE_API
00157     static size_t DecodeUTF8( const ASCII_char_t*   zUTF8String,
00158                               size_t                nUTF8StringBytes,
00159                               UCS2_char_t*          pUCS2Buffer,
00160                               size_t                nUCS2BufferBytes )
00161         throw( DWFOverflowException );
00162 
00176     _DWFCORE_API
00177     static size_t DecodeUTF8( const ASCII_char_t*   zUTF8String,
00178                               size_t                nUTF8StringBytes,
00179                               UCS4_char_t*          pUCS4Buffer,
00180                               size_t                nUCS4BufferBytes )
00181         throw( DWFOverflowException );
00182 
00183 
00198     _DWFCORE_API
00199     static size_t EncodeXML( const ASCII_char_t*    zASCIIString,
00200                              size_t                 nASCIIStringBytes,
00201                              ASCII_char_t*          pXMLBuffer,
00202                              size_t                 nXMLBufferBytes )
00203         throw( DWFException );
00204 
00238     _DWFCORE_API
00239     static size_t EncodeXML( const wchar_t*         zWideString,
00240                              size_t                 nWideStringBytes,
00241                              wchar_t*               pXMLBuffer,
00242                              size_t                 nXMLBufferBytes,
00243                              bool                   bHTMLEncodeNonASCII = true )
00244         throw( DWFException );
00245 
00260     _DWFCORE_API
00261     static size_t DecodeXML( const ASCII_char_t*   zXMLString,
00262                              size_t                nXMLStringBytes,
00263                              ASCII_char_t*         pASCIIBuffer,
00264                              size_t                nASCIIBufferBytes )
00265         throw( DWFException );
00266 
00281     _DWFCORE_API
00282     static size_t DecodeXML( const wchar_t*        zXMLString,
00283                              size_t                nXMLStringBytes,
00284                              wchar_t*              pWideStringBuffer,
00285                              size_t                nWideStringBufferBytes )
00286         throw( DWFException );
00287 
00312     _DWFCORE_API
00313     static size_t EncodeBase64( const void*         pRawBuffer,
00314                                 size_t              nRawBufferBytes,
00315                                 void*               pOutBuffer,
00316                                 size_t              nOutBufferBytes,
00317                                 bool                bRFC1521Compliant = true )
00318         throw( DWFOverflowException );
00319 
00320 
00341     _DWFCORE_API
00342     static size_t DecodeBase64( const void*             pRawBuffer,
00343                                 size_t                  nRawBufferBytes,
00344                                 void*                   pOutBuffer,
00345                                 size_t                  nOutBufferBytes,
00346                                 bool                    bRFC1521Compliant = true)
00347         throw( DWFOverflowException );
00348 
00360     _DWFCORE_API
00361     static double StringToDouble( const char* zString )
00362         throw( DWFException );
00363 
00375     _DWFCORE_API
00376     static double StringToDouble( const wchar_t* zString )
00377         throw( DWFException );
00378 
00391     _DWFCORE_API
00392     static DWFString DoubleToString( double dValue,short nPrecision,short nWidth=-1)
00393         throw( DWFException );
00394 
00408     _DWFCORE_API
00409     static void DoubleToString( wchar_t* pBuffer, size_t nBufferChars, double dValue,short nPrecision,short nWidth=-1)
00410         throw( DWFException );
00411 
00425     _DWFCORE_API
00426     static void DoubleToString( char* pBuffer, size_t nBufferChars, double dValue,short nPrecision,short nWidth=-1)
00427         throw( DWFException );
00428 
00440     _DWFCORE_API
00441     static wchar_t* RepairDecimalSeparators( wchar_t* zString )
00442         throw( DWFException );
00443 
00455     _DWFCORE_API
00456     static char* RepairDecimalSeparators( char* zString )
00457         throw( DWFException );
00458 
00472     _DWFCORE_API
00473     static off_t Find( const wchar_t*   zString,
00474                        const wchar_t    cFind,
00475                        off_t            iOffset = 0,
00476                        bool             bReverse = false )
00477         throw( DWFInvalidArgumentException );
00478 
00492     _DWFCORE_API
00493     static off_t Find( const wchar_t*   zString,
00494                        const wchar_t*   zFind,
00495                        off_t            iOffset = 0,
00496                        bool             bReverse = false )
00497         throw( DWFInvalidArgumentException );
00498 
00507     _DWFCORE_API
00508     static off_t FindFirst( const wchar_t*  zString,
00509                             const wchar_t   cFind )
00510         throw();
00511 
00520     _DWFCORE_API
00521     static off_t FindLast( const wchar_t*   zString,
00522                            const wchar_t    cFind )
00523         throw();
00524 
00525 #ifndef DWFCORE_STRING_DISABLE_FIXED_ASCII
00526 
00547     _DWFCORE_API
00548     static DWFString* BuildFixedASCIIString( const char* zASCIIString )
00549         throw( DWFException );
00550 
00551 #endif
00552 
00553 public:
00554 
00560     _DWFCORE_API 
00561     ~DWFString()
00562         throw();
00563 
00569     _DWFCORE_API 
00570     DWFString()
00571         throw();
00572 
00584     _DWFCORE_API
00585     DWFString( const char*      pBuffer,
00586                size_t           nBufferBytes )
00587         throw( DWFException );
00588 
00599     _DWFCORE_API
00600     DWFString( const char*      zASCIIString )
00601         throw( DWFException );
00602 
00612     _DWFCORE_API
00613     DWFString( const wchar_t*   pBuffer,
00614                size_t           nBufferBytes )
00615         throw( DWFException );
00616 
00625     _DWFCORE_API
00626     DWFString( const wchar_t*   zWideString )
00627         throw( DWFException );
00628 
00642     _DWFCORE_API
00643     DWFString( size_t nCharsHint )
00644         throw( DWFException );
00645 
00652     _DWFCORE_API 
00653     DWFString( const DWFString& rDWFString )
00654         throw( DWFException );
00655 
00662     _DWFCORE_API
00663     DWFString& operator=( const DWFString& rDWFString )
00664         throw( DWFException );
00665 
00672     _DWFCORE_API
00673     void assign( const DWFString& rDWFString )
00674         throw( DWFException );
00675 
00685     _DWFCORE_API
00686     void assign( const char*    pBuffer,
00687                          size_t         nBufferBytes )
00688         throw( DWFException );
00689 
00698     _DWFCORE_API
00699     void assign( const char*    zASCIIString )
00700         throw( DWFException );
00701 
00709     _DWFCORE_API
00710     void assign( const wchar_t* pBuffer,
00711                          size_t         nBufferBytes )
00712         throw( DWFException );
00713 
00720     _DWFCORE_API
00721     void assign( const wchar_t* zWideString )
00722         throw( DWFException );
00723 
00731     _DWFCORE_API
00732     bool operator==( const DWFString& rDWFString ) const
00733         throw();
00734 
00742     _DWFCORE_API
00743     bool operator==( const wchar_t* zWideString ) const
00744         throw();
00745 
00753     _DWFCORE_API
00754     bool operator!=( const DWFString& rDWFString ) const
00755         throw();
00756 
00764     _DWFCORE_API
00765     bool operator!=( const wchar_t* zWideString ) const
00766         throw();
00767 
00775     _DWFCORE_API
00776     bool operator<( const DWFString& rDWFString ) const
00777         throw();
00778 
00786     _DWFCORE_API
00787     bool operator<( const wchar_t* zWideString ) const
00788         throw();
00789 
00797     _DWFCORE_API
00798     bool operator>( const DWFString& rDWFString ) const
00799         throw();
00800 
00808     _DWFCORE_API
00809     bool operator>( const wchar_t* zWideString ) const
00810         throw();
00811 
00817     _DWFCORE_API
00818     void destroy()
00819         throw( DWFException );
00820 
00827     _DWFCORE_API
00828     void append( const DWFString& rDWFString )
00829         throw( DWFException );
00830 
00840     _DWFCORE_API
00841     void append( const char*    pBuffer,
00842                  size_t         nBufferBytes )
00843         throw( DWFException );
00844 
00853     _DWFCORE_API
00854     void append( const char*    zASCIIString )
00855         throw( DWFException );
00856 
00864     _DWFCORE_API
00865     void append( const wchar_t* pBuffer,
00866                  size_t         nBufferBytes )
00867         throw( DWFException );
00868 
00875     _DWFCORE_API
00876     void append( const wchar_t* zWideString )
00877         throw( DWFException );
00878 
00884     _DWFCORE_API
00885     size_t bytes() const
00886         throw();
00887 
00893     _DWFCORE_API
00894     size_t chars() const
00895         throw();
00896 
00910     _DWFCORE_API
00911     size_t getUTF8( char*   pBuffer,
00912                     size_t  nBufferBytes ) const
00913         throw( DWFException );
00914 
00928     _DWFCORE_API
00929     size_t getUTF8( char** ppBuffer ) const
00930         throw( DWFException );
00931 
00939     _DWFCORE_API
00940     operator const wchar_t*() const
00941         throw( DWFException );
00942 
00955     _DWFCORE_API
00956     off_t find( const wchar_t cFind,
00957                 off_t         iOffset = 0,
00958                 bool          bReverse = false ) const
00959         throw( DWFException );
00960 
00973     _DWFCORE_API
00974     off_t find( const wchar_t* zFind,
00975                 off_t          iOffset = 0,
00976                 bool           bReverse = false ) const
00977         throw( DWFException );
00978 
00986     _DWFCORE_API
00987     off_t findFirst( const wchar_t cFind ) const
00988         throw( DWFException );
00989 
00997     _DWFCORE_API
00998     off_t findLast( const wchar_t cFind ) const
00999         throw( DWFException );
01000 
01010     _DWFCORE_API
01011     DWFString substring( size_t iPos,
01012                          size_t iLen = (size_t) -1 ) const
01013          throw( DWFException );
01014 
01015 private:
01016 
01017     void _store( const void*    pBuffer,
01018                  size_t         nBufferBytes,
01019                  bool           bDecodeUTF8 = false,
01020                  bool           bFixedASCII = false )
01021         throw( DWFException );
01022 
01023     void _append( const void*   pBuffer,
01024                   size_t        nBufferBytes,
01025                   bool          bDecodeUTF8 = false )
01026         throw( DWFException );
01027 
01028     _DWFCORE_API
01029         void _affix()
01030         throw( DWFException );
01031 
01032 private:
01033 
01034     wchar_t*    _pHeapBuffer;
01035     size_t      _nBufferChars;
01036     size_t      _nDataChars;
01037 
01038 #ifndef DWFCORE_STRING_DISABLE_FIXED_ASCII
01039 
01040     bool        _bDataIsFixedASCII;
01041 
01042 #endif
01043 
01044     bool        _bRetainHeapBuffer;
01045 
01046         typedef struct __tBlock
01047         {
01048                 struct __tBlock* _next;
01049                 wchar_t* _buffer;
01050                 size_t size;
01051         public:
01052         __tBlock()
01053             : _next( NULL )
01054             , _buffer( NULL )
01055                         , size(0)
01056         {;}
01057         } _tBlock;
01058         _tBlock* _pHead, *_pTail;
01059         size_t _size;
01060 };
01061 
01062 //
01063 //
01064 //
01065 
01066 _DWFCORE_API
01067 inline
01068 size_t
01069 DWFString::bytes()
01070 const
01071 throw()
01072 {
01073         //
01074         // Need to resolve any append fragments before examining character counts.
01075         //
01076         const_cast<DWFString*>(this)->_affix();
01077 
01078 #ifndef DWFCORE_STRING_DISABLE_FIXED_ASCII
01079     if (_bDataIsFixedASCII)
01080     {
01081         return _nDataChars;
01082     }
01083     else
01084 #endif
01085     {
01086         return (_nDataChars * sizeof(wchar_t));
01087     }
01088 }
01089 
01090 _DWFCORE_API
01091 inline
01092 size_t
01093 DWFString::chars()
01094 const
01095 throw()
01096 {
01097         //
01098         // Need to resolve any append fragments before examining character counts.
01099         //
01100         const_cast<DWFString*>(this)->_affix();
01101 
01102     return _nDataChars;
01103 }
01104 
01105 _DWFCORE_API
01106 inline
01107 DWFString::operator const wchar_t*()
01108 const
01109 throw( DWFException )
01110 {
01111         //
01112         // Protect against someone calling us with a null pointer.
01113         //
01114     if(this==NULL)
01115     {
01116         return NULL;
01117     }
01118 
01119         //
01120         // Need to resolve any append fragments before checking stack/heap.
01121         //
01122     DWFString* pThis = const_cast<DWFString*>(this);
01123     pThis->_affix();
01124 
01125 #ifndef DWFCORE_STRING_DISABLE_FIXED_ASCII
01126     if (_bDataIsFixedASCII)
01127     {
01128         _DWFCORE_THROW( DWFNotImplementedException, /*NOXLATE*/L"This method is not available for fixed ASCII strings." ); 
01129     }
01130 #endif
01131 
01132         //
01133         // no data
01134         //
01135     return _pHeapBuffer;
01136 }
01137 
01138 class DWFStringTable
01139 {
01140 public:
01147         _DWFCORE_API static DWFStringTable& Instance();
01148 
01152     _DWFCORE_API static void Purge();
01153 
01161         _DWFCORE_API const DWFString* insert( const DWFString& zText );
01162 
01163 private:
01164     struct _Less
01165         {
01166                 bool operator()(const DWFString* lhs, const DWFString* rhs) const
01167                 {
01168                         return (*lhs) < (*rhs);
01169                 }
01170         };
01171 
01172         DWFStringTable();
01173         ~DWFStringTable();
01174 
01175 private:
01176         DWFStringTable (const DWFStringTable&);
01177         DWFStringTable& operator=( const DWFStringTable&);                      
01178 
01179 private:
01180         std::deque<DWFString> _oTable;
01181 
01182         typedef std::set< const DWFString*, _Less >  _tIndex;
01183         _tIndex              _oIndex;
01184 
01185     DWFThreadMutex*         _pMutex;
01186 
01187 };
01188 
01189 }
01190 
01191 #endif

Generated on Tue Jan 6 22:39:29 2009 for Autodesk DWF Core Library by  doxygen 1.4.5