Open Inventor Reference
|
00001 /* 00002 * 00003 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * Further, this software is distributed without any warranty that it is 00016 * free of the rightful claim of any third person regarding infringement 00017 * or the like. Any license provided herein, whether implied or 00018 * otherwise, applies only to this software file. Patent licenses, if 00019 * any, provided herein do not apply to combinations of this program with 00020 * other software, or any other product whatsoever. 00021 * 00022 * You should have received a copy of the GNU Lesser General Public 00023 * License along with this library; if not, write to the Free Software 00024 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00025 * 00026 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, 00027 * Mountain View, CA 94043, or: 00028 * 00029 * http://www.sgi.com 00030 * 00031 * For further information regarding this notice, see: 00032 * 00033 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ 00034 * 00035 */ 00036 00037 00038 /* 00039 * Copyright (C) 1990,91 Silicon Graphics, Inc. 00040 * 00041 _______________________________________________________________________ 00042 ______________ S I L I C O N G R A P H I C S I N C . ____________ 00043 | 00044 | $Revision: 1.1.1.1 $ 00045 | 00046 | Description: 00047 | This file contains definitions of the SbString and SbName 00048 | classes, which are useful variations on our friend, the 00049 | character string. 00050 | 00051 | Author(s) : Paul S. Strauss, Nick Thompson 00052 | 00053 ______________ S I L I C O N G R A P H I C S I N C . ____________ 00054 _______________________________________________________________________ 00055 */ 00056 00057 #ifndef _SB_STRING_ 00058 #define _SB_STRING_ 00059 00060 #include <Inventor/system/SbSystem.h> 00061 #include <Inventor/SbBasic.h> 00062 00063 #include <string.h> 00064 00065 00068 00078 00079 00080 class INVENTOR_API SbString { 00081 public: 00082 00084 SbString() { string = staticStorage; 00085 string[0] = '\0'; } 00086 00088 SbString(const char *str) { string = staticStorage; 00089 *this = str; } 00090 00092 SbString(const char *str, int start, int end); 00093 00095 SbString(const SbString &str) { string = staticStorage; 00096 *this = str.string; } 00097 00106 SbString(int digitString); 00107 00109 ~SbString(); 00110 00112 uint32_t hash() { return SbString::hash(string); } 00113 00115 int getLength() const { return static_cast<int>(strlen(string)); } 00116 00119 void makeEmpty(SbBool freeOld = TRUE); 00120 00122 const char * getString() const { return string; } 00123 00127 SbString getSubString(int startChar, int endChar = -1) const; 00128 00132 void deleteSubString(int startChar, int endChar = -1); 00133 00135 SbString & operator =(const char *str); 00137 SbString & operator =(const SbString &str) 00138 { return (*this = str.string); } 00139 00141 SbString & operator +=(const char *str); 00142 00144 SbString & operator +=(const SbString &str); 00145 00147 int operator !() const { return (string[0] == '\0'); } 00148 00150 friend INVENTOR_API int operator ==(const SbString &str, const char *s); 00151 00152 friend INVENTOR_API int operator ==(const char *s, const SbString &str) 00153 { return (str == s); } 00154 00155 00156 friend INVENTOR_API int operator ==(const SbString &str1, const SbString &str2) 00157 { return (str1 == str2.string); } 00158 00160 friend INVENTOR_API int operator !=(const SbString &str, const char *s); 00161 00162 friend INVENTOR_API int operator !=(const char *s, const SbString &str) 00163 { return (str != s); } 00164 00165 friend INVENTOR_API int operator !=(const SbString &str1, 00166 const SbString &str2) 00167 { return (str1 != str2.string); } 00168 00170 friend INVENTOR_API int operator <(const SbString &str, const char *s); 00171 00172 friend INVENTOR_API int operator <(const char *s, const SbString &str) 00173 { return (str < s); } 00174 00175 friend INVENTOR_API int operator <(const SbString &str1, const SbString &str2) 00176 { return (str1 < str2.string); } 00177 00178 SoINTERNAL public: 00179 00181 static uint32_t hash(const char *s); // Hash function 00182 00183 private: 00184 char *string; 00185 int storageSize; 00186 00188 #define SB_STRING_STATIC_STORAGE_SIZE 32 00189 char staticStorage[SB_STRING_STATIC_STORAGE_SIZE]; 00190 00191 void expand(int bySize); 00192 }; 00193 00201 00202 00203 SoINTERNAL class INVENTOR_API SbNameEntry { 00204 00205 public: 00207 SbBool isEmpty() const { return (string[0] == '\0'); } 00208 00210 SbBool isEqual(const char *s) const 00211 { return (string[0] == s[0] && ! strcmp(string, s)); } 00212 00213 private: 00214 static int nameTableSize; 00215 static SbNameEntry **nameTable; 00216 static struct SbNameChunk *chunk; 00217 00218 const char *string; 00219 uint32_t hashValue; 00220 SbNameEntry *next; 00221 00223 static void initClass(); 00224 00226 SbNameEntry(const char *s, uint32_t h, SbNameEntry *n) 00227 { string = s; hashValue = h; next = n; } 00228 00230 static const SbNameEntry * insert(const char *s); 00231 00232 friend INVENTOR_API class SbName; 00233 }; 00234 00235 00236 00239 00257 00258 00259 class INVENTOR_API SbName { 00260 public: 00261 00263 SbName(); 00264 00266 SbName(const char *s) { entry = SbNameEntry::insert(s); } 00267 00269 SbName(const SbString &s) { entry = SbNameEntry::insert(s.getString()); } 00270 00272 SbName(const SbName &n) { entry = n.entry; } 00273 00275 ~SbName() {} 00276 00278 const char *getString() const { return entry->string; } 00279 00281 int getLength() const { return static_cast<int>(strlen(entry->string)); } 00282 00285 static SbBool isIdentStartChar(char c); 00286 00289 static SbBool isIdentChar(char c); 00290 00293 static SbBool isBaseNameStartChar(char c); 00294 00297 static SbBool isBaseNameChar(char c); 00298 00300 int operator !() const { return entry->isEmpty(); } 00301 00303 friend INVENTOR_API int operator ==(const SbName &n, const char *s) 00304 { return n.entry->isEqual(s); } 00305 00306 friend INVENTOR_API int operator ==(const char *s, const SbName &n) 00307 { return n.entry->isEqual(s); } 00308 00309 friend INVENTOR_API int operator ==(const SbName &n1, const SbName &n2) 00310 { return n1.entry == n2.entry; } 00311 00313 friend INVENTOR_API int operator !=(const SbName &n, const char *s) 00314 { return ! n.entry->isEqual(s); } 00315 00316 friend INVENTOR_API int operator !=(const char *s, const SbName &n) 00317 { return ! n.entry->isEqual(s); } 00318 00319 friend INVENTOR_API int operator !=(const SbName &n1, const SbName &n2) 00320 { return n1.entry != n2.entry; } 00321 00322 private: 00323 const SbNameEntry *entry; 00324 }; 00325 00326 #endif /* _SB_STRING_ */