Coin Logo http://www.coin3d.org/
http://www.kongsberg.com/kogt/

SbString.h
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 <stdarg.h>
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:
54  SbString(void) { cc_string_construct(&this->str); }
55 
56  SbString(const char * s)
57  { cc_string_construct(&this->str); cc_string_set_text(&this->str, s); }
58 
59  SbString(const char * s, int start, int end)
60  { cc_string_construct(&this->str); cc_string_set_subtext(&this->str, s, start, end); }
61 
62  SbString(const SbString & s)
63  { cc_string_construct(&this->str); cc_string_set_string(&this->str, &s.str); }
64 
65  SbString(const int digits)
66  { cc_string_construct(&this->str); cc_string_set_integer(&this->str, digits); }
67 
68  ~SbString() { cc_string_clean(&this->str); }
69 
70  uint32_t hash(void) const { return cc_string_hash(&this->str); }
71  static uint32_t hash(const char * s) { return cc_string_hash_text(s); }
72 
73  int getLength(void) const { return cc_string_length(&this->str); }
74 
75  void makeEmpty(SbBool freeold = TRUE)
76  {
77  if ( freeold ) cc_string_clear(&this->str);
78  else cc_string_clear_no_free(&this->str);
79  }
80 
81  const char * getString(void) const { return cc_string_get_text(&this->str); }
82 
83  SbString getSubString(int startidx, int endidx = -1) const
84  {
85  SbString s;
86  cc_string_set_subtext(&s.str, cc_string_get_text(&this->str), startidx, endidx);
87  return s;
88  }
89  void deleteSubString(int startidx, int endidx = -1)
90  {
91  cc_string_remove_substring(&this->str, startidx, endidx);
92  }
93 
94  void addIntString(const int value) { cc_string_append_integer(&this->str, value); }
95 
96  char operator[](int index) const { return this->str.pointer[index]; }
97 
98  SbString & operator=(const char * s)
99  { cc_string_set_text(&this->str, s); return *this; }
101  { cc_string_set_text(&this->str, s.str.pointer); return *this; }
102 
103  SbString & operator+=(const char * s)
104  { cc_string_append_text(&this->str, s); return *this; }
106  { cc_string_append_string(&this->str, &s.str); return *this; }
107  SbString & operator+=(const char c)
108  { cc_string_append_char(&this->str, c); return *this; }
109 
110  int operator!(void) const { return ! cc_string_is(&this->str); }
111 
112  int compareSubString(const char * text, int offset = 0) const
113  { return cc_string_compare_subtext(&this->str, text, offset); }
114 
115  SbString & sprintf(const char * formatstr, ...)
116  {
117  va_list args; va_start(args, formatstr);
118  cc_string_vsprintf(&this->str, formatstr, args);
119  va_end(args); return *this;
120  }
121  SbString & vsprintf(const char * formatstr, va_list args)
122  { cc_string_vsprintf(&this->str, formatstr, args); return *this; }
123 
124  void apply(char (*func)(char input)) {
125  cc_string_apply(&this->str, reinterpret_cast<cc_apply_f>(func));
126  }
127 
128  int find(const SbString & s) const;
129  SbBool findAll(const SbString & s, SbIntList & found) const;
130 
131  SbString lower() const;
132  SbString upper() const;
133 
134  void print(std::FILE * fp) const;
135 
136  friend int operator==(const SbString & sbstr, const char * s);
137  friend int operator==(const char * s, const SbString & sbstr);
138  friend int operator==(const SbString & str1, const SbString & str2);
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 const SbString operator+(const SbString & str1, const SbString & str2);
143  friend const SbString operator+(const SbString & sbstr, const char * s);
144  friend const SbString operator+(const char * s, const SbString & sbstr);
145 
146 private:
147  struct cc_string str;
148 };
149 
150 inline int operator==(const SbString & sbstr, const char * s)
151 { return (cc_string_compare_text(sbstr.str.pointer, s) == 0); }
152 inline int operator==(const char * s, const SbString & sbstr)
153 { return (cc_string_compare_text(s, sbstr.str.pointer) == 0); }
154 inline int operator==(const SbString & str1, const SbString & str2)
155 { return (cc_string_compare_text(str1.str.pointer, str2.str.pointer) == 0); }
156 
157 inline int operator!=(const SbString & sbstr, const char * s)
158 { return (cc_string_compare_text(sbstr.str.pointer, s) != 0); }
159 inline int operator!=(const char * s, const SbString & sbstr)
160 { return (cc_string_compare_text(s, sbstr.str.pointer) != 0); }
161 inline int operator!=(const SbString & str1, const SbString & str2)
162 { return (cc_string_compare_text(str1.str.pointer, str2.str.pointer) != 0); }
163 
164 inline const SbString operator+(const SbString & str1, const SbString & str2)
165 {
166  SbString newstr(str1);
167  newstr += str2;
168  return newstr;
169 }
170 inline const SbString operator+(const SbString & sbstr, const char * s)
171 {
172  SbString newstr(sbstr);
173  newstr += s;
174  return newstr;
175 }
176 inline const SbString operator+(const char * s, const SbString & sbstr)
177 {
178  SbString newstr(s);
179  newstr += sbstr;
180  return newstr;
181 }
182 
183 #ifndef COIN_INTERNAL
184 // For Open Inventor compatibility.
185 #include <Inventor/SbName.h>
186 #endif // !COIN_INTERNAL
187 
188 #endif // !COIN_SBSTRING_H

Copyright © by Kongsberg Oil & Gas Technologies. All rights reserved.

Generated on Sat Oct 26 2013 23:25:56 for Coin by Doxygen 1.8.4.