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 <string.h>
00042 
00043 // FBX includes
00044 #include <fbxfilesdk/components/kbaselib/kbaselib_h.h>
00045 #include <fbxfilesdk/components/kbaselib/klib/kscopedptr.h>
00046 
00047 
00048 #if defined(KARCH_ENV_WIN32)
00049 
00050 // Conversions between WideChar, UTF8 and ANSI
00051     #define KFBX_UTF8_to_WIDECHAR( inUTF8,outWideChar ) \
00052         wchar_t outWideChar[1024]; \
00053         MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, outWideChar, 1024 );
00054 
00055     #define KFBX_WIDECHAR_to_UTF8( inWideChar,outUTF8 ) \
00056         char outUTF8[1024]; \
00057         WideCharToMultiByte( CP_UTF8,0,inWideChar,-1,outUTF8,1024,NULL,NULL);
00058 
00059     #define KFBX_WIDECHAR_to_ANSI( inWideChar,outANSI ) \
00060         char outANSI[1024]; \
00061         WideCharToMultiByte( CP_ACP,0,inWideChar,-1,outANSI,1024,NULL,NULL);
00062 
00063     #define KFBX_ANSI_to_WIDECHAR( inANSI,outWideChar ) \
00064         wchar_t outWideChar[1024]; \
00065         MultiByteToWideChar( CP_ACP, 0, inANSI, -1, outWideChar, 1024 );
00066 
00067     #define KFBX_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 KFBX_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 <fbxfilesdk/fbxfilesdk_nsbegin.h>
00083 class KMemoryAllocator;
00084 
00085 #if !defined(KARCH_ENV_WIN32)
00086     KFBX_DLL void KFBX_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 KFBX_DLL KString
00098     {
00099         public:
00104 
00105                 static KString      *Create();
00109                 static KString      *Create(KString *pString);
00111                 void                Destroy();
00115                 static KString* DestroyIfEmpty(KString *pString);
00119                 static KString* StringOrEmpty(KString *pString);
00121 
00122         public:
00127 
00129                 KString();
00130 
00134                 KString(const KString& pStr);
00135 
00139                 KString(const char* pStr);
00140 
00145                 KString(char pChar, size_t pNbRepeat = 1);
00146 
00151                 KString(const char* pCharPtr, size_t pLength);
00152 
00156                 KString(const int pValue);
00157 
00161                 KString(const float pValue);
00162 
00166                 KString(const double pValue);
00167 
00169                 ~KString();
00171 
00176 
00177                 inline bool IsOK() const;
00178 
00180                 KString &Invalidate();
00181 
00183                 inline size_t GetLen() const;
00184 
00186                 inline bool IsEmpty() const;
00187 
00189                 KString  &Empty();
00191 
00196 
00201                 char& operator[](int pIndex);
00202 
00207                 char operator[](int pIndex) const;
00208 
00210                 inline operator const char*() const;
00211 
00213                 inline char* Buffer();
00214 
00216                 inline const char* Buffer()const;
00217 
00219 
00224 
00228                 const KString& operator=(const KString& pStr);
00229 
00233                 const KString& operator=(char pChar);
00234 
00238                 const KString& operator=(const char* pStr);
00239 
00243                 const KString& operator=(int  pValue);
00244 
00248                 const KString& operator=(float pValue);
00249 
00253                 const KString& operator=(double pValue);
00254 
00259                 const KString& Copy(size_t pLen, const char* pStr);
00260 
00264                 void Swap(KString& pStr);
00266 
00271 
00275                 const KString& Append(const char* pStr);
00276 
00281                 const KString& AppendN(const char* pStr, size_t pLength);
00282 
00286                 const KString& operator+=(const KString& pKStr);
00287 
00291                 const KString& operator+=(char pChar);
00292 
00296                 const KString& operator+=(const char* pStr);
00297 
00301                 const KString& operator+=(int pValue);
00302 
00306                 const KString& operator+=(float pValue);
00307 
00312                 friend KFBX_DLL KString operator+(const KString& pKStr1, const KString& pKStr2);
00313 
00318                 friend KFBX_DLL KString operator+(const KString& pKStr, char pChar);
00319 
00324                 friend KFBX_DLL KString operator+(const KString& pKStr, const char* pStr);
00325 
00330                 friend KFBX_DLL KString operator+(const KString& pKStr, int pValue);
00331 
00336                 friend KFBX_DLL KString operator+(const KString& pKStr, float pValue);
00338 
00343 
00347                 int Compare(const char * pStr) const;
00348 
00352                 int CompareNoCase( const char * pStr ) const;
00353 
00357                 bool operator==(const KString &pStr) const;
00358 
00362                 bool operator!=(const KString &pStr) const;
00363 
00367                 bool operator< (const KString &pStr) const;
00368 
00372                 bool operator<=(const KString &pStr) const;
00373 
00377                 bool operator>=(const KString &pStr) const;
00378 
00382                 bool operator> (const KString &pStr) const;
00383 
00387                 bool operator==(const char *pStr) const;
00388 
00392                 bool operator!=(const char *pStr) const;
00393 
00397                 bool operator< (const char *pStr) const;
00398 
00402                 bool operator<=(const char *pStr) const;
00403 
00407                 bool operator>=(const char *pStr) const;
00408 
00412                 bool operator> (const char *pStr) const;
00414 
00419 
00424                 KString Mid(size_t pFirst, size_t pCount) const;
00425 
00429                 KString Mid(size_t pFirst) const;
00430 
00434                 KString Left(size_t pCount) const;
00435 
00439                 KString Right(size_t pCount) const;
00440 
00442 
00447 
00453                 enum PaddingType 
00454                 { 
00455                     eRight,
00456                     eLeft, 
00457                     eBoth
00458                 };
00459 
00466                 KString Pad(PaddingType pPadding, size_t pLen, char pCar = ' ') const;
00467 
00471                 KString UnPad(PaddingType pPadding) const;
00472 
00474 
00479 
00480                 KString Upper() const;
00481 
00483                 KString Lower() const;
00484 
00486                 KString Reverse() const;
00487 
00489                 KString ConvertToUnix() const;
00490 
00492                 KString ConvertToWindows() const;
00494 
00504                 int Find(char pChar, size_t pStartPosition = 0) const;
00505 
00511                 int Find(const char* pStrSub, size_t pStartPosition = 0) const;
00512 
00517                 int ReverseFind(char pChar) const;
00518 
00524                 int FindOneOf(const char * pStrCharSet, size_t pStartPosition = 0) const;
00525 
00532                 bool FindAndReplace(const char* pFind, const char* pReplaceBy, size_t pStartPosition = 0);
00533 
00539                 bool ReplaceAll( char pFind, char pReplaceBy );
00540 
00542 
00551                 int GetTokenCount(const char* pSpans) const;
00552 
00557                 KString GetToken(int pTokenIndex, const char* pSpans) const;
00559 
00564             public:
00566                 static KMemoryAllocator* AllocatorGet();
00568                 static void AllocatorPurge();
00570                 static void  AllocatorRelease();
00572 
00574         //  WARNING!
00575         //  Anything beyond these lines may not be documented accurately and is
00576         //  subject to change without notice.
00578         #ifndef DOXYGEN_SHOULD_SKIP_THIS
00579         private:
00580             void Init();
00581             void FreeBuffer();
00582             void FreeBuffer(char *&pOldData);
00583 
00584             bool AllocCopy(KString& pDest, size_t pCopyLen, size_t pCopyIndex) const;
00585             bool AllocBuffer(size_t pLen);
00586             bool AllocBuffer(size_t pLen, char *&pOldData);
00587 
00588             bool AssignCopy(size_t pSrcLen, const char * pSrcData);
00589             bool ConcatCopy(size_t pSrc1Len, const char * pSrc1Data,
00590                             size_t pSrc2Len, const char * pSrc2Data);
00591             bool ConcatInPlace(size_t pSrcLen, const char * pSrcData);
00592 
00593             bool IsIn(char pChar, const char* pString) const;
00594 
00595             static KScopedPtr<KMemoryAllocator> mMemoryPool;
00596 
00597         // Lengths/sizes in characters.
00598         // Note: an extra character is always allocated.
00599         private:
00600             char*       mData; // Actual string (zero terminated).
00601             size_t      mDataLen; // Does not include terminating 0.
00602             size_t      mAllocLen; // Does not include terminating 0.
00603             #ifdef UseKStringStatic
00604                 char    mStaticData[KStringStaticSize]; // Actual static string (zero terminated) used as long as the string is smaller than KStringStaticSize
00605             #endif
00606 
00607             static char ksNilString[1];    // Used when strlen == 0;
00608             static char ksErrorString[1];  // Used on allocation errors, so
00609 
00610         #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
00611 
00612     };
00613 
00615     KFBX_DLL KString operator+(const KString& pKStr1, const KString& pKStr2);
00616 
00618     KFBX_DLL KString operator+(const KString& pKStr, char pChar);
00619 
00621     KFBX_DLL KString operator+(const KString& pKStr, const char* pStr);
00622 
00624     KFBX_DLL KString operator+(const KString& pKStr, int pValue);
00625 
00627     KFBX_DLL KString operator+(const KString& pKStr, float pValue);
00628 
00629 
00633     inline void StripQuotationMarks(  KString& str)
00634     {
00635         str = str.Mid( 1, str.GetLen() - 2 );
00636     }
00637 
00642     inline void RemoveChar( KString& pString, char lToRemove )
00643     {
00644         int lPos = pString.ReverseFind( lToRemove );
00645 
00646         while( lPos >= 0 )
00647         {
00648             pString = pString.Left( lPos ) + pString.Mid( lPos + 1 );
00649             lPos = pString.ReverseFind( lToRemove );
00650         }
00651     }
00652 
00653     // ---- inline definition ----------------------------------------------
00654 
00655 
00657     inline bool KString::IsOK()const{ return mData != ksErrorString; }
00659     inline size_t KString::GetLen() const { return mDataLen; }
00661     inline bool KString::IsEmpty()const { return( GetLen() == 0 ); }
00662 
00664     inline char *KString::Buffer() { return mData; }
00666     inline const char *KString::Buffer()const{ return mData; }
00668     inline KString::operator const char *() const{ return mData; }
00669 
00672     class KStringCompare
00673     {
00674         public:
00675             inline int operator()(KString const &pKeyA, KString const &pKeyB) const
00676             {
00677                 return (pKeyA < pKeyB) ? -1 : ((pKeyB < pKeyA) ? 1 : 0);
00678             }
00679     };
00680 
00681     class KCharCompare
00682     {
00683         public:
00684             inline int operator()(char const* pKeyA, char const* pKeyB) const
00685             {
00686                 return strcmp( pKeyA, pKeyB );
00687             }
00688     };
00689 
00690 #include <fbxfilesdk/fbxfilesdk_nsend.h>
00691 
00692 #endif // FBXFILESDK_COMPONENTS_KBASELIB_KLIB_KSTRING_H
00693