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) 2001 - 2009 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 #include <fbxfilesdk/fbxfilesdk_nsbegin.h>
00046 
00047 #if defined(KARCH_ENV_WIN32)
00048 
00049     //Conversions between WideChar, UTF8 and ANSI
00050     #define KFBX_UTF8_to_WIDECHAR( inUTF8,out_UTF8_to_WIDECHAR ) \
00051         wchar_t* out_UTF8_to_WIDECHAR = NULL;\
00052         KFbxPointerFreeGuard out_UTF8_to_WIDECHAR##PointerFreeGuard; \
00053         if((char*)inUTF8 != NULL) { \
00054             out_UTF8_to_WIDECHAR = (wchar_t *)FbxSdkMalloc((strlen((char*)inUTF8) + (size_t)1) * sizeof(wchar_t)); \
00055             out_UTF8_to_WIDECHAR##PointerFreeGuard.SetPointer((void*)out_UTF8_to_WIDECHAR);\
00056             MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, out_UTF8_to_WIDECHAR, (int)((strlen((char*)inUTF8) + 1) * sizeof(wchar_t)) ); \
00057         }
00058 
00059     #define KFBX_WIDECHAR_to_UTF8( inWideChar,out_WIDECHAR_to_UTF8 )  \
00060         char* out_WIDECHAR_to_UTF8 = NULL; \
00061         KFbxPointerFreeGuard out_WIDECHAR_to_UTF8##PointerFreeGuard; \
00062         if((wchar_t*)inWideChar != NULL) { \
00063             out_WIDECHAR_to_UTF8 = (char *) FbxSdkMalloc((wcslen((wchar_t *)inWideChar)+1) * 2); \
00064             out_WIDECHAR_to_UTF8##PointerFreeGuard.SetPointer((void*) out_WIDECHAR_to_UTF8);\
00065             WideCharToMultiByte( CP_UTF8,0,inWideChar,-1,out_WIDECHAR_to_UTF8, (int)((wcslen((wchar_t *)inWideChar)+1) * 2),NULL,NULL); \
00066         }
00067 
00068     #define KFBX_WIDECHAR_to_ANSI( inWideChar,out_WIDECHAR_to_ANSI )  \
00069         char* out_WIDECHAR_to_ANSI = NULL; \
00070         KFbxPointerFreeGuard out_WIDECHAR_to_ANSI##PointerFreeGuard; \
00071         if((wchar_t*)inWideChar != NULL) { \
00072             out_WIDECHAR_to_ANSI = (char *) FbxSdkMalloc((wcslen((wchar_t *)inWideChar)+1) * 2); \
00073             out_WIDECHAR_to_ANSI##PointerFreeGuard.SetPointer((void*) out_WIDECHAR_to_ANSI); \
00074             WideCharToMultiByte( CP_ACP,0,inWideChar,-1,out_WIDECHAR_to_ANSI,(int)((wcslen((wchar_t *)inWideChar)+1) * 2),NULL,NULL); \
00075         }
00076 
00077     #define KFBX_ANSI_to_WIDECHAR( inANSI,out_ANSI_to_WIDECHAR ) \
00078         wchar_t* out_ANSI_to_WIDECHAR = NULL; \
00079         KFbxPointerFreeGuard out_ANSI_to_WIDECHAR##PointerFreeGuard;\
00080         if((char*)inANSI != NULL) { \
00081             out_ANSI_to_WIDECHAR = (wchar_t *)FbxSdkMalloc((strlen((char*)inANSI)+1) * sizeof(wchar_t)); \
00082             out_ANSI_to_WIDECHAR##PointerFreeGuard.SetPointer((void*) out_ANSI_to_WIDECHAR); \
00083             MultiByteToWideChar( CP_ACP, 0, inANSI, -1, out_ANSI_to_WIDECHAR, (int)((strlen((char*)inANSI) + 1) * sizeof(wchar_t)) ); \
00084         }
00085 
00086     #define KFBX_ANSI_to_UTF8( inANSI,outUTF8 ) { \
00087     if((char*)inANSI != NULL){ \
00088         wchar_t* temp_ANSI_to_UTF8_WideChar = (wchar_t *)FbxSdkMalloc((strlen((char*)inANSI)+1) * sizeof(wchar_t)); \
00089         MultiByteToWideChar( CP_ACP, 0, inANSI, -1, temp_ANSI_to_UTF8_WideChar, (int)((strlen((char*)inANSI)+1) * sizeof(wchar_t))); \
00090         WideCharToMultiByte( CP_UTF8, 0, temp_ANSI_to_UTF8_WideChar, -1, outUTF8, (int)((wcslen((wchar_t *)temp_ANSI_to_UTF8_WideChar)+1) * 2), NULL, NULL);\
00091         FbxSdkFree(temp_ANSI_to_UTF8_WideChar);}\
00092         }
00093 
00094     #define KFBX_UTF8_to_ANSI( inUTF8,outANSI ) { \
00095     if((char*)inUTF8 != NULL){ \
00096         wchar_t* temp_UTF8_to_ANSI_WideChar = (wchar_t *)FbxSdkMalloc((strlen((char*)inUTF8)+1) * sizeof(wchar_t));\
00097         MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, temp_UTF8_to_ANSI_WideChar, (int)((strlen((char*)inUTF8)+1) * sizeof(wchar_t))); \
00098         WideCharToMultiByte( CP_ACP, 0, temp_UTF8_to_ANSI_WideChar, -1, outANSI, (int)((wcslen((wchar_t *)temp_UTF8_to_ANSI_WideChar)+1) * 2), NULL, NULL);\
00099         FbxSdkFree(temp_UTF8_to_ANSI_WideChar);} \
00100         }
00101 #endif
00102 
00103 
00104 struct KString_prv;
00105 
00108 class KFBX_DLL KString
00109 {
00110 public:
00115 
00116         KString();
00117 
00120         KString(const KString& pStr);
00121 
00124         KString(const char* pStr);
00125 
00129         KString(char pChar, size_t pNbRepeat=1);
00130 
00134         KString(const char* pCharPtr, size_t pLength);
00135 
00138         KString(const int pValue);
00139 
00142         KString(const float pValue);
00143 
00146         KString(const double pValue);
00147 
00149         ~KString();
00150 
00152         void Destroy();
00154 
00159 
00160         size_t GetLen() const;
00161 
00163         bool IsEmpty() const;
00164 
00166         KString& Empty();
00167 
00171         char& operator[](int pIndex);
00172 
00176         char operator[](int pIndex) const;
00177 
00179         char* Buffer();
00180 
00182         const char* Buffer()const;
00184 
00191         const KString& operator=(const KString& pStr);
00192 
00195         const KString& operator=(char pChar);
00196 
00199         const KString& operator=(const char* pStr);
00200 
00203         const KString& operator=(int pValue);
00204 
00207         const KString& operator=(float pValue);
00208 
00211         const KString& operator=(double pValue);
00212 
00215         const KString& operator+=(const KString& pKStr);
00216 
00219         const KString& operator+=(char pChar);
00220 
00223         const KString& operator+=(const char* pStr);
00224 
00227         const KString& operator+=(int pValue);
00228 
00231         const KString& operator+=(float pValue);
00232 
00235         const KString& operator+=(double pValue);
00236 
00239         bool operator== (const KString &pStr) const;
00240 
00243         bool operator!= (const KString &pStr) const;
00244 
00247         bool operator< (const KString &pStr) const;
00248 
00251         bool operator<= (const KString &pStr) const;
00252 
00255         bool operator>= (const KString &pStr) const;
00256 
00259         bool operator> (const KString &pStr) const;
00260 
00263         bool operator== (const char *pStr) const;
00264 
00267         bool operator!= (const char *pStr) const;
00268 
00271         bool operator< (const char *pStr) const;
00272 
00275         bool operator<= (const char *pStr) const;
00276 
00279         bool operator>= (const char *pStr) const;
00280 
00283         bool operator> (const char *pStr) const;
00284 
00288         friend KFBX_DLL KString operator+(const KString& pKStr1, const KString& pKStr2);
00289 
00293         friend KFBX_DLL KString operator+(const KString& pKStr, char pChar);
00294 
00298         friend KFBX_DLL KString operator+(const KString& pKStr, const char* pStr);
00299 
00303         friend KFBX_DLL KString operator+(const KString& pKStr, int pValue);
00304 
00308         friend KFBX_DLL KString operator+(const KString& pKStr, float pValue);
00309 
00313         friend KFBX_DLL KString operator+(const KString& pKStr, double pValue);
00314 
00316         operator const char*() const;
00317 
00321         const KString& Copy(const char* pStr, size_t pLength);
00322 
00326         const KString& Append(const char* pStr, size_t pLength);
00327 
00330         int Compare(const char* pStr) const;
00331 
00334         int CompareNoCase(const char* pStr) const;
00335 
00338         void Swap(KString& pStr);
00339 
00341         KString Upper() const;
00342 
00344         KString Lower() const;
00346 
00354         KString Mid(size_t pFirst, size_t pCount) const;
00355 
00358         KString Mid(size_t pFirst) const;
00359 
00362         KString Left(size_t pCount) const;
00363 
00366         KString Right(size_t pCount) const;
00368 
00377         enum PaddingType {eRight, eLeft, eBoth};
00378 
00383         KString Pad(PaddingType pPadding, size_t pLen, char pCar=' ') const;
00384 
00387         KString UnPad(PaddingType pPadding) const;
00389 
00398         int Find(char pChar, size_t pStartPosition=0) const;
00399 
00404         int Find(const char* pStrSub, size_t pStartPosition=0) const;
00405 
00409         int ReverseFind(char pChar) const;
00410 
00415         int FindOneOf(const char * pStrCharSet, size_t pStartPosition=0) const;
00416 
00422         bool FindAndReplace(const char* pFind, const char* pReplaceBy, size_t pStartPosition=0);
00423 
00428         bool ReplaceAll(const char* pFind, const char* pReplaceBy);
00429 
00434         bool ReplaceAll(char pFind, char pReplaceBy);
00436 
00444         int GetTokenCount(const char* pSpans) const;
00445 
00449         KString GetToken(int pTokenIndex, const char* pSpans) const;
00451 
00452 private:
00453     void Init();
00454     KString_prv* mData;
00455 
00456 #ifdef _DEBUG
00457     const char*     mDEBUG_String;
00458     size_t          mDEBUG_StringLength;
00459 #endif
00460 };
00461 
00462 KFBX_INCOMPATIBLE_WITH_KARRAYTEMPLATE(KString);
00463 
00465 KFBX_DLL KString operator+(const KString& pKStr1, const KString& pKStr2);
00466 
00468 KFBX_DLL KString operator+(const KString& pKStr, char pChar);
00469 
00471 KFBX_DLL KString operator+(const KString& pKStr, const char* pStr);
00472 
00474 KFBX_DLL KString operator+(const KString& pKStr, int pValue);
00475 
00477 KFBX_DLL KString operator+(const KString& pKStr, float pValue);
00478 
00480 KFBX_DLL KString operator+(const KString& pKStr, double pValue);
00481 
00483 class KStringCompare
00484 {
00485 public:
00491     inline int operator()(KString const &pKeyA, KString const &pKeyB) const
00492     {
00493         return (pKeyA < pKeyB) ? -1 : ((pKeyB < pKeyA) ? 1 : 0);
00494     }
00495 };
00496 
00498 class KCharCompare
00499 {
00500 public:
00502     inline int operator()(char const* pKeyA, char const* pKeyB) const
00503     {
00504         return strcmp(pKeyA, pKeyB);
00505     }
00506 };
00507 
00512 inline void RemoveChar(KString& pString, char lToRemove)
00513 {
00514     int lPos = pString.ReverseFind(lToRemove);
00515     while( lPos >= 0 )
00516     {
00517         pString = pString.Left(lPos) + pString.Mid(lPos + 1);
00518         lPos = pString.ReverseFind(lToRemove);
00519     }
00520 }
00521 
00522 #include <fbxfilesdk/fbxfilesdk_nsend.h>
00523 
00524 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSTRING_H