kstring.h

Go to the documentation of this file.
00001 
00004 #ifndef _FBXSDK_KSTRING_H_
00005 #define _FBXSDK_KSTRING_H_
00006 
00007 /**************************************************************************************
00008 
00009  Copyright © 1995 - 2007 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 <kbaselib_h.h>
00042 
00043 #if defined(KARCH_ENV_WIN32)
00044 
00045 // Conversions between WideChar, UTF8 and ANSI
00046     #define UTF8_to_WIDECHAR( inUTF8,outWideChar ) \
00047         wchar_t outWideChar[1024]; \
00048         MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, outWideChar, 1024 );
00049 
00050     #define WIDECHAR_to_UTF8( inWideChar,outUTF8 ) \
00051         char outUTF8[1024]; \
00052         WideCharToMultiByte( CP_UTF8,0,inWideChar,-1,outUTF8,1024,NULL,NULL);
00053 
00054     #define WIDECHAR_to_ANSI( inWideChar,outANSI ) \
00055         char outANSI[1024]; \
00056         WideCharToMultiByte( CP_ACP,0,inWideChar,-1,outANSI,1024,NULL,NULL);
00057 
00058     #define ANSI_to_WIDECHAR( inANSI,outWideChar ) \
00059         wchar_t outWideChar[1024]; \
00060         MultiByteToWideChar( CP_ACP, 0, inANSI, -1, outWideChar, 1024 );
00061 
00062     #define ANSI_to_UTF8( inANSI,outUTF8 ) { \
00063         wchar_t tempWideChar[1024]; \
00064         MultiByteToWideChar( CP_ACP, 0, inANSI, -1, tempWideChar, 1024); \
00065         WideCharToMultiByte( CP_UTF8, 0, tempWideChar, -1, outUTF8, 1024, NULL, NULL); \
00066     }
00067 
00068     #define UTF8_to_ANSI( inUTF8,outANSI, dummy, dummy1 ) { \
00069         wchar_t tempWideChar[1024]; \
00070         MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, tempWideChar, 1024); \
00071         WideCharToMultiByte( CP_ACP, 0, tempWideChar, -1, outANSI, 1024, NULL, NULL); \
00072     }
00073 #endif
00074 
00075 
00076 #include <kbaselib_nsbegin.h>
00077 
00078 #if !defined(KARCH_ENV_WIN32)    
00079     KBASELIB_DLL void UTF8_to_ANSI( const char* pSrc, char* pDest, int pDestSize, char pSubstitute = '.' );
00080 #endif
00081 
00082     #define KStringStaticSize 64
00083 
00087     class KBASELIB_DLL KString
00088     {
00089     public:
00094 
00095         static KString      *Create();
00097         static KString      *Create(KString *pString);
00099         void                Destroy();
00101         static KString* DestroyIfEmpty(KString *pString);
00103         static KString* StringOrEmpty(KString *pString);
00105 
00106     public:
00107 
00112 
00114         KString();
00115 
00117         KString(const KString& pStr);
00118 
00120         KString(const char* pStr);
00121 
00123         KString(char pChar, int pNbRepeat = 1);
00124 
00126         KString(const char* pCharPtr, int pLength);
00127 
00129         KString(const int pValue);
00130 
00132         KString(const float pValue);
00133 
00135         KString(const double pValue);
00136 
00138         ~KString();
00139 
00141 
00146 
00148         bool IsOK() const;
00149 
00151         KString &Invalidate();
00152 
00154         int GetLen() const;
00155 
00157         bool IsEmpty() const;
00158 
00160         KString  &Empty();   
00161 
00163 
00168 
00170         char& operator[](int pIndex);
00171 
00173         char operator[](int pIndex) const;    
00174 
00176         operator const char*() const;
00177 
00179         char* ToChars() const;
00180 
00182         char* Buffer();     
00183 
00185         char* GetBuffer();
00186 
00188 
00193 
00195         const KString& operator=(const KString& pStr);
00196 
00198         const KString& operator=(char pChar);
00199 
00201         const KString& operator=(const char* pStr);
00202 
00204         const KString& operator=(int  pValue);
00205 
00207         const KString& operator=(float pValue);
00208 
00210         const KString& operator=(double pValue);
00211 
00213         const KString& Copy(int pLen, const char* pStr);
00214 
00216 
00221 
00223         const KString& Append(const char* pStr); 
00224 
00226         const KString& AppendN(const char* pStr, int pLength); 
00227 
00229         const KString& operator+=(const KString& pKStr);
00230 
00232         const KString& operator+=(char pChar);
00233 
00235         const KString& operator+=(const char* pStr);
00236 
00238         const KString& operator+=(int pValue);
00239 
00241         const KString& operator+=(float pValue);
00242 
00244         friend KBASELIB_DLL KString operator+(const KString& pKStr1, const KString& pKStr2);
00245 
00247         friend KBASELIB_DLL KString operator+(const KString& pKStr, char pChar);
00248 
00250         friend KBASELIB_DLL KString operator+(const KString& pKStr, const char* pStr);
00251 
00253         friend KBASELIB_DLL KString operator+(const KString& pKStr, int pValue);
00254 
00256         friend KBASELIB_DLL KString operator+(const KString& pKStr, float pValue);
00257 
00259 
00264         
00266         int Compare(const char * pStr) const;        
00267 
00269         int CompareNoCase( const char * pStr ) const;   
00270 
00272         bool operator==(const KString &pStr) const;
00273 
00275         bool operator!=(const KString &pStr) const;
00276 
00278         bool operator< (const KString &pStr) const;
00279 
00281         bool operator<=(const KString &pStr) const;
00282 
00284         bool operator>=(const KString &pStr) const;
00285 
00287         bool operator> (const KString &pStr) const;
00288 
00290         bool operator==(const char *pStr) const;
00291 
00293         bool operator!=(const char *pStr) const;
00294 
00296         bool operator< (const char *pStr) const;
00297 
00299         bool operator<=(const char *pStr) const;
00300 
00302         bool operator>=(const char *pStr) const;
00303 
00305         bool operator> (const char *pStr) const;
00306 
00308 
00313 
00315         KString Mid(int pFirst, int pCount) const;
00316 
00318         KString Mid(int pFirst) const;
00319 
00321         KString Left(int pCount) const;
00322 
00324         KString Right(int pCount) const;
00325 
00327 
00332 
00334         enum PaddingType { eRight, eLeft, eBoth };
00335 
00337         KString Pad(PaddingType pPadding, int pLen, char pCar = ' ') const;
00338 
00340         KString UnPad(PaddingType pPadding) const;
00341 
00343 
00348 
00350         KString Upper() const;
00351 
00353         KString Lower() const;
00354 
00356         KString Reverse() const;
00357 
00359         KString ConvertToUnix() const;
00360 
00362         KString ConvertToWindows() const;
00363 
00365 
00370 
00374         int Find(char pChar, int pStartPosition = 0) const; 
00375 
00379         int Find(const char* pStrSub, int pStartPosition = 0) const;
00380 
00384         int ReverseFind(char pChar) const;
00385 
00389         int FindOneOf(const char * pStrCharSet, int pStartPosition = 0) const;
00390 
00394         bool FindAndReplace(const char* pFind, const char* pReplaceBy, int pStartPosition = 0);
00395 
00397 
00402 
00404         int GetTokenCount(const char* pSpans);
00405 
00407         KString GetToken(int pTokenIndex, const char* pSpans);
00408 
00410 
00412     //
00413     //  WARNING!
00414     //
00415     //  Anything beyond these lines may not be documented accurately and is 
00416     //  subject to change without notice.
00417     //
00419 
00420     #ifndef DOXYGEN_SHOULD_SKIP_THIS
00421 
00422     private:
00423 
00424         // Lengths/sizes in characters. 
00425         // Note: an extra character is always allocated.
00426         char *mData; // Actual string (zero terminated).
00427         int mDataLen; // Does not include terminating 0.
00428         int mAllocLen; // Does not include terminating 0.
00429         char mStaticData[KStringStaticSize]; // Actual static string (zero terminated) used as long as the string is smaller than KStringStaticSize
00430 
00431         void Init();
00432         void FreeBuffer();
00433         void FreeBuffer(char *&pOldData);
00434 
00435         bool AllocCopy(KString& pDest, int pCopyLen, int pCopyIndex) const;
00436         bool AllocBuffer(int pLen);
00437         bool AllocBuffer(int pLen, char *&pOldData);
00438 
00439         bool AssignCopy(int pSrcLen, const char * pSrcData);
00440         bool ConcatCopy(int pSrc1Len, const char * pSrc1Data, 
00441                         int pSrc2Len, const char * pSrc2Data);
00442         bool ConcatInPlace(int pSrcLen, const char * pSrcData);
00443 
00444         bool IsIn(char pChar, const char* pString);
00445 
00446     #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
00447 
00448     };
00449 
00451     KBASELIB_DLL KString operator+(const KString& pKStr1, const KString& pKStr2);
00452 
00454     KBASELIB_DLL KString operator+(const KString& pKStr, char pChar);
00455 
00457     KBASELIB_DLL KString operator+(const KString& pKStr, const char* pStr);
00458 
00460     KBASELIB_DLL KString operator+(const KString& pKStr, int pValue);
00461 
00463     KBASELIB_DLL KString operator+(const KString& pKStr, float pValue);
00464 
00465 
00466     // Strip the first and last character of a string assuming they are quotation marks.
00467     inline void StripQuotationMarks(KString& str)
00468     {
00469         str = str.Mid( 1, str.GetLen() - 2 );
00470     }
00471 
00472     inline void RemoveChar( KString& pString, char lToRemove )
00473     {
00474         int lPos = pString.ReverseFind( lToRemove );
00475 
00476         while( lPos >= 0 ) 
00477         {
00478             pString = pString.Left( lPos ) + pString.Mid( lPos + 1 );
00479             lPos = pString.ReverseFind( lToRemove );
00480         }
00481     }
00482 
00483 #include <kbaselib_nsend.h>
00484 
00485 #endif // #define _FBXSDK_KSTRING_H_
00486