CLASS
1.1
|
00001 #ifndef _STRINGLINE_ 00002 #define _STRINGLINE_ 00003 00004 #include <string> 00005 #include <sstream> 00006 #include <iostream> 00007 #include <algorithm> 00008 #include <cctype> 00009 using namespace std; 00015 // Class extracting fields from a string / line. 00049 class StringLine 00050 { 00051 public: 00052 // Find the next word in a line. 00062 static string NextWord(string Line,int &start,char sep=' ', char alt_sep='\0'); 00063 // Find the previous word in a line. 00073 static string PreviousWord(string Line,int &start,char sep=' ', char alt_sep='\0'); 00074 static void ToLower(string &Line); // convert a string to Lower case 00075 static void ToUpper(string &Line); // convert a string to Upper case 00076 00077 // Find \p search in \p Line from the begining. 00084 static int Find(const char *search,string Line); 00085 // Find \p search in \p Line from the end. 00092 static int rFind(const char *search,string Line); 00093 // convert a input type (\p in_T) to another (\p out_T). 00103 template <class out_T, class in_T> static out_T convert(const in_T & t); 00104 // Find the start of a word in a line. 00111 static int GetStartWord(string Line,int CurrentPosition,char sep=' ', char alt_sep='\0'); 00112 // Find the end of a word in a line. 00119 static int GetEndWord(string Line,int CurrentPosition,char sep=' ', char alt_sep='\0'); 00120 // Replace a sub-string by an other in a string. 00126 string ReplaceAll(string InLine, string ToReplace, string By); 00127 }; 00128 00129 00130 //_________________________________________________________________________________ 00131 inline string StringLine::NextWord(string Line,int &start,char sep, char alt_sep) 00132 { 00133 string Word=""; 00134 if(start>=int(Line.size())) 00135 { 00136 return Word; 00137 } 00138 start=GetStartWord(Line,start,sep,alt_sep); 00139 int wordlength=GetEndWord(Line,start,sep,alt_sep)-start; 00140 00141 Word=Line.substr(start,wordlength); 00142 00143 start+=wordlength; 00144 return Word; 00145 } 00146 //_________________________________________________________________________________ 00147 inline string StringLine::PreviousWord(string Line,int &start,char sep, char alt_sep) 00148 { 00149 string Word=""; 00150 if(start<=0) 00151 { 00152 return Word; 00153 } 00154 int pos=Line.rfind(sep,start); 00155 int alt_pos=-1; 00156 int real_pos=pos; 00157 char real_sep=sep; 00158 if(alt_sep!='\0') 00159 { 00160 alt_pos=Line.rfind(alt_sep,start); 00161 real_pos=max(pos,alt_pos); 00162 if(real_pos!=pos) 00163 real_sep=alt_sep; 00164 } 00165 int wordlength=start-Line.rfind(real_sep,real_pos); 00166 if(real_pos<=0) 00167 { 00168 Word=Line.substr(0,start+1); 00169 start=0; 00170 return Word; 00171 } 00172 Word=Line.substr(real_pos+1,wordlength); 00173 00174 start-=wordlength+1; 00175 return Word; 00176 } 00177 00178 //_________________________________________________________________________________ 00179 inline void StringLine::ToLower(string &Line) 00180 { 00181 transform (Line.begin(), Line.end(), // source 00182 Line.begin(), // destination 00183 (int(*)(int))tolower); // operation 00184 } 00185 00186 //_________________________________________________________________________________ 00187 inline void StringLine::ToUpper(string &Line) 00188 { 00189 transform (Line.begin(), Line.end(), // source 00190 Line.begin(), // destination 00191 (int(*)(int))toupper); // operation 00192 } 00193 00194 //_________________________________________________________________________________ 00195 inline int StringLine::GetStartWord(string Line,int CurrentPosition,char sep, char alt_sep) 00196 { 00197 int pos=Line.find(sep,CurrentPosition); 00198 int alt_pos=-1; 00199 if(alt_sep!='\0') 00200 alt_pos=Line.find(alt_sep,CurrentPosition); 00201 int real_pos=pos; 00202 char real_sep=sep; 00203 if(alt_pos>=0) 00204 { 00205 real_pos=min(pos,alt_pos); 00206 if(pos==int(string::npos))real_pos=alt_pos; 00207 if(real_pos!=pos) 00208 real_sep=alt_sep; 00209 } 00210 if(real_pos==int(string::npos)) return CurrentPosition; 00211 while(CurrentPosition<int(Line.size()) && Line[CurrentPosition]==real_sep) 00212 CurrentPosition++; 00213 return CurrentPosition; 00214 } 00215 00216 //_________________________________________________________________________________ 00217 inline int StringLine::GetEndWord(string Line,int CurrentPosition,char sep, char alt_sep) 00218 { 00219 int pos=Line.find(sep,CurrentPosition); 00220 int alt_pos=-1; 00221 if(alt_sep!='\0') 00222 alt_pos=Line.find(alt_sep,CurrentPosition); 00223 int real_pos=pos; 00224 if(alt_pos>=0) 00225 { 00226 real_pos=min(pos,alt_pos); 00227 if(pos==int(string::npos))real_pos=alt_pos; 00228 } 00229 if(real_pos==int(string::npos)) 00230 return Line.size(); 00231 return real_pos; 00232 } 00233 00234 //_________________________________________________________________________________ 00235 inline int StringLine::Find(const char *search,string Line) 00236 { 00237 size_t Pos=Line.find(search); 00238 if(Pos != string::npos ) return Pos; 00239 return -1; 00240 } 00241 00242 //_________________________________________________________________________________ 00243 inline int StringLine::rFind(const char *search,string Line) 00244 { 00245 size_t Pos=Line.rfind(search); 00246 if(Pos != string::npos) return Pos; 00247 return -1; 00248 } 00249 00250 //_________________________________________________________________________________ 00251 template <class out_T, class in_T> 00252 inline out_T StringLine::convert(const in_T & t) 00253 { 00254 stringstream stream; 00255 stream << t; // insert value to stream 00256 out_T result; // store conversion's result here 00257 stream >> result; // write value to result 00258 return result; 00259 } 00260 00261 //_________________________________________________________________________________ 00262 inline string StringLine::ReplaceAll(string InLine, string ToReplace, string By) 00263 { 00264 int start=0; 00265 int pos=InLine.find(ToReplace,start); 00266 while(pos!=int(string::npos)) 00267 { 00268 InLine.replace(pos,ToReplace.size(),By); 00269 start=0; 00270 pos=InLine.find(ToReplace,start); 00271 } 00272 return InLine; 00273 00274 } 00275 #endif