00001
00002
00003
00004
00005
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
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #ifndef _SB_STRING_
00052 #define _SB_STRING_
00053
00054 #include <string.h>
00055 #include <wchar.h>
00056 #include <stdarg.h>
00057
00058 #include <Inventor/SoInventorBase.h>
00059 #include <Inventor/SbBase.h>
00060 #include <Inventor/STL/string>
00061 #include <Inventor/STL/ostream>
00062 #include <Inventor/STL/vector>
00063
00064 #ifdef _WIN32
00065 #pragma warning(push)
00066 #pragma warning(disable:4251)
00067 #endif
00068
00203 class SbString {
00204 public:
00205
00209 SbString();
00210
00214 SbString(const std::string &std_string);
00215
00219 SbString(const std::wstring &std_wstring);
00220
00224 SbString(const char *str);
00225
00231 SbString(const char *str, int start, int end);
00232
00238 SbString(const wchar_t *str, int start, int end);
00239
00240
00244 SbString(const SbString &str);
00245
00249 SbString( const wchar_t* str );
00250
00254 ~SbString();
00255
00260 static const int npos;
00261
00267 SbBool isEmpty() const;
00268
00273 SbBool isNull() const;
00274
00278 int getLength() const;
00279
00285 void makeEmpty( const SbBool freeOld = TRUE );
00286
00290 void makeNull();
00291
00297 SbString getSubString(int startChar, int endChar = -1) const;
00298
00304 void deleteSubString(int startChar, int endChar = -1);
00305
00309 SbBool contains( const SbString& str ) const;
00310
00314 bool endsWith(const SbString& str) const;
00315
00320 int find( const SbString& str ) const;
00321
00326 int rfind( const SbString& str ) const;
00327
00333 int findLast( const SbString& str ) const;
00334
00338 SbBool containsAny( const SbString& str ) const;
00339
00345 int findAny( const SbString& str ) const;
00346
00350 void replace( const SbString& patternStr, const SbString& substStr );
00351
00352
00353
00358 SbString lower() const;
00359
00364 SbString upper() const;
00365
00374 SbString& setNum( short value, int base = 10 );
00375
00379 SbString& setNum( unsigned short value, int base = 10 );
00380
00384 SbString& setNum( int value, int base = 10 );
00385
00389 SbString& setNum( unsigned int value, int base = 10 );
00390
00394 SbString& setNum( long value, int base = 10 );
00395
00399 SbString& setNum( unsigned long value, int base = 10 );
00400
00404 SbString& setNum( double value );
00405
00409 SbString& setNum( float value );
00410
00417 unsigned short toUShort( SbBool* ok = NULL ) const;
00418
00422 short toShort( SbBool* ok = NULL ) const;
00423
00427 unsigned int toUInt( SbBool* ok = NULL ) const;
00428
00432 int toInt( SbBool* ok = NULL ) const;
00433
00437 unsigned long toULong( SbBool* ok = NULL ) const;
00438
00442 long toLong( SbBool* ok = NULL ) const;
00443
00447 float toFloat( SbBool* ok = NULL ) const;
00448
00452 double toDouble( SbBool* ok = NULL ) const;
00453
00454
00455
00456
00457
00458
00462 SbBool isUnicode() const;
00463
00464
00465
00469 SbString& fromLatin1( const char* str );
00470
00474 SbString& fromUtf16( const unsigned short* str );
00475
00479 SbString& fromWideChar( const wchar_t* str );
00480
00481
00482
00486 const char* toLatin1() const;
00487
00491 unsigned short* toUtf16() const;
00492
00496 const wchar_t* toWideChar() const;
00497
00501 std::wstring toStdWString() const;
00502
00506 std::string toStdString() const;
00507
00508
00509
00513 SbString &operator +=( const SbString& str );
00514
00518 SbString &operator +=( const char *str );
00519
00523 SbString &operator =(const SbString &str);
00524
00528 SbString &operator =(const char *str);
00529
00530
00534 friend const SbString operator+( const SbString &s1, const SbString &s2 );
00535
00541 friend int operator ==(const SbString &str1, const SbString &str2);
00542
00548 friend int operator ==(const SbString &str, const char *s);
00549
00555 friend int operator ==(const char *s, const SbString &str);
00556
00562 friend int operator !=(const SbString &str1, const SbString &str2);
00563
00569 friend bool operator < (const SbString &str1, const SbString &str2);
00570
00576 friend bool operator > (const SbString &str1, const SbString &str2);
00577
00583 friend bool operator <= (const SbString &str1, const SbString &str2);
00584
00590 friend bool operator >= (const SbString &str1, const SbString &str2);
00591
00595 friend std::ostream& operator << (std::ostream& os, const SbString& str)
00596 {
00597 if (str.isUnicode())
00598 return os << str.toUtf8();
00599
00600 return os << str.toLatin1();
00601 }
00602
00603
00607 uint32_t hash() const;
00608
00609
00610
00618 SoNONUNICODE SbString( const int digitString );
00619
00626 SoNONUNICODE const char *getString() const;
00627
00634 SoNONUNICODE const std::string &getSString() const;
00635
00642 SoNONUNICODE SbString & sprintf(const char * cformat, ... );
00643
00649 SoNONUNICODE SbString & vprintf(const char * cformat, va_list arg);
00650
00657 SoNONUNICODE friend const SbString operator+( const SbString &s1, const char *s2 );
00658
00665 SoNONUNICODE friend const SbString operator+( const char *s1, const SbString &s2 );
00666
00672 SoNONUNICODE char operator[]( int i ) const;
00673
00679 SoNONUNICODE int operator !() const;
00680
00688 SoNONUNICODE friend int operator ==(const SbString &str, const std::string &s);
00689
00697 SoNONUNICODE friend int operator ==(const std::string &s, const SbString &str);
00698
00706 SoNONUNICODE friend int operator !=(const SbString &str, const char *s);
00707
00715 SoNONUNICODE friend int operator !=(const char *s, const SbString &str);
00716
00724 SoNONUNICODE friend bool operator < (const SbString &str, const char *s);
00725
00733 SoNONUNICODE friend bool operator < (const char *s, const SbString &str);
00734
00742 SoNONUNICODE friend bool operator < (const SbString &str, const std::string &s);
00743
00751 SoNONUNICODE friend bool operator < (const std::string &s, const SbString &str);
00752
00760 SoNONUNICODE friend bool operator > (const SbString &str, const char *s);
00761
00769 SoNONUNICODE friend bool operator > (const char *s, const SbString &str);
00770
00778 SoNONUNICODE friend bool operator > (const SbString &str, const std::string &s);
00779
00787 SoNONUNICODE friend bool operator > (const std::string &s, const SbString &str);
00788
00796 SoNONUNICODE friend bool operator <= (const SbString &str, const char *s);
00797
00805 SoNONUNICODE friend bool operator <= (const char *s, const SbString &str);
00806
00814 SoNONUNICODE friend bool operator <= (const SbString &str, const std::string &s);
00815
00823 SoNONUNICODE friend bool operator <= (const std::string &s, const SbString &str);
00824
00832 SoNONUNICODE friend bool operator >= (const SbString &str, const char *s);
00833
00841 SoNONUNICODE friend bool operator >= (const char *s, const SbString &str);
00842
00850 SoNONUNICODE friend bool operator >= (const SbString &str, const std::string &s);
00851
00858 SoNONUNICODE friend bool operator >= (const std::string &s, const SbString &str);
00859
00861
00863
00867 SoNONUNICODE const SO_WCHAR_T *wgetString() const;
00868
00872 SoNONUNICODE const std::wstring &wgetSString() const;
00873
00877 SoNONUNICODE SbString &operator =(const SO_WCHAR_T *str);
00878
00882 SoNONUNICODE SbString &operator +=(const SO_WCHAR_T *str);
00883
00889 SoNONUNICODE friend int operator ==(const SbString &str, const SO_WCHAR_T *s);
00890
00896 SoNONUNICODE friend int operator ==(const SO_WCHAR_T *s, const SbString &str);
00897
00903 SoNONUNICODE friend int operator ==(const SbString &str, const std::wstring &s);
00904
00910 SoNONUNICODE friend int operator ==(const std::wstring &s, const SbString &str);
00911
00917 SoNONUNICODE friend int operator !=(const SbString &str, const SO_WCHAR_T *s);
00918
00924 SoNONUNICODE friend int operator !=(const SO_WCHAR_T *s, const SbString &str);
00925
00931 SoNONUNICODE friend bool operator < (const SbString &str, const SO_WCHAR_T *s);
00932
00938 SoNONUNICODE friend bool operator < (const SO_WCHAR_T *s, const SbString &str);
00939
00945 SoNONUNICODE friend bool operator < (const SbString &str, const std::wstring &s);
00946
00952 SoNONUNICODE friend bool operator < (const std::wstring &s, const SbString &str);
00953
00959 SoNONUNICODE friend bool operator > (const SbString &str, const SO_WCHAR_T *s);
00960
00966 SoNONUNICODE friend bool operator > (const SO_WCHAR_T *s, const SbString &str);
00967
00973 SoNONUNICODE friend bool operator > (const SbString &str, const std::wstring &s);
00974
00980 SoNONUNICODE friend bool operator > (const std::wstring &s, const SbString &str);
00981
00987 SoNONUNICODE friend bool operator <= (const SbString &str, const SO_WCHAR_T *s);
00988
00994 SoNONUNICODE friend bool operator <= (const SO_WCHAR_T *s, const SbString &str);
00995
01001 SoNONUNICODE friend bool operator <= (const SbString &str, const std::wstring &s);
01002
01008 SoNONUNICODE friend bool operator <= (const std::wstring &s, const SbString &str);
01009
01015 SoNONUNICODE friend bool operator >= (const SbString &str, const SO_WCHAR_T *s);
01016
01022 SoNONUNICODE friend bool operator >= (const SO_WCHAR_T *s, const SbString &str);
01023
01029 SoNONUNICODE friend bool operator >= (const SbString &str, const std::wstring &s);
01030
01036 SoNONUNICODE friend bool operator >= (const std::wstring &s, const SbString &str);
01037
01038 private:
01039
01045 SbString& fromUtf8( const char* str );
01046
01047
01053 const char* toUtf8() const;
01054
01055 static uint32_t hash(const char *s);
01056 static uint32_t hash(const SO_WCHAR_T *s);
01057
01058 static inline bool isDigit(const int c);
01059 static inline bool isSpace(const int c);
01060
01062 void split(std::vector<SbString>& tokens) const;
01063
01070 void setCapacity( int nBytes );
01071
01073 const std::string& nonUnicodeToStdString() const { return m_cdata; }
01074
01075 private:
01076
01077 static void initClass();
01078
01079 static void exitClass();
01080
01081 #ifdef _WIN32
01082 # define SB_LOCALE_DATA_TYPE _locale_t
01083 # define SB_LOCALE_ALLOC_FUNC _create_locale
01084 # define SB_LOCALE_FREE_FUNC _free_locale
01085 # define SB_LOCALE_STRTOD_FUNC _strtod_l
01086 # define SB_LOCALE_STRTOL_FUNC _strtol_l
01087 # define SB_LOCALE_SPRINTF_FUNC _sprintf_l
01088 # define SB_LOCALE_SNPRINTF_FUNC _snprintf_l
01089 # define SB_LOCALE_SCANF_FUNC _scanf_l
01090 # define SB_LOCALE_SSCANF_FUNC _sscanf_l
01091 # define SB_LOCALE_FSCANF_FUNC _fscanf_l
01092 # define SB_LOCALE_VPRINTF _vprintf_l
01093 # define SB_LOCALE_FPRINTF_FUNC _fprintf_l
01094 # define SB_LOCALE , (SB_LOCALE_DATA_TYPE)SbString::getLocaleObject()
01095 #else
01096 # define SB_LOCALE_DATA_TYPE locale_t
01097 # define SB_LOCALE_ALLOC_FUNC newlocale
01098 # define SB_LOCALE_FREE_FUNC freelocale
01099 # define SB_LOCALE_STRTOD_FUNC strtod
01100 # define SB_LOCALE_STRTOL_FUNC strtol
01101 # define SB_LOCALE_SPRINTF_FUNC ::sprintf
01102 # define SB_LOCALE_FPRINTF_FUNC fprintf
01103 # define SB_LOCALE_SNPRINTF_FUNC snprintf
01104 # define SB_LOCALE_SCANF_FUNC scanf
01105 # define SB_LOCALE_SSCANF_FUNC sscanf
01106 # define SB_LOCALE_FSCANF_FUNC fscanf
01107 # define SB_LOCALE_VPRINTF vprintf
01108 # define SB_LOCALE
01109 #endif
01110
01111 static float toFloat_l( const char* str, SbBool* ok = NULL );
01112
01113 static double toDouble_l( const char* str, SbBool* ok = NULL );
01114
01115 static long toLong_l( const char* str, SbBool* ok = NULL );
01116
01117 static void toString_l( char* str, const char* format, float num );
01118
01119 static void toString_l( char* str, const char* format, double num );
01120
01121 static void* getLocaleObject();
01122
01123
01124
01125 static SbString trimRight( const SbString & _str );
01126
01127
01128
01129 static SbString trimLeft( const SbString & _str );
01130
01131
01132
01133 static SbString trim( const SbString & _str );
01134
01135 private:
01136
01137
01138 static void buildLocaleCache();
01139 static bool localeCacheInitialized;
01140 static bool localeDigitCache[256];
01141 static bool localeSpaceCache[256];
01142 static char* s_tmpstr;
01143 static size_t s_tmpStrSize;
01144
01145 std::wstring m_data;
01146 std::string m_cdata;
01147
01148 mutable std::wstring m_tmpData;
01149 mutable std::string m_tmpCData;
01150
01151 bool m_isNull;
01152
01153
01154 #if !defined(_WIN32)
01155
01156
01157 mutable unsigned short* m_utf16Buffer;
01158 #endif
01159 };
01160
01161
01162 inline SbBool
01163 SbString::isEmpty() const
01164 {
01165 return (isNull() || (getLength() == 0));
01166 }
01167
01168 inline SbBool
01169 SbString::isNull() const
01170 {
01171 return m_isNull;
01172 }
01173
01174 inline bool SbString::isDigit(const int c)
01175 {
01176 if ((c < 0) || (c>=256))
01177 return false;
01178
01179 if (!localeCacheInitialized)
01180 buildLocaleCache();
01181 return localeDigitCache[c];
01182 }
01183
01184 inline bool SbString::isSpace(const int c)
01185 {
01186 if (!localeCacheInitialized)
01187 buildLocaleCache();
01188 return localeSpaceCache[c];
01189 }
01190
01191 #ifdef _WIN32
01192 #pragma warning(pop)
01193 #endif
01194
01195 #include <Inventor/SbName.h>
01196
01197 #endif // _SB_STRING_
01198
01199
01200