FBX SDK Reference Guide: kstring.h Source File
Go to the documentation of this file.
00001 
00004 #ifndef _FBXSDK_KSTRING_H_
00005 #define _FBXSDK_KSTRING_H_
00006 
00007 /**************************************************************************************
00008 
00009  Copyright  2001 - 2008 Autodesk, Inc. and/or its licensors.
00010  All Rights Reserved.
00011 
00012  The coded instructions, statements, computer programs, and/or related material 
00013  (collectively the "Data") in these files contain unpublished information 
00014  proprietary to Autodesk, Inc. and/or its licensors, which is protected by 
00015  Canada and United States of America federal copyright law and by international 
00016  treaties. 
00017  
00018  The Data may not be disclosed or distributed to third parties, in whole or in
00019  part, without the prior written consent of Autodesk, Inc. ("Autodesk").
00020 
00021  THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY.
00022  ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO
00023  WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR ARISING
00024  BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES OF TITLE, 
00025  NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR USE. 
00026  WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT WARRANT THAT THE OPERATION
00027  OF THE DATA WILL BE UNINTERRUPTED OR ERROR FREE. 
00028  
00029  IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS
00030  OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR EXPENSES
00031  OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE DAMAGES OR OTHER
00032  SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS OF PROFITS, REVENUE
00033  OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR DAMAGES OF ANY KIND),
00034  HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF LIABILITY, WHETHER DERIVED
00035  FROM CONTRACT, TORT (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE), OR OTHERWISE,
00036  ARISING OUT OF OR RELATING TO THE DATA OR ITS USE OR ANY OTHER PERFORMANCE,
00037  WHETHER OR NOT AUTODESK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS
00038  OR DAMAGE. 
00039 
00040 **************************************************************************************/
00041 #include <string.h>
00042 
00043 // FBX includes
00044 #include <kbaselib_h.h>
00045 #include <klib/kscopedptr.h>
00046 
00047 
00048 #if defined(KARCH_ENV_WIN32)
00049 
00050 // Conversions between WideChar, UTF8 and ANSI
00051     #define UTF8_to_WIDECHAR( inUTF8,outWideChar ) \
00052         wchar_t outWideChar[1024]; \
00053         MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, outWideChar, 1024 );
00054 
00055     #define WIDECHAR_to_UTF8( inWideChar,outUTF8 ) \
00056         char outUTF8[1024]; \
00057         WideCharToMultiByte( CP_UTF8,0,inWideChar,-1,outUTF8,1024,NULL,NULL);
00058 
00059     #define WIDECHAR_to_ANSI( inWideChar,outANSI ) \
00060         char outANSI[1024]; \
00061         WideCharToMultiByte( CP_ACP,0,inWideChar,-1,outANSI,1024,NULL,NULL);
00062 
00063     #define ANSI_to_WIDECHAR( inANSI,outWideChar ) \
00064         wchar_t outWideChar[1024]; \
00065         MultiByteToWideChar( CP_ACP, 0, inANSI, -1, outWideChar, 1024 );
00066 
00067     #define ANSI_to_UTF8( inANSI,outUTF8 ) { \
00068         wchar_t tempWideChar[1024]; \
00069         MultiByteToWideChar( CP_ACP, 0, inANSI, -1, tempWideChar, 1024); \
00070         WideCharToMultiByte( CP_UTF8, 0, tempWideChar, -1, outUTF8, 1024, NULL, NULL); \
00071     }
00072 
00073     #define UTF8_to_ANSI( inUTF8,outANSI, dummy, dummy1 ) { \
00074         wchar_t tempWideChar[1024]; \
00075         MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, tempWideChar, 1024); \
00076         WideCharToMultiByte( CP_ACP, 0, tempWideChar, -1, outANSI, 1024, NULL, NULL); \
00077     }
00078 #endif
00079 
00080 
00081 
00082 #include <kbaselib_nsbegin.h>
00083 class KMemoryAllocator;
00084 
00085 #if !defined(KARCH_ENV_WIN32)
00086     KBASELIB_DLL void UTF8_to_ANSI( const char* pSrc, char* pDest, size_t pDestSize, char pSubstitute = '.' );
00087 #endif
00088 
00089 //  #define UseKStringStatic
00090     #ifdef UseKStringStatic
00091         #define KStringStaticSize 64
00092     #endif
00093 
00097     class KBASELIB_DLL KString
00098     {
00099         public:
00104 
00105                 static KString      *Create();
00107                 static KString      *Create(KString *pString);
00109                 void                Destroy();
00111                 static KString* DestroyIfEmpty(KString *pString);
00113                 static KString* StringOrEmpty(KString *pString);
00115 
00116         public:
00121 
00123                 KString();
00124 
00126                 KString(const KString& pStr);
00127 
00129                 KString(const char* pStr);
00130 
00132                 KString(char pChar, size_t pNbRepeat = 1);
00133 
00135                 KString(const char* pCharPtr, size_t pLength);
00136 
00138                 KString(const int pValue);
00139 
00141                 KString(const float pValue);
00142 
00144                 KString(const double pValue);
00145 
00147                 ~KString();
00149 
00154 
00155                 inline bool IsOK() const;
00156 
00158                 KString &Invalidate();
00159 
00161                 inline size_t GetLen() const;
00162 
00164                 inline bool IsEmpty() const;
00165 
00167                 KString  &Empty();
00169 
00174 
00176                 char& operator[](int pIndex);
00177 
00179                 char operator[](int pIndex) const;
00180 
00182                 inline operator const char*() const;
00183 
00185                 inline char* Buffer();
00186 
00188                 inline const char* Buffer()const;
00189 
00191 
00196 
00198                 const KString& operator=(const KString& pStr);
00199 
00201                 const KString& operator=(char pChar);
00202 
00204                 const KString& operator=(const char* pStr);
00205 
00207                 const KString& operator=(int  pValue);
00208 
00210                 const KString& operator=(float pValue);
00211 
00213                 const KString& operator=(double pValue);
00214 
00216                 const KString& Copy(size_t pLen, const char* pStr);
00217 
00218                 // Swap the contents of two KString objects; no allocation is performed.
00219                 void Swap(KString&);
00221 
00226 
00228                 const KString& Append(const char* pStr);
00229 
00231                 const KString& AppendN(const char* pStr, size_t pLength);
00232 
00234                 const KString& operator+=(const KString& pKStr);
00235 
00237                 const KString& operator+=(char pChar);
00238 
00240                 const KString& operator+=(const char* pStr);
00241 
00243                 const KString& operator+=(int pValue);
00244 
00246                 const KString& operator+=(float pValue);
00247 
00249                 friend KBASELIB_DLL KString operator+(const KString& pKStr1, const KString& pKStr2);
00250 
00252                 friend KBASELIB_DLL KString operator+(const KString& pKStr, char pChar);
00253 
00255                 friend KBASELIB_DLL KString operator+(const KString& pKStr, const char* pStr);
00256 
00258                 friend KBASELIB_DLL KString operator+(const KString& pKStr, int pValue);
00259 
00261                 friend KBASELIB_DLL KString operator+(const KString& pKStr, float pValue);
00263 
00268 
00270                 int Compare(const char * pStr) const;
00271 
00273                 int CompareNoCase( const char * pStr ) const;
00274 
00276                 bool operator==(const KString &pStr) const;
00277 
00279                 bool operator!=(const KString &pStr) const;
00280 
00282                 bool operator< (const KString &pStr) const;
00283 
00285                 bool operator<=(const KString &pStr) const;
00286 
00288                 bool operator>=(const KString &pStr) const;
00289 
00291                 bool operator> (const KString &pStr) const;
00292 
00294                 bool operator==(const char *pStr) const;
00295 
00297                 bool operator!=(const char *pStr) const;
00298 
00300                 bool operator< (const char *pStr) const;
00301 
00303                 bool operator<=(const char *pStr) const;
00304 
00306                 bool operator>=(const char *pStr) const;
00307 
00309                 bool operator> (const char *pStr) const;
00311 
00316 
00318                 KString Mid(size_t pFirst, size_t pCount) const;
00319 
00321                 KString Mid(size_t pFirst) const;
00322 
00324                 KString Left(size_t pCount) const;
00325 
00327                 KString Right(size_t pCount) const;
00328 
00330 
00335 
00341                 enum PaddingType 
00342                 { 
00343                     eRight,
00344                     eLeft, 
00345                     eBoth
00346                 };
00347 
00349                 KString Pad(PaddingType pPadding, size_t pLen, char pCar = ' ') const;
00350 
00352                 KString UnPad(PaddingType pPadding) const;
00353 
00355 
00360 
00361                 KString Upper() const;
00362 
00364                 KString Lower() const;
00365 
00367                 KString Reverse() const;
00368 
00370                 KString ConvertToUnix() const;
00371 
00373                 KString ConvertToWindows() const;
00375 
00383                 int Find(char pChar, size_t pStartPosition = 0) const;
00384 
00388                 int Find(const char* pStrSub, size_t pStartPosition = 0) const;
00389 
00393                 int ReverseFind(char pChar) const;
00394 
00398                 int FindOneOf(const char * pStrCharSet, size_t pStartPosition = 0) const;
00399 
00403                 bool FindAndReplace(const char* pFind, const char* pReplaceBy, size_t pStartPosition = 0);
00404 
00408                 bool ReplaceAll( char pFind, char pReplaceBy );
00409 
00411 
00416 
00417                 int GetTokenCount(const char* pSpans) const;
00418 
00420                 KString GetToken(int pTokenIndex, const char* pSpans) const;
00422 
00427             public:
00428                 static KMemoryAllocator* AllocatorGet();
00429                 static void AllocatorPurge();
00430                 static void  AllocatorRelease();
00432 
00434         //  WARNING!
00435         //  Anything beyond these lines may not be documented accurately and is
00436         //  subject to change without notice.
00438         #ifndef DOXYGEN_SHOULD_SKIP_THIS
00439         private:
00440             void Init();
00441             void FreeBuffer();
00442             void FreeBuffer(char *&pOldData);
00443 
00444             bool AllocCopy(KString& pDest, size_t pCopyLen, size_t pCopyIndex) const;
00445             bool AllocBuffer(size_t pLen);
00446             bool AllocBuffer(size_t pLen, char *&pOldData);
00447 
00448             bool AssignCopy(size_t pSrcLen, const char * pSrcData);
00449             bool ConcatCopy(size_t pSrc1Len, const char * pSrc1Data,
00450                             size_t pSrc2Len, const char * pSrc2Data);
00451             bool ConcatInPlace(size_t pSrcLen, const char * pSrcData);
00452 
00453             bool IsIn(char pChar, const char* pString) const;
00454 
00455             static KScopedPtr<KMemoryAllocator> mMemoryPool;
00456 
00457         // Lengths/sizes in characters.
00458         // Note: an extra character is always allocated.
00459         private:
00460             char*       mData; // Actual string (zero terminated).
00461             size_t      mDataLen; // Does not include terminating 0.
00462             size_t      mAllocLen; // Does not include terminating 0.
00463             #ifdef UseKStringStatic
00464                 char    mStaticData[KStringStaticSize]; // Actual static string (zero terminated) used as long as the string is smaller than KStringStaticSize
00465             #endif
00466 
00467             static char ksNilString[1];    // Used when strlen == 0;
00468             static char ksErrorString[1];  // Used on allocation errors, so
00469 
00470         #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
00471 
00472     };
00473 
00475     KBASELIB_DLL KString operator+(const KString& pKStr1, const KString& pKStr2);
00476 
00478     KBASELIB_DLL KString operator+(const KString& pKStr, char pChar);
00479 
00481     KBASELIB_DLL KString operator+(const KString& pKStr, const char* pStr);
00482 
00484     KBASELIB_DLL KString operator+(const KString& pKStr, int pValue);
00485 
00487     KBASELIB_DLL KString operator+(const KString& pKStr, float pValue);
00488 
00489 
00490     // Strip the first and last character of a string assuming they are quotation marks.
00491     inline void StripQuotationMarks(KString& str)
00492     {
00493         str = str.Mid( 1, str.GetLen() - 2 );
00494     }
00495 
00496     inline void RemoveChar( KString& pString, char lToRemove )
00497     {
00498         size_t lPos = pString.ReverseFind( lToRemove );
00499 
00500         while( lPos >= 0 )
00501         {
00502             pString = pString.Left( lPos ) + pString.Mid( lPos + 1 );
00503             lPos = pString.ReverseFind( lToRemove );
00504         }
00505     }
00506 
00507     // ---- inline definition ----------------------------------------------
00508     inline bool KString::IsOK()const{ return mData != ksErrorString; }
00509     inline size_t KString::GetLen() const { return mDataLen; }
00510     inline bool KString::IsEmpty()const { return( GetLen() == 0 ); }
00511 
00512     inline char *KString::Buffer() { return mData; }
00513     inline const char *KString::Buffer()const{ return mData; }
00514     inline KString::operator const char *() const{ return mData; }
00515 
00518     class KStringCompare
00519     {
00520         public:
00521             inline int operator()(KString const &pKeyA, KString const &pKeyB) const
00522             {
00523                 return (pKeyA < pKeyB) ? -1 : ((pKeyB < pKeyA) ? 1 : 0);
00524             }
00525     };
00526 
00527     class KCharCompare
00528     {
00529         public:
00530             inline int operator()(char const* pKeyA, char const* pKeyB) const
00531             {
00532                 return strcmp( pKeyA, pKeyB );
00533             }
00534     };
00535 
00536 #include <kbaselib_nsend.h>
00537 
00538 #endif // #define _FBXSDK_KSTRING_H_
00539