Use configured resolution for login/outgame/ingame
[ryzomcore.git] / ryzom / tools / phrase_generator / sstring.h
bloba86005a9527fa9628522601f8a416b726555bc5a
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef PDSLIB_STRING_H
19 #define PDSLIB_STRING_H
21 #include <string>
24 class CSString: public std::string
26 public:
27 CSString()
31 CSString(const char *s)
33 *(std::string *)this=s;
36 CSString(const std::string &s)
38 *(std::string *)this=s;
41 CSString(char c)
43 *(std::string *)this=c;
46 CSString(int i,const char *fmt="%d")
48 char buf[1024];
49 sprintf(buf,fmt,i);
50 *this=buf;
53 CSString(unsigned u,const char *fmt="%u")
55 char buf[1024];
56 sprintf(buf,fmt,u);
57 *this=buf;
60 CSString(double d,const char *fmt="%f")
62 char buf[1024];
63 sprintf(buf,fmt,d);
64 *this=buf;
67 CSString(const char *s,const char *fmt)
69 char buf[1024];
70 sprintf(buf,fmt,s);
71 *this=buf;
74 CSString(const std::string &s,const char *fmt)
76 char buf[1024];
77 sprintf(buf,fmt,s.c_str());
78 *this=buf;
81 char operator*()
83 if (empty())
84 return 0;
85 return (*this)[0];
88 // return the n right hand most characters of a string
89 CSString right(unsigned count) const
91 if (count>=size())
92 return *this;
93 return substr(size()-count);
96 // return the string minus the n right hand most characters of a string
97 CSString rightCrop(unsigned count) const
99 if (count>=size())
100 return CSString();
101 return substr(0,size()-count);
104 // return the n left hand most characters of a string
105 CSString left(unsigned count) const
107 return substr(0,count);
110 // return the string minus the n left hand most characters of a string
111 CSString leftCrop(unsigned count) const
113 if (count>=size())
114 return CSString();
115 return substr(count);
118 // return sub string up to but not including first instance of given character
119 CSString splitTo(char c,bool truncateThis=false)
121 unsigned i;
122 CSString result;
123 for (i=0;i<size() && (*this)[i]!=c;++i)
124 result+=(*this)[i];
126 // remove the result string from the input string if so desired
127 if (truncateThis)
129 if (i<size()-1)
130 (*this)=substr(i+1); // +1 to skip the separator character
131 else
132 clear();
135 return result;
138 // return sub string up to but not including first instance of given character
139 CSString splitTo(const char *s,bool truncateThis=false)
141 unsigned i;
142 CSString result;
143 for (i=0;i<size();++i)
145 // perform a quick string compare
146 int j;
147 for (j=0;s[j]!=0 && s[j]==(&((*this)[i]))[j];++j)
150 // if string compare matched then return result so far
151 if (s[j]==0)
153 // remove the result string from the input string if so desired
154 if (truncateThis)
156 if (i<size()-1)
157 (*this)=substr(i+1); // +1 to skip the separator character
158 else
159 clear();
162 return result;
164 result+=(*this)[i];
166 // we didn't find the separator string so we're returning a copy of the whole string
167 if (truncateThis)
168 clear();
169 return result;
172 // return sub string from character following first instance of given character on
173 CSString splitFrom(char c) const
175 CSString result;
176 std::string::const_iterator it;
177 for (it=begin();it!=end() && *it!=c;++it)
179 if (it!=end())
181 ++it;
182 for (;it!=end();++it)
183 result+=*it;
185 return result;
188 // return sub string from character following first instance of given character on
189 CSString splitFrom(const char *s) const
191 unsigned int i;
192 CSString result;
193 for (i=0;i<size();++i)
195 // perform a quick string compare
196 unsigned int j;
197 for (j=0;i+j<size() && s[j]!=0 && s[j]==(*this)[i+j];++j)
200 // if string compare matched then build and return a result
201 if (s[j]==0)
203 result=substr(i+j);
204 return result;
207 return result;
210 // behave like a s strtok() routine, returning the sun string extracted from (and removed from) *this
211 CSString strtok(const char *separators)
213 unsigned int i;
214 CSString result;
216 // skip leading junk
217 for (i=0;i<size();++i)
219 // look for the next character in the 'separator' character list supplied
220 unsigned j;
221 for (j=0;separators[j] && (*this)[i]!=separators[j];++j)
223 // if not found then we're at end of leading junk
224 if (!separators[j])
225 break;
228 // copy out everything up to the next separator character
229 for (;i<size();++i)
231 // look for the next character in the 'separator' character list supplied
232 unsigned j;
233 for (j=0;separators[j] && (*this)[i]!=separators[j];++j)
235 // if not found then we're at end of leading junk
236 if (separators[j])
237 break;
238 result+=(*this)[i];
241 // skip trailing junk
242 for (;i<size();++i)
244 // look for the next character in the 'separator' character list supplied
245 unsigned j;
246 for (j=0;separators[j] && (*this)[i]!=separators[j];++j)
248 // if not found then we're at end of leading junk
249 if (!separators[j])
250 break;
253 // delete the treated bit from this string
254 (*this)=substr(i);
256 return result;
259 // return first word (blank separated)
260 CSString firstWord(bool truncateThis=false)
262 CSString result;
263 unsigned i=0;
264 // skip white space
265 for (i=0;i<size() && isWhiteSpace((*this)[i]);++i)
268 if ( ((*this)[i]>='A' && (*this)[i]<='Z') || ((*this)[i]>='a' && (*this)[i]<='z') ||
269 ((*this)[i]>='0' && (*this)[i]<='9') || (*this)[i]=='_')
271 // copy out an alpha-numeric string
272 for (;i<(*this).size() &&
273 ( ((*this)[i]>='A' && (*this)[i]<='Z') || ((*this)[i]>='a' && (*this)[i]<='z') ||
274 ((*this)[i]>='0' && (*this)[i]<='9') || (*this)[i]=='_')
275 ;++i)
276 result+=(*this)[i];
278 else
280 // just take the first character of the input
281 result=(*this)[i];
282 ++i;
285 // remove the result string from the input string if so desired
286 if (truncateThis)
288 if (i<size())
289 (*this)=substr(i);
290 else
291 clear();
294 return result;
297 CSString firstWordConst() const
299 return const_cast<CSString *>(this)->firstWord();
302 // return sub string up to but not including first instance of given character
303 CSString tailFromFirstWord() const
305 CSString hold=*this;
306 hold.firstWord(true);
307 return hold;
310 // count the number of words (or quote delimited sub-strings) in a string
311 unsigned countWords() const
313 unsigned count=0;
314 CSString hold=strip();
315 while (!hold.empty())
317 hold=hold.tailFromFirstWord().strip();
318 ++count;
320 return count;
323 // count the number of words (or quote delimited sub-strings) in a string
324 CSString word(unsigned idx) const
326 CSString hold=strip();
328 for (unsigned count=0;count<idx;++count)
329 hold=hold.tailFromFirstWord().strip();
331 return hold.firstWord();
334 // return first word or quote-encompassed sub-string
335 CSString firstWordOrWords(bool truncateThis=false)
337 CSString hold=strip();
338 if (hold[0]!='\"')
339 return firstWord(truncateThis);
341 // the string is quote enclosed
342 CSString result;
343 unsigned i=1; // skip leading quote
344 // copy from character following opening quote to char preceding closing quote (or end of string)
345 while (i<hold.size() && hold[i]!='\"')
347 result+=hold[i];
348 ++i;
351 // remove the result string from the input string if so desired
352 if (truncateThis)
354 if (i<size()-1)
355 (*this)=substr(i+1); // +1 to skip the closing quote
356 else
357 clear();
360 return result;
363 CSString firstWordOrWordsConst() const
365 return const_cast<CSString *>(this)->firstWordOrWords();
368 // return sub string up to but not including first instance of given character
369 CSString tailFromFirstWordOrWords() const
371 CSString hold=*this;
372 hold.firstWordOrWords(true);
373 return hold;
376 // count the number of words (or quote delimited sub-strings) in a string
377 unsigned countWordOrWords() const
379 unsigned count=0;
380 CSString hold=strip();
381 while (!hold.empty())
383 hold=hold.tailFromFirstWordOrWords().strip();
384 ++count;
386 return count;
389 // count the number of words (or quote delimited sub-strings) in a string
390 CSString wordOrWords(unsigned idx) const
392 CSString hold=strip();
394 for (unsigned count=0;count<idx;++count)
395 hold=hold.tailFromFirstWordOrWords().strip();
397 return hold.firstWordOrWords();
400 // a handy utility routine for knowing if a character is a white space character or not
401 static bool isWhiteSpace(char c) { return c==' ' || c=='\t' || c=='\n' || c=='\r' || c==26; }
403 // return a copy of the string with leading and trainling spaces rmoved
404 CSString strip() const
406 CSString result;
407 int i,j;
408 for (j=size()-1; j>=0 && isWhiteSpace((*this)[j]); --j) {}
409 for (i=0; i<j && isWhiteSpace((*this)[i]); ++i) {}
410 for (; i<=j; ++i)
411 result+=(*this)[i];
412 return result;
415 // making an upper case copy of a string
416 CSString toUpper() const
418 CSString result;
419 std::string::const_iterator it;
420 for (it=begin();it!=end();++it)
422 char c=(*it);
423 if (c>='a' && c<='z')
424 c^=('a'^'A');
425 result+=c;
427 return result;
430 // making a lower case copy of a string
431 CSString toLower() const
433 CSString result;
434 std::string::const_iterator it;
435 for (it=begin();it!=end();++it)
437 char c=(*it);
438 if (c>='A' && c<='Z')
439 c^=('a'^'A');
440 result+=c;
442 return result;
445 // replacing all occurences of one string with another
446 CSString replace(const char *toFind,const char *replacement) const
448 // just bypass the problems that can cause a crash...
449 if (toFind==NULL || *toFind==0)
450 return *this;
452 unsigned i,j;
453 CSString result;
454 for (i=0;i<size();)
456 // string compare toFind against (*this)+i ...
457 for (j=0;toFind[j];++j)
458 if ((*this)[i+j]!=toFind[j])
459 break;
460 // if strings were identical then j reffers to ASCIIZ terminator at end of 'toFind'
461 if (toFind[j]==0)
463 if (replacement!=NULL)
464 result+=replacement;
465 i+=j;
467 else
469 result+=(*this)[i];
470 ++i;
473 return result;
476 // find index at which a sub-string starts - if sub-string not found then returns size()
477 unsigned find(const char *toFind,unsigned startLocation=0) const
479 // just bypass the problems that can cause a crash...
480 if (toFind==NULL || *toFind==0 || startLocation>size())
481 return size();
483 unsigned i,j;
484 for (i=startLocation;i<size();++i)
486 // string compare toFind against (*this)+i ...
487 for (j=0;toFind[j];++j)
488 if ((*this)[i+j]!=toFind[j])
489 break;
490 // if strings were identical then we're done
491 if (toFind[j]==0)
492 return i;
494 return i;
497 // return true if this contains given sub string
498 bool contains(const char *toFind) const
500 return find(toFind)!=size();
503 // a couple of handy atoi routines...
504 template <class C> bool atoi(C& result) const
506 result=::atoi(c_str());
507 return (result!=0 || *this=="0");
509 unsigned atoi() const
511 return ::atoi(c_str());
514 // a couple of handy atof routines...
515 template <class C> bool atof(C& result) const
517 result=::atof(c_str());
518 return (result!=0 || *this=="0");
520 double atof() const
522 return ::atof(c_str());
525 // case insensitive string compare
526 bool operator==(const std::string &other) const
528 return stricmp(c_str(),other.c_str())==0;
531 // case insesnsitive string compare
532 bool operator!=(const std::string &other) const
534 return !(*this==other);
538 #endif