// Ryzom - MMORPG Framework // Copyright (C) 2010 Winch Gate Property Limited // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . #ifndef PDSLIB_STRING_H #define PDSLIB_STRING_H #include class CSString: public std::string { public: CSString() { } CSString(const char *s) { *(std::string *)this=s; } CSString(const std::string &s) { *(std::string *)this=s; } CSString(char c) { *(std::string *)this=c; } CSString(int i,const char *fmt="%d") { char buf[1024]; sprintf(buf,fmt,i); *this=buf; } CSString(unsigned u,const char *fmt="%u") { char buf[1024]; sprintf(buf,fmt,u); *this=buf; } CSString(double d,const char *fmt="%f") { char buf[1024]; sprintf(buf,fmt,d); *this=buf; } CSString(const char *s,const char *fmt) { char buf[1024]; sprintf(buf,fmt,s); *this=buf; } CSString(const std::string &s,const char *fmt) { char buf[1024]; sprintf(buf,fmt,s.c_str()); *this=buf; } char operator*() { if (empty()) return 0; return (*this)[0]; } // return the n right hand most characters of a string CSString right(unsigned count) const { if (count>=size()) return *this; return substr(size()-count); } // return the string minus the n right hand most characters of a string CSString rightCrop(unsigned count) const { if (count>=size()) return CSString(); return substr(0,size()-count); } // return the n left hand most characters of a string CSString left(unsigned count) const { return substr(0,count); } // return the string minus the n left hand most characters of a string CSString leftCrop(unsigned count) const { if (count>=size()) return CSString(); return substr(count); } // return sub string up to but not including first instance of given character CSString splitTo(char c,bool truncateThis=false) { unsigned i; CSString result; for (i=0;i='A' && (*this)[i]<='Z') || ((*this)[i]>='a' && (*this)[i]<='z') || ((*this)[i]>='0' && (*this)[i]<='9') || (*this)[i]=='_') { // copy out an alpha-numeric string for (;i<(*this).size() && ( ((*this)[i]>='A' && (*this)[i]<='Z') || ((*this)[i]>='a' && (*this)[i]<='z') || ((*this)[i]>='0' && (*this)[i]<='9') || (*this)[i]=='_') ;++i) result+=(*this)[i]; } else { // just take the first character of the input result=(*this)[i]; ++i; } // remove the result string from the input string if so desired if (truncateThis) { if (i(this)->firstWord(); } // return sub string up to but not including first instance of given character CSString tailFromFirstWord() const { CSString hold=*this; hold.firstWord(true); return hold; } // count the number of words (or quote delimited sub-strings) in a string unsigned countWords() const { unsigned count=0; CSString hold=strip(); while (!hold.empty()) { hold=hold.tailFromFirstWord().strip(); ++count; } return count; } // count the number of words (or quote delimited sub-strings) in a string CSString word(unsigned idx) const { CSString hold=strip(); for (unsigned count=0;count(this)->firstWordOrWords(); } // return sub string up to but not including first instance of given character CSString tailFromFirstWordOrWords() const { CSString hold=*this; hold.firstWordOrWords(true); return hold; } // count the number of words (or quote delimited sub-strings) in a string unsigned countWordOrWords() const { unsigned count=0; CSString hold=strip(); while (!hold.empty()) { hold=hold.tailFromFirstWordOrWords().strip(); ++count; } return count; } // count the number of words (or quote delimited sub-strings) in a string CSString wordOrWords(unsigned idx) const { CSString hold=strip(); for (unsigned count=0;count=0 && isWhiteSpace((*this)[j]); --j) {} for (i=0; i='a' && c<='z') c^=('a'^'A'); result+=c; } return result; } // making a lower case copy of a string CSString toLower() const { CSString result; std::string::const_iterator it; for (it=begin();it!=end();++it) { char c=(*it); if (c>='A' && c<='Z') c^=('a'^'A'); result+=c; } return result; } // replacing all occurences of one string with another CSString replace(const char *toFind,const char *replacement) const { // just bypass the problems that can cause a crash... if (toFind==NULL || *toFind==0) return *this; unsigned i,j; CSString result; for (i=0;isize()) return size(); unsigned i,j; for (i=startLocation;i bool atoi(C& result) const { result=::atoi(c_str()); return (result!=0 || *this=="0"); } unsigned atoi() const { return ::atoi(c_str()); } // a couple of handy atof routines... template bool atof(C& result) const { result=::atof(c_str()); return (result!=0 || *this=="0"); } double atof() const { return ::atof(c_str()); } // case insensitive string compare bool operator==(const std::string &other) const { return stricmp(c_str(),other.c_str())==0; } // case insesnsitive string compare bool operator!=(const std::string &other) const { return !(*this==other); } }; #endif