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
00315 int find( const SbString& str ) const;
00316
00321 int rfind( const SbString& str ) const;
00322
00328 int findLast( const SbString& str ) const;
00329
00333 SbBool containsAny( const SbString& str ) const;
00334
00340 int findAny( const SbString& str ) const;
00341
00345 void replace( const SbString& patternStr, const SbString& substStr );
00346
00347
00348
00353 SbString lower() const;
00354
00359 SbString upper() const;
00360
00369 SbString& setNum( short value, int base = 10 );
00370
00374 SbString& setNum( unsigned short value, int base = 10 );
00375
00379 SbString& setNum( int value, int base = 10 );
00380
00384 SbString& setNum( unsigned int value, int base = 10 );
00385
00389 SbString& setNum( long value, int base = 10 );
00390
00394 SbString& setNum( unsigned long value, int base = 10 );
00395
00399 SbString& setNum( double value );
00400
00404 SbString& setNum( float value );
00405
00412 unsigned short toUShort( SbBool* ok = NULL ) const;
00413
00417 short toShort( SbBool* ok = NULL ) const;
00418
00422 unsigned int toUInt( SbBool* ok = NULL ) const;
00423
00427 int toInt( SbBool* ok = NULL ) const;
00428
00432 unsigned long toULong( SbBool* ok = NULL ) const;
00433
00437 long toLong( SbBool* ok = NULL ) const;
00438
00442 float toFloat( SbBool* ok = NULL ) const;
00443
00447 double toDouble( SbBool* ok = NULL ) const;
00448
00449
00450
00451
00452
00453
00457 SbBool isUnicode() const;
00458
00459
00460
00464 SbString& fromLatin1( const char* str );
00465
00469 SbString& fromUtf16( const unsigned short* str );
00470
00474 SbString& fromWideChar( const wchar_t* str );
00475
00476
00477
00481 const char* toLatin1() const;
00482
00486 unsigned short* toUtf16() const;
00487
00491 const wchar_t* toWideChar() const;
00492
00496 std::wstring toStdWString() const;
00497
00501 std::string toStdString() const;
00502
00503
00504
00508 SbString &operator +=( const SbString& str );
00509
00513 SbString &operator +=( const char *str );
00514
00518 SbString &operator =(const SbString &str);
00519
00523 SbString &operator =(const char *str);
00524
00525
00529 friend const SbString operator+( const SbString &s1, const SbString &s2 );
00530
00536 friend int operator ==(const SbString &str1, const SbString &str2);
00537
00543 friend int operator ==(const SbString &str, const char *s);
00544
00550 friend int operator ==(const char *s, const SbString &str);
00551
00557 friend int operator !=(const SbString &str1, const SbString &str2);
00558
00564 friend bool operator < (const SbString &str1, const SbString &str2);
00565
00571 friend bool operator > (const SbString &str1, const SbString &str2);
00572
00578 friend bool operator <= (const SbString &str1, const SbString &str2);
00579
00585 friend bool operator >= (const SbString &str1, const SbString &str2);
00586
00590 friend std::ostream& operator << (std::ostream& os, const SbString& str)
00591 {
00592 if (str.isUnicode())
00593 return os << str.toUtf8();
00594
00595 return os << str.toLatin1();
00596 }
00597
00598
00602 uint32_t hash() const;
00603
00604
00605
00613 SoNONUNICODE SbString( const int digitString );
00614
00621 SoNONUNICODE const char *getString() const;
00622
00629 SoNONUNICODE const std::string &getSString() const;
00630
00637 SoNONUNICODE SbString & sprintf(const char * cformat, ... );
00638
00644 SoNONUNICODE SbString & vprintf(const char * cformat, va_list arg);
00645
00652 SoNONUNICODE friend const SbString operator+( const SbString &s1, const char *s2 );
00653
00660 SoNONUNICODE friend const SbString operator+( const char *s1, const SbString &s2 );
00661
00667 SoNONUNICODE char operator[]( int i ) const;
00668
00674 SoNONUNICODE int operator !() const;
00675
00683 SoNONUNICODE friend int operator ==(const SbString &str, const std::string &s);
00684
00692 SoNONUNICODE friend int operator ==(const std::string &s, const SbString &str);
00693
00701 SoNONUNICODE friend int operator !=(const SbString &str, const char *s);
00702
00710 SoNONUNICODE friend int operator !=(const char *s, const SbString &str);
00711
00719 SoNONUNICODE friend bool operator < (const SbString &str, const char *s);
00720
00728 SoNONUNICODE friend bool operator < (const char *s, const SbString &str);
00729
00737 SoNONUNICODE friend bool operator < (const SbString &str, const std::string &s);
00738
00746 SoNONUNICODE friend bool operator < (const std::string &s, const SbString &str);
00747
00755 SoNONUNICODE friend bool operator > (const SbString &str, const char *s);
00756
00764 SoNONUNICODE friend bool operator > (const char *s, const SbString &str);
00765
00773 SoNONUNICODE friend bool operator > (const SbString &str, const std::string &s);
00774
00782 SoNONUNICODE friend bool operator > (const std::string &s, const SbString &str);
00783
00791 SoNONUNICODE friend bool operator <= (const SbString &str, const char *s);
00792
00800 SoNONUNICODE friend bool operator <= (const char *s, const SbString &str);
00801
00809 SoNONUNICODE friend bool operator <= (const SbString &str, const std::string &s);
00810
00818 SoNONUNICODE friend bool operator <= (const std::string &s, const SbString &str);
00819
00827 SoNONUNICODE friend bool operator >= (const SbString &str, const char *s);
00828
00836 SoNONUNICODE friend bool operator >= (const char *s, const SbString &str);
00837
00845 SoNONUNICODE friend bool operator >= (const SbString &str, const std::string &s);
00846
00853 SoNONUNICODE friend bool operator >= (const std::string &s, const SbString &str);
00854
00856
00858
00862 SoNONUNICODE const SO_WCHAR_T *wgetString() const;
00863
00867 SoNONUNICODE const std::wstring &wgetSString() const;
00868
00872 SoNONUNICODE SbString &operator =(const SO_WCHAR_T *str);
00873
00877 SoNONUNICODE SbString &operator +=(const SO_WCHAR_T *str);
00878
00884 SoNONUNICODE friend int operator ==(const SbString &str, const SO_WCHAR_T *s);
00885
00891 SoNONUNICODE friend int operator ==(const SO_WCHAR_T *s, const SbString &str);
00892
00898 SoNONUNICODE friend int operator ==(const SbString &str, const std::wstring &s);
00899
00905 SoNONUNICODE friend int operator ==(const std::wstring &s, const SbString &str);
00906
00912 SoNONUNICODE friend int operator !=(const SbString &str, const SO_WCHAR_T *s);
00913
00919 SoNONUNICODE friend int operator !=(const SO_WCHAR_T *s, const SbString &str);
00920
00926 SoNONUNICODE friend bool operator < (const SbString &str, const SO_WCHAR_T *s);
00927
00933 SoNONUNICODE friend bool operator < (const SO_WCHAR_T *s, const SbString &str);
00934
00940 SoNONUNICODE friend bool operator < (const SbString &str, const std::wstring &s);
00941
00947 SoNONUNICODE friend bool operator < (const std::wstring &s, const SbString &str);
00948
00954 SoNONUNICODE friend bool operator > (const SbString &str, const SO_WCHAR_T *s);
00955
00961 SoNONUNICODE friend bool operator > (const SO_WCHAR_T *s, const SbString &str);
00962
00968 SoNONUNICODE friend bool operator > (const SbString &str, const std::wstring &s);
00969
00975 SoNONUNICODE friend bool operator > (const std::wstring &s, const SbString &str);
00976
00982 SoNONUNICODE friend bool operator <= (const SbString &str, const SO_WCHAR_T *s);
00983
00989 SoNONUNICODE friend bool operator <= (const SO_WCHAR_T *s, const SbString &str);
00990
00996 SoNONUNICODE friend bool operator <= (const SbString &str, const std::wstring &s);
00997
01003 SoNONUNICODE friend bool operator <= (const std::wstring &s, const SbString &str);
01004
01010 SoNONUNICODE friend bool operator >= (const SbString &str, const SO_WCHAR_T *s);
01011
01017 SoNONUNICODE friend bool operator >= (const SO_WCHAR_T *s, const SbString &str);
01018
01024 SoNONUNICODE friend bool operator >= (const SbString &str, const std::wstring &s);
01025
01031 SoNONUNICODE friend bool operator >= (const std::wstring &s, const SbString &str);
01032
01033 private:
01034
01040 SbString& fromUtf8( const char* str );
01041
01042
01048 const char* toUtf8() const;
01049
01050 static uint32_t hash(const char *s);
01051 static uint32_t hash(const SO_WCHAR_T *s);
01052
01053 static inline bool isDigit(const int c);
01054 static inline bool isSpace(const int c);
01055
01057 void split(std::vector<SbString>& tokens) const;
01058
01065 void setCapacity( int nBytes );
01066
01068 const std::string& nonUnicodeToStdString() const { return m_cdata; }
01069
01070 private:
01071
01072 static void initClass();
01073
01074 static void exitClass();
01075
01076 #ifdef _WIN32
01077 # define SB_LOCALE_DATA_TYPE _locale_t
01078 # define SB_LOCALE_ALLOC_FUNC _create_locale
01079 # define SB_LOCALE_FREE_FUNC _free_locale
01080 # define SB_LOCALE_STRTOD_FUNC _strtod_l
01081 # define SB_LOCALE_STRTOL_FUNC _strtol_l
01082 # define SB_LOCALE_SPRINTF_FUNC _sprintf_l
01083 # define SB_LOCALE_SNPRINTF_FUNC _snprintf_l
01084 # define SB_LOCALE_SCANF_FUNC _scanf_l
01085 # define SB_LOCALE_SSCANF_FUNC _sscanf_l
01086 # define SB_LOCALE_FSCANF_FUNC _fscanf_l
01087 # define SB_LOCALE_VPRINTF _vprintf_l
01088 # define SB_LOCALE_FPRINTF_FUNC _fprintf_l
01089 # define SB_LOCALE , (SB_LOCALE_DATA_TYPE)SbString::getLocaleObject()
01090 #else
01091 # define SB_LOCALE_DATA_TYPE locale_t
01092 # define SB_LOCALE_ALLOC_FUNC newlocale
01093 # define SB_LOCALE_FREE_FUNC freelocale
01094 # define SB_LOCALE_STRTOD_FUNC strtod
01095 # define SB_LOCALE_STRTOL_FUNC strtol
01096 # define SB_LOCALE_SPRINTF_FUNC ::sprintf
01097 # define SB_LOCALE_FPRINTF_FUNC fprintf
01098 # define SB_LOCALE_SNPRINTF_FUNC snprintf
01099 # define SB_LOCALE_SCANF_FUNC scanf
01100 # define SB_LOCALE_SSCANF_FUNC sscanf
01101 # define SB_LOCALE_FSCANF_FUNC fscanf
01102 # define SB_LOCALE_VPRINTF vprintf
01103 # define SB_LOCALE
01104 #endif
01105
01106 static float toFloat_l( const char* str, SbBool* ok = NULL );
01107
01108 static double toDouble_l( const char* str, SbBool* ok = NULL );
01109
01110 static long toLong_l( const char* str, SbBool* ok = NULL );
01111
01112 static void toString_l( char* str, const char* format, float num );
01113
01114 static void toString_l( char* str, const char* format, double num );
01115
01116 static void* getLocaleObject();
01117
01118
01119
01120 static SbString trimRight( const SbString & _str );
01121
01122
01123
01124 static SbString trimLeft( const SbString & _str );
01125
01126
01127
01128 static SbString trim( const SbString & _str );
01129
01130 private:
01131
01132
01133 static void buildLocaleCache();
01134 static bool localeCacheInitialized;
01135 static bool localeDigitCache[256];
01136 static bool localeSpaceCache[256];
01137 static char* s_tmpstr;
01138 static size_t s_tmpStrSize;
01139
01140 std::wstring m_data;
01141 std::string m_cdata;
01142
01143 mutable std::wstring m_tmpData;
01144 mutable std::string m_tmpCData;
01145
01146 bool m_isNull;
01147
01148
01149 #if !defined(_WIN32)
01150
01151
01152 mutable unsigned short* m_utf16Buffer;
01153 #endif
01154 };
01155
01156
01157 inline SbBool
01158 SbString::isEmpty() const
01159 {
01160 return (isNull() || (getLength() == 0));
01161 }
01162
01163 inline SbBool
01164 SbString::isNull() const
01165 {
01166 return m_isNull;
01167 }
01168
01169 inline bool SbString::isDigit(const int c)
01170 {
01171 if ((c < 0) || (c>=256))
01172 return false;
01173
01174 if (!localeCacheInitialized)
01175 buildLocaleCache();
01176 return localeDigitCache[c];
01177 }
01178
01179 inline bool SbString::isSpace(const int c)
01180 {
01181 if (!localeCacheInitialized)
01182 buildLocaleCache();
01183 return localeSpaceCache[c];
01184 }
01185
01186 #ifdef _WIN32
01187 #pragma warning(pop)
01188 #endif
01189
01190 #include <Inventor/SbName.h>
01191
01192 #endif // _SB_STRING_
01193
01194
01195