00001
00004 #ifndef _FBXSDK_KSTRING_H_
00005 #define _FBXSDK_KSTRING_H_
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <string.h>
00042
00043
00044 #include <kbaselib_h.h>
00045 #include <klib/kscopedptr.h>
00046
00047
00048 #if defined(KARCH_ENV_WIN32)
00049
00050
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
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
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
00435
00436
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
00458
00459 private:
00460 char* mData;
00461 size_t mDataLen;
00462 size_t mAllocLen;
00463 #ifdef UseKStringStatic
00464 char mStaticData[KStringStaticSize];
00465 #endif
00466
00467 static char ksNilString[1];
00468 static char ksErrorString[1];
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
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
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