Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

clanstring.h

Go to the documentation of this file.
00001 /*
00002         $Id: clanstring.h,v 1.19 2001/03/20 01:40:05 plasmoid Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00016 
00017 #ifndef header_clanstring
00018 #define header_clanstring
00019 
00020 #include <string>
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <iostream>
00026 #include <stdio.h>
00027 
00028 #ifndef WIN32
00029 #include <cctype>
00030 #endif
00031 
00032 #ifdef BORLAND
00033 #include <ctype.h>
00034 #endif
00035 
00036 #ifdef __BEOS__
00037 extern char *fcvt (double, int, int *, int *);
00038 #endif
00039 
00040 class CL_String
00041 {
00042         std::string str;
00043 
00044         std::string int_to_string(const char *prefix, const int number)
00045         {
00046                 char buf[20];
00047                 sprintf(buf, "%d", number);
00048 
00049                 int len=0;
00050                 if (prefix!=NULL) len=strlen(prefix);
00051 
00052                 char *text=new char[len+strlen(buf)+1];
00053                 if (prefix!=NULL)
00054                 {
00055                         strcpy(text, prefix);
00056                         strcat(text, buf);
00057                 }
00058                 else
00059                 {
00060                         strcpy(text, buf);
00061                 }
00062 
00063                 std::string res(text);
00064                 delete[] text;
00065                 return res;
00066         }
00067         
00068         std::string float_to_string(const char *prefix, const float _float_number)
00069         {
00070                 char buf[25];
00071                 buf[0]=0;
00072 
00073                 int decimal, sign;
00074                 int precision=4;
00075                 char temp;
00076 
00077                 float float_number = _float_number;
00078 
00079                 const char *_float_buffer = fcvt(
00080                         float_number,
00081                         precision,
00082                         &decimal,
00083                         &sign);
00084                         
00085                 char *float_buffer = new char[strlen(_float_buffer)+1];
00086                 strcpy(float_buffer, _float_buffer);
00087 
00088                 if (sign) strcpy(buf, "-");
00089                 if (decimal<=0)
00090                 {
00091                         strcat(buf, "0.");
00092                         strcat(buf, float_buffer);
00093                 }
00094             else
00095                 {
00096                         temp=float_buffer[decimal];
00097                         float_buffer[decimal]=0;
00098                         strcat(buf, float_buffer);
00099                         float_buffer[decimal]=temp;
00100 
00101                         strcat(buf, ".");
00102                         strcat(buf, &float_buffer[decimal]);
00103                 }
00104     
00105                 int len=0;
00106                 if (prefix!=NULL) len=strlen(prefix);
00107 
00108                 char *text=new char[len+strlen(buf)+1];
00109                 if (prefix!=NULL)
00110                 {
00111                         strcpy(text, prefix);
00112                         strcat(text, buf);
00113                 }
00114                 else strcpy(text, buf);
00115 
00116                 std::string res(text);
00117                 delete[] text;
00118                 return res;
00119         }
00120 
00121         std::string append_string(const char *prefix, const char *postfix)
00122         {
00123                 int len=0;
00124                 if (prefix!=NULL) len=strlen(prefix);
00125                 if (postfix!=NULL) len+=strlen(postfix);
00126                 char *text=new char[len+1];
00127                 if (prefix!=NULL)
00128                 {
00129                         strcpy(text, prefix);
00130                         if (postfix!=NULL) strcat(text, postfix);
00131                 }
00132                 else if (postfix!=NULL)
00133                 {
00134                         strcpy(text, postfix);
00135                 }
00136 
00137                 std::string res(text);
00138                 delete[] text;
00139                 return res;
00140         }
00141 
00142 public:
00143         CL_String()
00144         {
00145         }
00146         
00147         CL_String(const std::string &_str)
00148         {
00149                 this->str = append_string(NULL, _str.c_str());
00150         }
00151 
00152         CL_String(const int value)
00153         {
00154                 str=int_to_string(NULL, value);
00155         }
00156 
00157         CL_String(const float float_value)
00158         {
00159                 str=float_to_string(NULL, float_value);
00160         }
00161 
00162         CL_String(const char *text)
00163         {
00164                 str=text;
00165         }
00166         
00167         CL_String(const CL_String &clone)
00168         {
00169                 str=clone.str;
00170         }
00171 
00172         CL_String(const CL_String &text1, const CL_String &text2)
00173         {
00174                 str=text1.str;
00175                 str.append(text2.str);
00176         }
00177 
00178         CL_String(const char *text1, const char *text2)
00179         {
00180                 str=text1;
00181                 str.append(text2);
00182         }
00183 
00184         CL_String(const char *text, int length)
00185         {
00186                 str.append(text, length);
00187         }
00188 
00189         CL_String operator+ (const CL_String &other_string)
00190         {
00191                 return CL_String(str, other_string.str);
00192         }
00193 
00194         CL_String operator+ (const char *other_text)
00195         {
00196                 return CL_String(str, other_text);
00197         }
00198 
00199         CL_String operator+ (const int number)
00200         {
00201                 return CL_String(int_to_string(str.c_str(), number));
00202         }
00203 
00204         CL_String operator+ (const float number)
00205         {
00206                 return CL_String(float_to_string(str.c_str(), number));
00207         }
00208 
00209         CL_String &operator+= (const CL_String &other_string)
00210         {
00211                 str.append(other_string.str);
00212                 return *this;
00213         }
00214 
00215         CL_String &operator+= (const char *other_text)
00216         {
00217                 str.append(other_text);
00218                 return *this;
00219         }
00220 
00221         CL_String &operator+= (const int number)
00222         {
00223                 str = int_to_string(str.c_str(), number);
00224                 return *this;
00225         }
00226 
00227         CL_String &operator+= (const float number)
00228         {
00229                 str = float_to_string(str.c_str(), number);
00230                 return *this;
00231         }
00232 
00233         CL_String &operator+= (double float_number)
00234         {
00235                 return operator+= ((float) float_number);
00236         }
00237 
00238         CL_String &operator= (const CL_String &other_string)
00239         {
00240                 str=other_string.str;
00241                 return *this;
00242         }
00243 
00244         CL_String &operator= (const char *other_text)
00245         {
00246                 str = other_text;
00247                 return *this;
00248         }
00249 
00250         CL_String &operator= (const int number)
00251         {
00252                 str=int_to_string(NULL, number);
00253                 return *this;
00254         }
00255 
00256         CL_String &operator= (const float float_number)
00257         {
00258                 str=float_to_string(NULL, float_number);
00259                 return *this;
00260         }
00261 
00262         CL_String &operator= (double float_number)
00263         {
00264                 return operator= ((float) float_number);
00265         }
00266 
00267         bool operator== (const CL_String &other_string)
00268         {
00269                 return str == other_string.str;
00270         }
00271 
00272         bool operator== (const char *other_string)
00273         {
00274                 return str == other_string;
00275         }
00276         
00277         bool operator!= (const CL_String &other_string)
00278         {
00279                 return !operator==(other_string);
00280         }
00281 
00282         bool operator!= (const char *other_text)
00283         {
00284                 return !operator==(other_text);
00285         }
00286 
00287         char *get_string()
00288         {
00289                 return (char *) str.c_str();
00290         }
00291 
00292         int get_length()
00293         {
00294                 return str.length();
00295         }
00296 
00297         int get_as_int()
00298         {
00299                 return atoi(str.c_str());
00300         }
00301 
00302         float get_as_float()
00303         {
00304                 return atof(str.c_str());
00305         }
00306 
00307         operator const char* ()
00308         {
00309                 return str.c_str();
00310         }
00311         
00312         operator std::string ()
00313         {
00314                 return str;
00315         }
00316 
00317         CL_String &operator<< (const CL_String &other_string)
00318         {
00319                 str.append(other_string.str);
00320                 return *this;
00321         }
00322 
00323         CL_String &operator<< (const char *other_string)
00324         {
00325                 str.append(other_string);
00326                 return *this;
00327         }
00328 
00329         CL_String &operator<< (const unsigned char *other_string)
00330         {
00331                 str.append((const char *) other_string);
00332                 return *this;
00333         }
00334 
00335         CL_String &operator<< (const unsigned short number)
00336         {
00337                 str=int_to_string(str.c_str(), number);
00338                 return *this;
00339         }
00340 
00341         CL_String &operator<< (const short number)
00342         {
00343                 str=int_to_string(str.c_str(), number);
00344                 return *this;
00345         }
00346 
00347         CL_String &operator<< (const unsigned int number)
00348         {
00349                 str=int_to_string(str.c_str(), number);
00350                 return *this;
00351         }
00352 
00353         CL_String &operator<< (const int number)
00354         {
00355                 str=int_to_string(str.c_str(), number);
00356                 return *this;
00357         }
00358 
00359         CL_String &operator<< (const float float_number)
00360         {
00361                 str=float_to_string(str.c_str(), float_number);
00362                 return *this;
00363         }
00364 
00365         CL_String &operator<< (double float_number)
00366         {
00367                 return operator<< ((float) float_number);
00368         }
00369 
00370         CL_String get_word(int word_no)
00371         {
00372                 int len=get_length();
00373                 int cur_word=-1;
00374                 int word_start=0;
00375 
00376                 int cur_state=0;
00377                 for (int i=0; i<len; i++)
00378                 {
00379                         if (cur_state == 0)
00380                         {
00381                                 if (str[i]!=32)
00382                                 {
00383                                         if (str[i]==34)
00384                                         {
00385                                                 word_start = i+1;
00386                                                 cur_state = 2;
00387                                         }
00388                                         else
00389                                         {
00390                                                 word_start = i;
00391                                                 cur_state = 1;
00392                                         }
00393                                         cur_word++;
00394                                 }
00395                         }
00396                         else if (cur_state == 1)
00397                         {
00398                                 if (str[i]==32)
00399                                 {
00400                                         if (cur_word == word_no) return mid(word_start, i-word_start);
00401                                         cur_state = 0;
00402                                 }
00403                         }
00404                         else if (cur_state == 2)
00405                         {
00406                                 if (str[i]==34)
00407                                 {
00408                                         if (cur_word == word_no) return mid(word_start, i-word_start);
00409                                         cur_state = 0;
00410                                 }
00411                         }
00412                 }
00413 
00414                 if (cur_word == word_no) return mid(word_start);
00415 
00416                 return CL_String("");
00417         }
00418 
00419         CL_String mid(int start, int len=-1)
00420         {
00421                 if (len==-1) len=get_length()-start;
00422                 return CL_String(std::string(str, start, len));
00423         }
00424 
00425         CL_String right(int len)
00426         {
00427                 int strlength = get_length();
00428                 if (strlength < len) return *this;
00429                 return CL_String(std::string(str, strlength-len, len));
00430         }
00431 
00432         int find(int character, int start_pos=0)
00433         {
00434                 const char *ptr = str.c_str();
00435                 char *pos = (char*)strchr(ptr+start_pos, character);
00436                 if (pos == NULL) return -1;
00437                 return int(pos-ptr);
00438         }
00439 
00440         int find_last(char character) const
00441         {
00442                 const char *temp=str.c_str();
00443                 int start_pos=str.length();
00444                 for(int i=start_pos;i;i--)
00445                         if(*(temp+i)==character)
00446                                 return i;
00447                 return -1;
00448         }
00449 
00450         void to_lower()
00451         {
00452                 int len = str.length();
00453                 for (int i=0; i<len; i++)
00454                 {
00455                         str[i] = tolower(str[i]);
00456                 }
00457         }
00458 
00459         void to_upper()
00460         {
00461                 int len = str.length();
00462                 for (int i=0; i<len; i++)
00463                 {
00464                         str[i] = toupper(str[i]);
00465                 }
00466         }
00467         
00468         bool case_compare(const char *other)
00469         {
00470                 if (other==NULL) return false;
00471 
00472 #ifdef WIN32
00473 #ifdef BORLAND
00474                 if (stricmp(str.c_str(), other)==0) return true;
00475 #else
00476                 if (_stricmp(str.c_str(), other)==0) return true;
00477 #endif
00478 #else
00479                 if (strcasecmp(str.c_str(), other)==0) return true;
00480 #endif
00481                 return false;
00482         }
00483 };
00484 
00485 #endif

Generated at Wed Apr 4 19:53:59 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001