1 #ifndef COIN_SBSTRING_H 2 #define COIN_SBSTRING_H 3 4 /**************************************************************************\ 5 * Copyright (c) Kongsberg Oil & Gas Technologies AS 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are 10 * met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * Neither the name of the copyright holder nor the names of its 20 * contributors may be used to endorse or promote products derived from 21 * this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 \**************************************************************************/ 35 36 #include <cstdarg> 37 #include <cstdio> 38 39 #include <Inventor/system/inttypes.h> 40 #include <Inventor/C/base/string.h> 41 42 #ifdef COIN_INTERNAL 43 #define COIN_ALLOW_SBINTLIST 44 #include <Inventor/lists/SbIntList.h> 45 #undef COIN_ALLOW_SBINTLIST 46 #else 47 #include <Inventor/lists/SbIntList.h> 48 #endif // COIN_INTERNAL 49 50 // ************************************************************************* 51 52 class COIN_DLL_API SbString { 53 public: SbString(void)54 SbString(void) { cc_string_construct(&this->str); } 55 SbString(const char * s)56 SbString(const char * s) 57 { cc_string_construct(&this->str); cc_string_set_text(&this->str, s); } 58 SbString(const wchar_t * s)59 SbString(const wchar_t * s) 60 { cc_string_construct(&this->str); cc_string_set_wtext(&this->str, s); } 61 SbString(const char * s,int start,int end)62 SbString(const char * s, int start, int end) 63 { cc_string_construct(&this->str); cc_string_set_subtext(&this->str, s, start, end); } 64 SbString(const SbString & s)65 SbString(const SbString & s) 66 { cc_string_construct(&this->str); cc_string_set_string(&this->str, &s.str); } 67 SbString(const int digits)68 SbString(const int digits) 69 { cc_string_construct(&this->str); cc_string_set_integer(&this->str, digits); } 70 ~SbString()71 ~SbString() { cc_string_clean(&this->str); } 72 hash(void)73 uint32_t hash(void) const { return cc_string_hash(&this->str); } hash(const char * s)74 static uint32_t hash(const char * s) { return cc_string_hash_text(s); } 75 getLength(void)76 int getLength(void) const { return cc_string_length(&this->str); } 77 78 void makeEmpty(SbBool freeold = TRUE) 79 { 80 if ( freeold ) cc_string_clear(&this->str); 81 else cc_string_clear_no_free(&this->str); 82 } 83 getString(void)84 const char * getString(void) const { return cc_string_get_text(&this->str); } 85 86 SbString getSubString(int startidx, int endidx = -1) const 87 { 88 SbString s; 89 cc_string_set_subtext(&s.str, cc_string_get_text(&this->str), startidx, endidx); 90 return s; 91 } 92 void deleteSubString(int startidx, int endidx = -1) 93 { 94 cc_string_remove_substring(&this->str, startidx, endidx); 95 } 96 addIntString(const int value)97 void addIntString(const int value) { cc_string_append_integer(&this->str, value); } 98 99 char operator[](int index) const { return this->str.pointer[index]; } 100 101 SbString & operator=(const char * s) 102 { cc_string_set_text(&this->str, s); return *this; } 103 SbString & operator=(const SbString & s) 104 { cc_string_set_text(&this->str, s.str.pointer); return *this; } 105 106 SbString & operator+=(const char * s) 107 { cc_string_append_text(&this->str, s); return *this; } 108 SbString & operator+=(const SbString & s) 109 { cc_string_append_string(&this->str, &s.str); return *this; } 110 SbString & operator+=(const char c) 111 { cc_string_append_char(&this->str, c); return *this; } 112 113 int operator!(void) const { return ! cc_string_is(&this->str); } 114 115 int compareSubString(const char * text, int offset = 0) const 116 { return cc_string_compare_subtext(&this->str, text, offset); } 117 sprintf(const char * formatstr,...)118 SbString & sprintf(const char * formatstr, ...) 119 { 120 va_list args; va_start(args, formatstr); 121 cc_string_vsprintf(&this->str, formatstr, args); 122 va_end(args); return *this; 123 } vsprintf(const char * formatstr,va_list args)124 SbString & vsprintf(const char * formatstr, va_list args) 125 { cc_string_vsprintf(&this->str, formatstr, args); return *this; } 126 apply(char (* func)(char input))127 void apply(char (*func)(char input)) { 128 cc_string_apply(&this->str, reinterpret_cast<cc_apply_f>(func)); 129 } 130 131 int find(const SbString & s) const; 132 SbBool findAll(const SbString & s, SbIntList & found) const; 133 134 SbString lower() const; 135 SbString upper() const; 136 137 void print(std::FILE * fp) const; 138 139 friend int operator==(const SbString & sbstr, const char * s); 140 friend int operator==(const char * s, const SbString & sbstr); 141 friend int operator==(const SbString & str1, const SbString & str2); 142 friend int operator!=(const SbString & sbstr, const char * s); 143 friend int operator!=(const char * s, const SbString & sbstr); 144 friend int operator!=(const SbString & str1, const SbString & str2); 145 friend int operator<(const SbString & sbstr, const char * s); 146 friend int operator<(const char * s, const SbString & sbstr); 147 friend int operator<(const SbString & str1, const SbString & str2); 148 friend int operator>(const SbString & sbstr, const char * s); 149 friend int operator>(const char * s, const SbString & sbstr); 150 friend int operator>(const SbString & str1, const SbString & str2); 151 friend const SbString operator+(const SbString & str1, const SbString & str2); 152 friend const SbString operator+(const SbString & sbstr, const char * s); 153 friend const SbString operator+(const char * s, const SbString & sbstr); 154 155 private: 156 struct cc_string str; 157 }; 158 159 inline int operator==(const SbString & sbstr, const char * s) 160 { return (cc_string_compare_text(sbstr.str.pointer, s) == 0); } 161 inline int operator==(const char * s, const SbString & sbstr) 162 { return (cc_string_compare_text(s, sbstr.str.pointer) == 0); } 163 inline int operator==(const SbString & str1, const SbString & str2) 164 { return (cc_string_compare_text(str1.str.pointer, str2.str.pointer) == 0); } 165 166 inline int operator!=(const SbString & sbstr, const char * s) 167 { return (cc_string_compare_text(sbstr.str.pointer, s) != 0); } 168 inline int operator!=(const char * s, const SbString & sbstr) 169 { return (cc_string_compare_text(s, sbstr.str.pointer) != 0); } 170 inline int operator!=(const SbString & str1, const SbString & str2) 171 { return (cc_string_compare_text(str1.str.pointer, str2.str.pointer) != 0); } 172 173 inline int operator<(const SbString & sbstr, const char * s) 174 { return (cc_string_compare_text(sbstr.str.pointer, s) < 0); } 175 inline int operator<(const char * s, const SbString & sbstr) 176 { return (cc_string_compare_text(s, sbstr.str.pointer) < 0); } 177 inline int operator<(const SbString & str1, const SbString & str2) 178 { return (cc_string_compare_text(str1.str.pointer, str2.str.pointer) < 0); } 179 180 inline int operator>(const SbString & sbstr, const char * s) 181 { return (cc_string_compare_text(sbstr.str.pointer, s) > 0); } 182 inline int operator>(const char * s, const SbString & sbstr) 183 { return (cc_string_compare_text(s, sbstr.str.pointer) > 0); } 184 inline int operator>(const SbString & str1, const SbString & str2) 185 { return (cc_string_compare_text(str1.str.pointer, str2.str.pointer) > 0); } 186 187 inline const SbString operator+(const SbString & str1, const SbString & str2) 188 { 189 SbString newstr(str1); 190 newstr += str2; 191 return newstr; 192 } 193 inline const SbString operator+(const SbString & sbstr, const char * s) 194 { 195 SbString newstr(sbstr); 196 newstr += s; 197 return newstr; 198 } 199 inline const SbString operator+(const char * s, const SbString & sbstr) 200 { 201 SbString newstr(s); 202 newstr += sbstr; 203 return newstr; 204 } 205 206 #ifndef COIN_INTERNAL 207 // For Open Inventor compatibility. 208 #include <Inventor/SbName.h> 209 #endif // !COIN_INTERNAL 210 211 #endif // !COIN_SBSTRING_H 212