CGR Localization
 All Classes Namespaces Files Functions Variables Macros Pages
sstring.h
1 // (C) 2004-2005 James R. Bruce, Carnegie Mellon University
2 // Licenced under the GNU General Public License (GPL) version 2,
3 // or alternately by a specific written agreement.
4 
5 #ifndef __SIMPLE_STRING_H__
6 #define __SIMPLE_STRING_H__
7 
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 
13 #define SSTRING_TEM template <class char_t>
14 #define SSTRING_FUN SimpleString<char_t>
15 
16 SSTRING_TEM
18  char_t *buf;
19  int size;
20 
21 public:
22  static const int LocNone = -1;
23 
24 public:
25  // creation and assignment calls
26  SimpleString()
27  {buf=NULL; size=0;}
28  SimpleString(const char *str)
29  {buf=NULL; size=0; copy(str);}
30  SimpleString(const char *str,int length)
31  {buf=NULL; size=0; copy(str,length);}
32  SimpleString(const SimpleString &str)
33  {buf=NULL; size=0; copy(str.buf);}
34  SimpleString(int expected_size)
35  {buf=NULL; size=0; allocate(expected_size);}
36  ~SimpleString()
37  {reset();}
38  void reset();
39  void clear()
40  {reset();}
41 
42  SimpleString &operator=(const SimpleString &str)
43  {copy(str.buf); return(*this);}
44  SimpleString &operator=(const char *str)
45  {copy(str); return(*this);}
46 
47 protected:
48  bool allocate(int _size);
49  bool reallocate(int _size);
50 public:
51  bool valid()
52  {return(buf != NULL);}
53 
54  bool copy(const char *str);
55  bool copy(const char *str,int length);
56  bool copy(const SimpleString &str)
57  {return(copy(str.buf));}
58  bool copy(const SimpleString &str,int start);
59  bool copy(const SimpleString &str,int start,int end);
60  bool remove(int start,int end);
61  void truncate(int len);
62 
63  bool add(const char *str);
64  bool add(const SimpleString &str)
65  {return(add(str.buf));}
66  SimpleString &operator+=(const char *str)
67  {add(str); return(*this);}
68  SimpleString &operator+=(const SimpleString &str)
69  {add(str); return(*this);}
70 
71  // string accessors
72  const char_t *operator()() const
73  {return(buf);}
74  int length() const
75  {return(strlen(buf));}
76  char_t operator[](int i) const
77  {return(buf[i]);}
78 
79  // comparison and search
80  bool operator==(const SimpleString &str) const
81  {return(strcmp(buf,str.buf) == 0);}
82  bool operator==(const char *cstr) const
83  {return(strcmp(buf,cstr) == 0);}
84  bool operator!=(const SimpleString &str) const
85  {return(strcmp(buf,str.buf) != 0);}
86  bool operator!=(const char *cstr) const
87  {return(strcmp(buf,cstr) != 0);}
88 
89  bool begins(const char *str) const
90  {return(memcmp(buf,str,strlen(str)) == 0);}
91 
92  int findNext(char_t key,int col) const;
93  int findFirst(char_t key) const
94  {return(findNext(key,0));}
95  int findLast(char_t key) const;
96 
97  bool getInt (int &col,int &val);
98  bool getFloat (int &col,float &val);
99  bool getDouble(int &col,double &val);
100 
101  // formatted printing into string
102  int printf(const char *format, ...)
103  __attribute__((format(__printf__,2,3)));
104  // file input
105  bool fgets(FILE *in,int maxsize);
106 };
107 
108 SSTRING_TEM
109 void SSTRING_FUN::reset()
110 {
111  delete[](buf);
112  buf = NULL;
113  size = 0;
114 }
115 
116 // make sure we have sufficient storage
117 SSTRING_TEM
118 bool SSTRING_FUN::allocate(int _size)
119 {
120  if(buf!=NULL && _size<size) return(true);
121 
122  delete[](buf);
123  buf = new char_t[_size];
124 
125  if(buf){
126  size = _size;
127  buf[0] = 0;
128  }else{
129  size = 0;
130  }
131 
132  return(buf != NULL);
133 }
134 
135 SSTRING_TEM
136 bool SSTRING_FUN::reallocate(int _size)
137 {
138  if(buf!=NULL && _size<size) return(true);
139 
140  char_t *nbuf = new char_t[_size];
141 
142  if(nbuf){
143  int ms = (size < _size)? size : _size;
144  memcpy(nbuf,buf,ms);
145  size = _size;
146  }else{
147  size = 0;
148  }
149 
150  delete[](buf);
151  buf = nbuf;
152 
153  return(buf != NULL);
154 }
155 
156 SSTRING_TEM
157 bool SSTRING_FUN::copy(const char *str)
158 {
159  int sz = strlen(str)+1;
160  if(!allocate(sz)) return(false);
161  memcpy(buf,str,sz*sizeof(char_t));
162  return(true);
163 }
164 
165 SSTRING_TEM
166 bool SSTRING_FUN::copy(const char *str,int length)
167 {
168  int sz = 0;
169  while(str[sz] && sz<length) sz++;
170  if(!allocate(sz+1)) return(false);
171  memcpy(buf,str,sz*sizeof(char_t));
172  buf[sz]=0;
173  return(true);
174 }
175 
176 SSTRING_TEM
177 bool SSTRING_FUN::copy(const SimpleString &str,int start)
178 {
179  int l = str.length();
180  if(start<0 || start>l) return(false);
181  int sz = l-start+1;
182  allocate(sz);
183  memcpy(buf,str.buf+start,sz*sizeof(char_t));
184  return(true);
185 }
186 
187 SSTRING_TEM
188 bool SSTRING_FUN::copy(const SimpleString &str,int start,int end)
189 {
190  int l = str.length();
191  if(start>end || start<0 || end>l) return(false);
192  int sz = end-start+1;
193  allocate(sz);
194  memcpy(buf,str.buf+start,sz*sizeof(char_t));
195  return(true);
196 }
197 
198 SSTRING_TEM
199 bool SSTRING_FUN::remove(int start,int end)
200 {
201  int l = length();
202  if(start>end || start<0 || end>l) return(false);
203  if(start==end) return(true);
204  int sz = l-end+1;
205  memmove(buf+start,buf+end,sz*sizeof(char_t));
206  return(true);
207 }
208 
209 SSTRING_TEM
210 void SSTRING_FUN::truncate(int len)
211 {
212  int l = length();
213  if(len>=0 && len<l) buf[len] = 0;
214 }
215 
216 SSTRING_TEM
217 bool SSTRING_FUN::add(const char *str)
218 {
219  int base = length();
220  int sz = strlen(str)+1;
221  if(!reallocate(base+sz)) return(false);
222  memcpy(buf+base,str,sz*sizeof(char_t));
223  return(true);
224 }
225 
226 SSTRING_TEM
227 int SSTRING_FUN::findNext(char_t key,int col) const
228 {
229  char_t ch;
230 
231  while((ch=buf[col])!=0 && ch!=key) col++;
232  return((ch == key)? col : LocNone);
233 }
234 
235 SSTRING_TEM
236 int SSTRING_FUN::findLast(char_t key) const
237 {
238  int i = 0, k = LocNone;
239  char_t ch;
240 
241  while((ch = buf[i]) != 0){
242  if(ch == key) k = i;
243  i++;
244  }
245 
246  return(k);
247 }
248 
249 SSTRING_TEM
250 bool SSTRING_FUN::getInt(int &col,int &val)
251 {
252  char *start = buf + col,*end;
253  int v = strtol(start,&end,10);
254  col = end - buf;
255  bool ok = (end != start);
256  if(ok) val = v;
257  return(ok);
258 }
259 
260 SSTRING_TEM
261 bool SSTRING_FUN::getFloat(int &col,float &val)
262 {
263  char *start = buf + col,*end;
264  float v = strtof(start,&end);
265  col = end - buf;
266  bool ok = (end != start);
267  if(ok) val = v;
268  return(ok);
269 }
270 
271 SSTRING_TEM
272 bool SSTRING_FUN::getDouble(int &col,double &val)
273 {
274  char *start = buf + col,*end;
275  double v = strtod(start,&end);
276  col = end - buf;
277  bool ok = (end != start);
278  if(ok) val = v;
279  return(ok);
280 }
281 
282 SSTRING_TEM
283 int SSTRING_FUN::printf(const char *format, ...)
284 {
285  va_list al;
286  int r = 0;
287 
288  va_start(al,format);
289  r = vsnprintf(buf,size,format,al);
290  va_end(al);
291  if(r < size) return(r);
292 
293  if(!allocate(r+1)) return(LocNone);
294 
295  va_start(al,format);
296  r = vsnprintf(buf,size,format,al);
297  va_end(al);
298  return(r);
299 }
300 
301 SSTRING_TEM
302 bool SSTRING_FUN::fgets(FILE *in,int maxsize)
303 {
304  if(!allocate(maxsize) || maxsize==0) return(false);
305  buf[0] = 0;
306  return(::fgets(buf,size,in) != NULL);
307 }
308 
311 
313 public:
314  bool operator()(const CharString &a,const CharString &b) const
315  {return(strcmp(a(),b()) < 0);}
316 };
317 
318 #endif