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
00202 class SbString {
00203 public:
00204
00208 SbString();
00209
00213 SbString(const std::string &std_string);
00214
00218 SbString(const std::wstring &std_wstring);
00219
00223 SbString(const char *str);
00224
00230 SbString(const char *str, int start, int end);
00231
00237 SbString(const wchar_t *str, int start, int end);
00238
00239
00243 SbString(const SbString &str);
00244
00248 SbString( const wchar_t* str );
00249
00253 ~SbString();
00254
00259 static const int npos;
00260
00266 SbBool isEmpty() const;
00267
00272 SbBool isNull() const;
00273
00277 int getLength() const;
00278
00284 void makeEmpty( const SbBool freeOld = TRUE );
00285
00289 void makeNull();
00290
00296 SbString getSubString(int startChar, int endChar = -1) const;
00297
00303 void deleteSubString(int startChar, int endChar = -1);
00304
00308 SbBool contains( const SbString& str ) const;
00309
00313 bool endsWith( const SbString& str ) const;
00314
00319 int find( const SbString& str ) const;
00320
00325 int rfind( const SbString& str ) const;
00326
00332 int findLast( const SbString& str ) const;
00333
00337 SbBool containsAny( const SbString& str ) const;
00338
00344 int findAny( const SbString& str ) const;
00345
00349 void replace( const SbString& patternStr, const SbString& substStr );
00350
00351
00352
00357 SbString lower() const;
00358
00363 SbString upper() const;
00364
00373 SbString& setNum( short value, int base = 10 );
00374
00378 SbString& setNum( unsigned short value, int base = 10 );
00379
00383 SbString& setNum( int value, int base = 10 );
00384
00388 SbString& setNum( unsigned int value, int base = 10 );
00389
00393 SbString& setNum( long value, int base = 10 );
00394
00398 SbString& setNum( unsigned long value, int base = 10 );
00399
00403 SbString& setNum( double value );
00404
00408 SbString& setNum( float value );
00409
00416 unsigned short toUShort( SbBool* ok = NULL ) const;
00417
00421 short toShort( SbBool* ok = NULL ) const;
00422
00426 unsigned int toUInt( SbBool* ok = NULL ) const;
00427
00431 int toInt( SbBool* ok = NULL ) const;
00432
00436 unsigned long toULong( SbBool* ok = NULL ) const;
00437
00441 long toLong( SbBool* ok = NULL ) const;
00442
00446 float toFloat( SbBool* ok = NULL ) const;
00447
00451 double toDouble( SbBool* ok = NULL ) const;
00452
00453
00454
00455
00456
00457
00461 SbBool isUnicode() const;
00462
00463
00464
00468 SbString& fromLatin1( const char* str );
00469
00473 SbString& fromUtf16( const unsigned short* str );
00474
00478 SbString& fromWideChar( const wchar_t* str );
00479
00480
00481
00485 const char* toLatin1() const;
00486
00490 unsigned short* toUtf16() const;
00491
00495 const wchar_t* toWideChar() const;
00496
00500 std::wstring toStdWString() const;
00501
00505 std::string toStdString() const;
00506
00507
00508
00512 SbString &operator +=( const SbString& str );
00513
00517 SbString &operator +=( const char *str );
00518
00522 SbString &operator =(const SbString &str);
00523
00527 SbString &operator =(const char *str);
00528
00529
00533 friend const SbString operator+( const SbString &s1, const SbString &s2 );
00534
00540 friend int operator ==(const SbString &str1, const SbString &str2);
00541
00547 friend int operator ==(const SbString &str, const char *s);
00548
00554 friend int operator ==(const char *s, const SbString &str);
00555
00561 friend int operator !=(const SbString &str1, const SbString &str2);
00562
00568 friend bool operator < (const SbString &str1, const SbString &str2);
00569
00575 friend bool operator > (const SbString &str1, const SbString &str2);
00576
00582 friend bool operator <= (const SbString &str1, const SbString &str2);
00583
00589 friend bool operator >= (const SbString &str1, const SbString &str2);
00590
00594 friend std::ostream& operator << (std::ostream& os, const SbString& str)
00595 {
00596 if (str.isUnicode())
00597 return os << str.toUtf8();
00598
00599 return os << str.toLatin1();
00600 }
00601
00602
00606 uint32_t hash() const;
00607
00608
00609
00617 SoNONUNICODE SbString( const int digitString );
00618
00625 SoNONUNICODE const char *getString() const;
00626
00633 SoNONUNICODE const std::string &getSString() const;
00634
00641 SoNONUNICODE SbString & sprintf(const char * cformat, ... );
00642
00648 SoNONUNICODE SbString & vprintf(const char * cformat, va_list arg);
00649
00656 SoNONUNICODE friend const SbString operator+( const SbString &s1, const char *s2 );
00657
00664 SoNONUNICODE friend const SbString operator+( const char *s1, const SbString &s2 );
00665
00671 SoNONUNICODE char operator[]( int i ) const;
00672
00678 SoNONUNICODE int operator !() const;
00679
00687 SoNONUNICODE friend int operator ==(const SbString &str, const std::string &s);
00688
00696 SoNONUNICODE friend int operator ==(const std::string &s, const SbString &str);
00697
00705 SoNONUNICODE friend int operator !=(const SbString &str, const char *s);
00706
00714 SoNONUNICODE friend int operator !=(const char *s, const SbString &str);
00715
00723 SoNONUNICODE friend bool operator < (const SbString &str, const char *s);
00724
00732 SoNONUNICODE friend bool operator < (const char *s, const SbString &str);
00733
00741 SoNONUNICODE friend bool operator < (const SbString &str, const std::string &s);
00742
00750 SoNONUNICODE friend bool operator < (const std::string &s, const SbString &str);
00751
00759 SoNONUNICODE friend bool operator > (const SbString &str, const char *s);
00760
00768 SoNONUNICODE friend bool operator > (const char *s, const SbString &str);
00769
00777 SoNONUNICODE friend bool operator > (const SbString &str, const std::string &s);
00778
00786 SoNONUNICODE friend bool operator > (const std::string &s, const SbString &str);
00787
00795 SoNONUNICODE friend bool operator <= (const SbString &str, const char *s);
00796
00804 SoNONUNICODE friend bool operator <= (const char *s, const SbString &str);
00805
00813 SoNONUNICODE friend bool operator <= (const SbString &str, const std::string &s);
00814
00822 SoNONUNICODE friend bool operator <= (const std::string &s, const SbString &str);
00823
00831 SoNONUNICODE friend bool operator >= (const SbString &str, const char *s);
00832
00840 SoNONUNICODE friend bool operator >= (const char *s, const SbString &str);
00841
00849 SoNONUNICODE friend bool operator >= (const SbString &str, const std::string &s);
00850
00857 SoNONUNICODE friend bool operator >= (const std::string &s, const SbString &str);
00858
00860
00862
00866 SoNONUNICODE const SO_WCHAR_T *wgetString() const;
00867
00871 SoNONUNICODE const std::wstring &wgetSString() const;
00872
00876 SoNONUNICODE SbString &operator =(const SO_WCHAR_T *str);
00877
00881 SoNONUNICODE SbString &operator +=(const SO_WCHAR_T *str);
00882
00888 SoNONUNICODE friend int operator ==(const SbString &str, const SO_WCHAR_T *s);
00889
00895 SoNONUNICODE friend int operator ==(const SO_WCHAR_T *s, const SbString &str);
00896
00902 SoNONUNICODE friend int operator ==(const SbString &str, const std::wstring &s);
00903
00909 SoNONUNICODE friend int operator ==(const std::wstring &s, const SbString &str);
00910
00916 SoNONUNICODE friend int operator !=(const SbString &str, const SO_WCHAR_T *s);
00917
00923 SoNONUNICODE friend int operator !=(const SO_WCHAR_T *s, const SbString &str);
00924
00930 SoNONUNICODE friend bool operator < (const SbString &str, const SO_WCHAR_T *s);
00931
00937 SoNONUNICODE friend bool operator < (const SO_WCHAR_T *s, const SbString &str);
00938
00944 SoNONUNICODE friend bool operator < (const SbString &str, const std::wstring &s);
00945
00951 SoNONUNICODE friend bool operator < (const std::wstring &s, const SbString &str);
00952
00958 SoNONUNICODE friend bool operator > (const SbString &str, const SO_WCHAR_T *s);
00959
00965 SoNONUNICODE friend bool operator > (const SO_WCHAR_T *s, const SbString &str);
00966
00972 SoNONUNICODE friend bool operator > (const SbString &str, const std::wstring &s);
00973
00979 SoNONUNICODE friend bool operator > (const std::wstring &s, const SbString &str);
00980
00986 SoNONUNICODE friend bool operator <= (const SbString &str, const SO_WCHAR_T *s);
00987
00993 SoNONUNICODE friend bool operator <= (const SO_WCHAR_T *s, const SbString &str);
00994
01000 SoNONUNICODE friend bool operator <= (const SbString &str, const std::wstring &s);
01001
01007 SoNONUNICODE friend bool operator <= (const std::wstring &s, const SbString &str);
01008
01014 SoNONUNICODE friend bool operator >= (const SbString &str, const SO_WCHAR_T *s);
01015
01021 SoNONUNICODE friend bool operator >= (const SO_WCHAR_T *s, const SbString &str);
01022
01028 SoNONUNICODE friend bool operator >= (const SbString &str, const std::wstring &s);
01029
01035 SoNONUNICODE friend bool operator >= (const std::wstring &s, const SbString &str);
01036
01037 private:
01038
01044 SbString& fromUtf8( const char* str );
01045
01046
01052 const char* toUtf8() const;
01053
01054 static uint32_t hash(const char *s);
01055 static uint32_t hash(const SO_WCHAR_T *s);
01056
01057 static inline bool isDigit(const int c);
01058 static inline bool isSpace(const int c);
01059
01061 void split(std::vector<SbString>& tokens) const;
01062
01069 void setCapacity( int nBytes );
01070
01072 const std::string& nonUnicodeToStdString() const { return m_cdata; }
01073
01074 private:
01075
01076 static void initClass();
01077
01078 static void exitClass();
01079
01080 #ifdef _WIN32
01081 # define SB_LOCALE_DATA_TYPE _locale_t
01082 # define SB_LOCALE_ALLOC_FUNC _create_locale
01083 # define SB_LOCALE_FREE_FUNC _free_locale
01084 # define SB_LOCALE_STRTOD_FUNC _strtod_l
01085 # define SB_LOCALE_STRTOL_FUNC _strtol_l
01086 # define SB_LOCALE_SPRINTF_FUNC _sprintf_l
01087 # define SB_LOCALE_SNPRINTF_FUNC _snprintf_l
01088 # define SB_LOCALE_SCANF_FUNC _scanf_l
01089 # define SB_LOCALE_SSCANF_FUNC _sscanf_l
01090 # define SB_LOCALE_FSCANF_FUNC _fscanf_l
01091 # define SB_LOCALE_VPRINTF _vprintf_l
01092 # define SB_LOCALE_FPRINTF_FUNC _fprintf_l
01093 # define SB_LOCALE , (SB_LOCALE_DATA_TYPE)SbString::getLocaleObject()
01094 #else
01095 # define SB_LOCALE_DATA_TYPE locale_t
01096 # define SB_LOCALE_ALLOC_FUNC newlocale
01097 # define SB_LOCALE_FREE_FUNC freelocale
01098 # define SB_LOCALE_STRTOD_FUNC strtod
01099 # define SB_LOCALE_STRTOL_FUNC strtol
01100 # define SB_LOCALE_SPRINTF_FUNC ::sprintf
01101 # define SB_LOCALE_FPRINTF_FUNC fprintf
01102 # define SB_LOCALE_SNPRINTF_FUNC snprintf
01103 # define SB_LOCALE_SCANF_FUNC scanf
01104 # define SB_LOCALE_SSCANF_FUNC sscanf
01105 # define SB_LOCALE_FSCANF_FUNC fscanf
01106 # define SB_LOCALE_VPRINTF vprintf
01107 # define SB_LOCALE
01108 #endif
01109
01110 static float toFloat_l( const char* str, SbBool* ok = NULL );
01111
01112 static double toDouble_l( const char* str, SbBool* ok = NULL );
01113
01114 static long toLong_l( const char* str, SbBool* ok = NULL );
01115
01116 static void toString_l( char* str, const char* format, float num );
01117
01118 static void toString_l( char* str, const char* format, double num );
01119
01120 static void* getLocaleObject();
01121
01122
01123
01124 static SbString trimRight( const SbString & _str );
01125
01126
01127
01128 static SbString trimLeft( const SbString & _str );
01129
01130
01131
01132 static SbString trim( const SbString & _str );
01133
01134 private:
01135
01136
01137 static void buildLocaleCache();
01138 static bool localeCacheInitialized;
01139 static bool localeDigitCache[256];
01140 static bool localeSpaceCache[256];
01141 static char* s_tmpstr;
01142 static size_t s_tmpStrSize;
01143
01144 std::wstring m_data;
01145 std::string m_cdata;
01146
01147 mutable std::wstring m_tmpData;
01148 mutable std::string m_tmpCData;
01149
01150 bool m_isNull;
01151
01152
01153 #if !defined(_WIN32)
01154
01155
01156 mutable unsigned short* m_utf16Buffer;
01157 #endif
01158 };
01159
01160
01161 inline SbBool
01162 SbString::isEmpty() const
01163 {
01164 return (isNull() || (getLength() == 0));
01165 }
01166
01167 inline SbBool
01168 SbString::isNull() const
01169 {
01170 return m_isNull;
01171 }
01172
01173 inline bool SbString::isDigit(const int c)
01174 {
01175 if ((c < 0) || (c>=256))
01176 return false;
01177
01178 if (!localeCacheInitialized)
01179 buildLocaleCache();
01180 return localeDigitCache[c];
01181 }
01182
01183 inline bool SbString::isSpace(const int c)
01184 {
01185 if (!localeCacheInitialized)
01186 buildLocaleCache();
01187 return localeSpaceCache[c];
01188 }
01189
01190 #ifdef _WIN32
01191 #pragma warning(pop)
01192 #endif
01193
01194 #include <Inventor/SbName.h>
01195
01196 #endif // _SB_STRING_
01197
01198
01199