kstring.h

Go to the documentation of this file.
00001 
00004 #ifndef FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSTRING_H
00005 #define FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSTRING_H
00006 
00007 /**************************************************************************************
00008 
00009  Copyright (C) 1994 - 2010 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 <fbxfilesdk/fbxfilesdk_def.h>
00042 
00043 #include <fbxfilesdk/components/kbaselib/klib/kdebug.h>
00044 
00045 #if defined(KARCH_DEV_MSC) && (KARCH_DEV_MSC <= 1600)
00046     #if defined(KARCH_ARCH_IA32)
00047         #define SIZE_OF_STDSTRING 32
00048     #else
00049         #define SIZE_OF_STDSTRING 48
00050     #endif
00051 #elif defined(KARCH_DEV_GNUC)
00052     #if defined(KARCH_ARCH_IA32)
00053         #define SIZE_OF_STDSTRING 4
00054     #else
00055         #define SIZE_OF_STDSTRING 8
00056     #endif
00057 #endif
00058 
00059 #if !defined(SIZE_OF_STDSTRING)
00060     #error Unsupported platform
00061 #endif
00062 
00063 #define KFBX_KSTRING_OPAQUE_OBJECT_HEADER void* mOpaqueChunk[SIZE_OF_STDSTRING/sizeof(void*)]
00064 #ifndef KFBX_KSTRING_OPAQUE_OBJECT
00065     #define KFBX_KSTRING_OPAQUE_OBJECT KFBX_KSTRING_OPAQUE_OBJECT_HEADER
00066 #endif
00067 
00068 #include <fbxfilesdk/fbxfilesdk_nsbegin.h>
00069 
00070 //Conversions between WideChar, UTF8 and ANSI
00071 
00077 KFBX_DLL void KFbxUTF8ToWidechar(const char* pInUTF8, wchar_t*& pOutWideChar);
00078 
00084 KFBX_DLL void KFbxWideCharToUTF8(const wchar_t* pInWideChar, char*& pOutUTF8 );
00085 
00086 #if defined(KARCH_ENV_WIN32)
00087 
00092     KFBX_DLL void KFbxWideCharToAnsi(const wchar_t* pInWideChar, char*& pOutANSI );
00093 
00099     KFBX_DLL void KFbxAnsiToWideChar(const char* inANSI, wchar_t*& pOutWideChar );
00100 
00106     KFBX_DLL void KFbxAnsiToUTF8( const char* inANSI, char*& outUTF8 );
00107 
00113     KFBX_DLL void KFbxUTF8ToAnsi( const char* pInUTF8, char*& outANSI );
00114 #endif
00115 
00116 //Old macros to convert between WideChar, UTF8 and ANSI
00117 #if defined(KARCH_ENV_WIN32)
00118     // DEPREACTED! Use KFbxUTF8ToWidechar instead
00119     #define KFBX_UTF8_to_WIDECHAR( inUTF8,out_UTF8_to_WIDECHAR ) \
00120         wchar_t* out_UTF8_to_WIDECHAR = NULL;\
00121         KFbxPointerFreeGuard out_UTF8_to_WIDECHAR##PointerFreeGuard; \
00122         if((char*)inUTF8 != NULL) { \
00123         out_UTF8_to_WIDECHAR = (wchar_t *)FbxSdkMalloc((strlen((char*)inUTF8) + (size_t)1) * sizeof(wchar_t)); \
00124         out_UTF8_to_WIDECHAR##PointerFreeGuard.SetPointer((void*)out_UTF8_to_WIDECHAR);\
00125         MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, out_UTF8_to_WIDECHAR, (int)((strlen((char*)inUTF8) + 1)) ); \
00126         }
00127 
00128     // DEPREACTED! Use KFbxWideCharToUTF8 instead
00129     #define KFBX_WIDECHAR_to_UTF8( inWideChar,out_WIDECHAR_to_UTF8 )  \
00130         char* out_WIDECHAR_to_UTF8 = NULL; \
00131         KFbxPointerFreeGuard out_WIDECHAR_to_UTF8##PointerFreeGuard; \
00132         if((wchar_t*)inWideChar != NULL) { \
00133         out_WIDECHAR_to_UTF8 = (char *) FbxSdkMalloc((wcslen((wchar_t *)inWideChar)+1) * sizeof(wchar_t)); \
00134         out_WIDECHAR_to_UTF8##PointerFreeGuard.SetPointer((void*) out_WIDECHAR_to_UTF8);\
00135         WideCharToMultiByte( CP_UTF8,0,inWideChar,-1,out_WIDECHAR_to_UTF8, (int)((wcslen((wchar_t *)inWideChar)+1) * sizeof(wchar_t)),NULL,NULL); \
00136         }
00137 
00138     // DEPREACTED! Use KFbxWideCharToAnsi instead
00139     #define KFBX_WIDECHAR_to_ANSI( inWideChar,out_WIDECHAR_to_ANSI )  \
00140         char* out_WIDECHAR_to_ANSI = NULL; \
00141         KFbxPointerFreeGuard out_WIDECHAR_to_ANSI##PointerFreeGuard; \
00142         if((wchar_t*)inWideChar != NULL) { \
00143         out_WIDECHAR_to_ANSI = (char *) FbxSdkMalloc((wcslen((wchar_t *)inWideChar)+1) * sizeof(wchar_t)); \
00144         out_WIDECHAR_to_ANSI##PointerFreeGuard.SetPointer((void*) out_WIDECHAR_to_ANSI); \
00145         WideCharToMultiByte( CP_ACP,0,inWideChar,-1,out_WIDECHAR_to_ANSI,(int)((wcslen((wchar_t *)inWideChar)+1) * sizeof(wchar_t)),NULL,NULL); \
00146         }
00147 
00148     // DEPREACTED! Use KFbxAnsiToWideChar instead
00149     #define KFBX_ANSI_to_WIDECHAR( inANSI,out_ANSI_to_WIDECHAR ) \
00150         wchar_t* out_ANSI_to_WIDECHAR = NULL; \
00151         KFbxPointerFreeGuard out_ANSI_to_WIDECHAR##PointerFreeGuard;\
00152         if((char*)inANSI != NULL) { \
00153         out_ANSI_to_WIDECHAR = (wchar_t *)FbxSdkMalloc((strlen((char*)inANSI)+1) * sizeof(wchar_t)); \
00154         out_ANSI_to_WIDECHAR##PointerFreeGuard.SetPointer((void*) out_ANSI_to_WIDECHAR); \
00155         MultiByteToWideChar( CP_ACP, 0, inANSI, -1, out_ANSI_to_WIDECHAR, (int)((strlen((char*)inANSI) + 1)) ); \
00156         }
00157 
00158     // DEPREACTED! Use KFbxAnsiToUTF8 instead
00159     #define KFBX_ANSI_to_UTF8( inANSI,outUTF8 )  \
00160         char* outUTF8 = NULL; \
00161         KFbxPointerFreeGuard outUTF8##PointerFreeGuard; \
00162         if((char*)inANSI != NULL){ \
00163         size_t temp_len = strlen((char*)inANSI) + (size_t)1;\
00164         outUTF8 = (char *) FbxSdkMalloc(temp_len); \
00165         outUTF8##PointerFreeGuard.SetPointer((void*) outUTF8);\
00166         wchar_t* temp_ANSI_to_UTF8_WideChar = (wchar_t *)FbxSdkMalloc(temp_len * sizeof(wchar_t)); \
00167         MultiByteToWideChar( CP_ACP, 0, inANSI, -1, temp_ANSI_to_UTF8_WideChar, (int)(temp_len)); \
00168         WideCharToMultiByte( CP_UTF8, 0, temp_ANSI_to_UTF8_WideChar, -1, outUTF8, (int)(temp_len), NULL, NULL);\
00169         FbxSdkFree(temp_ANSI_to_UTF8_WideChar);}\
00170 
00171     // DEPREACTED! Use KFbxUTF8ToAnsi instead
00172     #define KFBX_UTF8_to_ANSI( inUTF8,outANSI )  \
00173         char* outANSI = NULL; \
00174         KFbxPointerFreeGuard outANSI##PointerFreeGuard; \
00175         if((char*)inUTF8 != NULL){ \
00176         size_t temp_len = strlen((char*)inUTF8) + (size_t)1;\
00177         outANSI = (char *) FbxSdkMalloc(temp_len); \
00178         outANSI##PointerFreeGuard.SetPointer((void*) outANSI);\
00179         wchar_t* temp_UTF8_to_ANSI_WideChar = (wchar_t *)FbxSdkMalloc(temp_len * sizeof(wchar_t));\
00180         MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, temp_UTF8_to_ANSI_WideChar, (int)(temp_len)); \
00181         WideCharToMultiByte( CP_ACP, 0, temp_UTF8_to_ANSI_WideChar, -1, outANSI, (int)(temp_len), NULL, NULL);\
00182         FbxSdkFree(temp_UTF8_to_ANSI_WideChar);} \
00183 
00184     #pragma deprecated(KFBX_UTF8_to_WIDECHAR, KFBX_WIDECHAR_to_UTF8, KFBX_WIDECHAR_to_ANSI, KFBX_ANSI_to_WIDECHAR, KFBX_ANSI_to_UTF8, KFBX_UTF8_to_ANSI)
00185 #endif
00186 
00189 class KFBX_DLL KString
00190 {
00191 public:
00196 
00197         KString();
00198 
00201         KString(const KString& pStr);
00202 
00205         KString(const char* pStr);
00206 
00210         KString(char pChar, size_t pNbRepeat=1);
00211 
00215         KString(const char* pCharPtr, size_t pLength);
00216 
00219         KString(const int pValue);
00220 
00223         KString(const float pValue);
00224 
00227         KString(const double pValue);
00228 
00230         ~KString();
00231 
00233         void Destroy();
00235 
00240 
00241         size_t GetLen() const;
00242 
00244         bool IsEmpty() const;
00245 
00247         KString& Empty();
00248 
00252         char& operator[](int pIndex);
00253 
00257         char operator[](int pIndex) const;
00258 
00260         char* Buffer();
00261 
00263         const char* Buffer()const;
00265 
00272         const KString& operator=(const KString& pStr);
00273 
00276         const KString& operator=(char pChar);
00277 
00280         const KString& operator=(const char* pStr);
00281 
00284         const KString& operator=(int pValue);
00285 
00288         const KString& operator=(float pValue);
00289 
00292         const KString& operator=(double pValue);
00293 
00296         const KString& operator+=(const KString& pKStr);
00297 
00300         const KString& operator+=(char pChar);
00301 
00304         const KString& operator+=(const char* pStr);
00305 
00308         const KString& operator+=(int pValue);
00309 
00312         const KString& operator+=(float pValue);
00313 
00316         const KString& operator+=(double pValue);
00317 
00320         bool operator== (const KString &pStr) const;
00321 
00324         bool operator!= (const KString &pStr) const;
00325 
00328         bool operator< (const KString &pStr) const;
00329 
00332         bool operator<= (const KString &pStr) const;
00333 
00336         bool operator>= (const KString &pStr) const;
00337 
00340         bool operator> (const KString &pStr) const;
00341 
00344         bool operator== (const char *pStr) const;
00345 
00348         bool operator!= (const char *pStr) const;
00349 
00352         bool operator< (const char *pStr) const;
00353 
00356         bool operator<= (const char *pStr) const;
00357 
00360         bool operator>= (const char *pStr) const;
00361 
00364         bool operator> (const char *pStr) const;
00365 
00369         friend KFBX_DLL KString operator+(const KString& pKStr1, const KString& pKStr2);
00370 
00374         friend KFBX_DLL KString operator+(const KString& pKStr, char pChar);
00375 
00379         friend KFBX_DLL KString operator+(const KString& pKStr, const char* pStr);
00380 
00384         friend KFBX_DLL KString operator+(const KString& pKStr, int pValue);
00385 
00389         friend KFBX_DLL KString operator+(const KString& pKStr, float pValue);
00390 
00394         friend KFBX_DLL KString operator+(const KString& pKStr, double pValue);
00395 
00397         operator const char*() const;
00398 
00402         const KString& Copy(const char* pStr, size_t pLength);
00403 
00407         const KString& Append(const char* pStr, size_t pLength);
00408 
00411         int Compare(const char* pStr) const;
00412 
00415         int CompareNoCase(const char* pStr) const;
00416 
00419         void Swap(KString& pStr);
00420 
00422         KString Upper() const;
00423 
00425         KString Lower() const;
00427 
00435         KString Mid(size_t pFirst, size_t pCount) const;
00436 
00439         KString Mid(size_t pFirst) const;
00440 
00443         KString Left(size_t pCount) const;
00444 
00447         KString Right(size_t pCount) const;
00449 
00458         enum PaddingType {eRight, eLeft, eBoth};
00459 
00464         KString Pad(PaddingType pPadding, size_t pLen, char pCar=' ') const;
00465 
00468         KString UnPad(PaddingType pPadding) const;
00470 
00479         int Find(char pChar, size_t pStartPosition=0) const;
00480 
00485         int Find(const char* pStrSub, size_t pStartPosition=0) const;
00486 
00490         int ReverseFind(char pChar) const;
00491 
00496         int FindOneOf(const char * pStrCharSet, size_t pStartPosition=0) const;
00497 
00503         bool FindAndReplace(const char* pFind, const char* pReplaceBy, size_t pStartPosition=0);
00504 
00509         bool ReplaceAll(const char* pFind, const char* pReplaceBy);
00510 
00515         bool ReplaceAll(char pFind, char pReplaceBy);
00517 
00525         int GetTokenCount(const char* pSpans) const;
00526 
00530         KString GetToken(int pTokenIndex, const char* pSpans) const;
00532 
00533 private:
00534     KFBX_KSTRING_OPAQUE_OBJECT;
00535 };
00536 
00537 KFBX_INCOMPATIBLE_WITH_KARRAYTEMPLATE(KString);
00538 
00540 KFBX_DLL KString operator+(const KString& pKStr1, const KString& pKStr2);
00541 
00543 KFBX_DLL KString operator+(const KString& pKStr, char pChar);
00544 
00546 KFBX_DLL KString operator+(const KString& pKStr, const char* pStr);
00547 
00549 KFBX_DLL KString operator+(const KString& pKStr, int pValue);
00550 
00552 KFBX_DLL KString operator+(const KString& pKStr, float pValue);
00553 
00555 KFBX_DLL KString operator+(const KString& pKStr, double pValue);
00556 
00558 class KStringCompare
00559 {
00560 public:
00566     inline int operator()(KString const &pKeyA, KString const &pKeyB) const
00567     {
00568         return (pKeyA < pKeyB) ? -1 : ((pKeyB < pKeyA) ? 1 : 0);
00569     }
00570 };
00571 
00573 class KCharCompare
00574 {
00575 public:
00577     inline int operator()(char const* pKeyA, char const* pKeyB) const
00578     {
00579         return strcmp(pKeyA, pKeyB);
00580     }
00581 };
00582 
00587 inline void RemoveChar(KString& pString, char lToRemove)
00588 {
00589     int lPos = pString.ReverseFind(lToRemove);
00590     while( lPos >= 0 )
00591     {
00592         pString = pString.Left(lPos) + pString.Mid(lPos + 1);
00593         lPos = pString.ReverseFind(lToRemove);
00594     }
00595 }
00596 
00597 #include <fbxfilesdk/fbxfilesdk_nsend.h>
00598 
00599 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSTRING_H