5 #ifndef __SIMPLE_STRING_H__
6 #define __SIMPLE_STRING_H__
13 #define SSTRING_TEM template <class char_t>
14 #define SSTRING_FUN SimpleString<char_t>
22 static const int LocNone = -1;
29 {buf=NULL; size=0; copy(str);}
31 {buf=NULL; size=0; copy(str,length);}
33 {buf=NULL; size=0; copy(str.buf);}
35 {buf=NULL; size=0; allocate(expected_size);}
43 {copy(str.buf);
return(*
this);}
45 {copy(str);
return(*
this);}
48 bool allocate(
int _size);
49 bool reallocate(
int _size);
52 {
return(buf != NULL);}
54 bool copy(
const char *str);
55 bool copy(
const char *str,
int length);
57 {
return(copy(str.buf));}
60 bool remove(
int start,
int end);
61 void truncate(
int len);
63 bool add(
const char *str);
65 {
return(add(str.buf));}
67 {add(str);
return(*
this);}
69 {add(str);
return(*
this);}
72 const char_t *operator()()
const
75 {
return(strlen(buf));}
76 char_t operator[](
int i)
const
81 {
return(strcmp(buf,str.buf) == 0);}
82 bool operator==(
const char *cstr)
const
83 {
return(strcmp(buf,cstr) == 0);}
85 {
return(strcmp(buf,str.buf) != 0);}
86 bool operator!=(
const char *cstr)
const
87 {
return(strcmp(buf,cstr) != 0);}
89 bool begins(
const char *str)
const
90 {
return(memcmp(buf,str,strlen(str)) == 0);}
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;
97 bool getInt (
int &col,
int &val);
98 bool getFloat (
int &col,
float &val);
99 bool getDouble(
int &col,
double &val);
102 int printf(
const char *format, ...)
103 __attribute__((format(__printf__,2,3)));
105 bool fgets(FILE *in,
int maxsize);
109 void SSTRING_FUN::reset()
118 bool SSTRING_FUN::allocate(
int _size)
120 if(buf!=NULL && _size<size)
return(
true);
123 buf =
new char_t[_size];
136 bool SSTRING_FUN::reallocate(
int _size)
138 if(buf!=NULL && _size<size)
return(
true);
140 char_t *nbuf =
new char_t[_size];
143 int ms = (size < _size)? size : _size;
157 bool SSTRING_FUN::copy(
const char *str)
159 int sz = strlen(str)+1;
160 if(!allocate(sz))
return(
false);
161 memcpy(buf,str,sz*
sizeof(char_t));
166 bool SSTRING_FUN::copy(
const char *str,
int length)
169 while(str[sz] && sz<length) sz++;
170 if(!allocate(sz+1))
return(
false);
171 memcpy(buf,str,sz*
sizeof(char_t));
177 bool SSTRING_FUN::copy(
const SimpleString &str,
int start)
179 int l = str.length();
180 if(start<0 || start>l)
return(
false);
183 memcpy(buf,str.buf+start,sz*
sizeof(char_t));
188 bool SSTRING_FUN::copy(
const SimpleString &str,
int start,
int end)
190 int l = str.length();
191 if(start>end || start<0 || end>l)
return(
false);
192 int sz = end-start+1;
194 memcpy(buf,str.buf+start,sz*
sizeof(char_t));
199 bool SSTRING_FUN::remove(
int start,
int end)
202 if(start>end || start<0 || end>l)
return(
false);
203 if(start==end)
return(
true);
205 memmove(buf+start,buf+end,sz*
sizeof(char_t));
210 void SSTRING_FUN::truncate(
int len)
213 if(len>=0 && len<l) buf[len] = 0;
217 bool SSTRING_FUN::add(
const char *str)
220 int sz = strlen(str)+1;
221 if(!reallocate(base+sz))
return(
false);
222 memcpy(buf+base,str,sz*
sizeof(char_t));
227 int SSTRING_FUN::findNext(char_t key,
int col)
const
231 while((ch=buf[col])!=0 && ch!=key) col++;
232 return((ch == key)? col : LocNone);
236 int SSTRING_FUN::findLast(char_t key)
const
238 int i = 0, k = LocNone;
241 while((ch = buf[i]) != 0){
250 bool SSTRING_FUN::getInt(
int &col,
int &val)
252 char *start = buf + col,*end;
253 int v = strtol(start,&end,10);
255 bool ok = (end != start);
261 bool SSTRING_FUN::getFloat(
int &col,
float &val)
263 char *start = buf + col,*end;
264 float v = strtof(start,&end);
266 bool ok = (end != start);
272 bool SSTRING_FUN::getDouble(
int &col,
double &val)
274 char *start = buf + col,*end;
275 double v = strtod(start,&end);
277 bool ok = (end != start);
283 int SSTRING_FUN::printf(
const char *format, ...)
289 r = vsnprintf(buf,size,format,al);
291 if(r < size)
return(r);
293 if(!allocate(r+1))
return(LocNone);
296 r = vsnprintf(buf,size,format,al);
302 bool SSTRING_FUN::fgets(FILE *in,
int maxsize)
304 if(!allocate(maxsize) || maxsize==0)
return(
false);
306 return(::fgets(buf,size,in) != NULL);
314 bool operator()(
const CharString &a,
const CharString &b)
const
315 {
return(strcmp(a(),b()) < 0);}