MURE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
StringLine.hxx
Go to the documentation of this file.
1 #ifndef _STRINGLINE_
2 #define _STRINGLINE_
3 
4 #include <string>
5 #include <sstream>
6 #include <iostream>
7 #include <algorithm>
8 #include <cctype>
9 using namespace std;
15 
50 {
51  public:
53 
62  static string NextWord(string Line,int &start,char sep=' ', char alt_sep='\0');
64 
73  static string PreviousWord(string Line,int &start,char sep=' ', char alt_sep='\0');
74  static void ToLower(string &Line);
75  static void ToUpper(string &Line);
76 
78 
84  static int Find(const char *search,string Line);
86 
92  static int rFind(const char *search,string Line);
94 
103  template <class out_T, class in_T> static out_T convert(const in_T & t);
105 
121  template <class T> static bool ToNumber(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&))
122  {
123  if(s.size()==0) return true;
124  std::istringstream iss(s);
125  return !(iss >> f >> t).fail();
126  }
128 
134  static int GetStartWord(string Line,int CurrentPosition,char sep=' ', char alt_sep='\0');
136 
142  static int GetEndWord(string Line,int CurrentPosition,char sep=' ', char alt_sep='\0');
144 
149  static string ReplaceAll(string InLine, string ToReplace, string By);
150  static bool IsDouble(const std::string& s);
151 };
152 
153 
154 //_________________________________________________________________________________
155 inline string StringLine::NextWord(string Line,int &start,char sep, char alt_sep)
156 {
157  string Word="";
158  if(start>=int(Line.size()))
159  {
160  return Word;
161  }
162  start=GetStartWord(Line,start,sep,alt_sep);
163  int wordlength=GetEndWord(Line,start,sep,alt_sep)-start;
164 
165  Word=Line.substr(start,wordlength);
166 
167  start+=wordlength;
168  return Word;
169 }
170 //_________________________________________________________________________________
171 inline string StringLine::PreviousWord(string Line,int &start,char sep, char alt_sep)
172 {
173  string Word="";
174  if(start<=0)
175  {
176  return Word;
177  }
178  int pos=Line.rfind(sep,start);
179  int alt_pos=-1;
180  int real_pos=pos;
181  char real_sep=sep;
182  if(alt_sep!='\0')
183  {
184  alt_pos=Line.rfind(alt_sep,start);
185  real_pos=max(pos,alt_pos);
186  if(real_pos!=pos)
187  real_sep=alt_sep;
188  }
189  int wordlength=start-Line.rfind(real_sep,real_pos);
190  if(real_pos<=0)
191  {
192  Word=Line.substr(0,start+1);
193  start=0;
194  return Word;
195  }
196  Word=Line.substr(real_pos+1,wordlength);
197 
198  start-=wordlength+1;
199  return Word;
200 }
201 
202 //_________________________________________________________________________________
203 inline void StringLine::ToLower(string &Line)
204 {
205  transform (Line.begin(), Line.end(), // source
206  Line.begin(), // destination
207  (int(*)(int))tolower); // operation
208 }
209 
210 //_________________________________________________________________________________
211 inline void StringLine::ToUpper(string &Line)
212 {
213  transform (Line.begin(), Line.end(), // source
214  Line.begin(), // destination
215  (int(*)(int))toupper); // operation
216 }
217 
218 //_________________________________________________________________________________
219 inline int StringLine::GetStartWord(string Line,int CurrentPosition,char sep, char alt_sep)
220 {
221  int pos=Line.find(sep,CurrentPosition);
222  int alt_pos=-1;
223  if(alt_sep!='\0')
224  alt_pos=Line.find(alt_sep,CurrentPosition);
225  int real_pos=pos;
226  char real_sep=sep;
227  if(alt_pos>=0)
228  {
229  real_pos=min(pos,alt_pos);
230  if(pos==int(string::npos))real_pos=alt_pos;
231  if(real_pos!=pos)
232  real_sep=alt_sep;
233  }
234  if(real_pos==int(string::npos)) return CurrentPosition;
235  while(CurrentPosition<int(Line.size()) && Line[CurrentPosition]==real_sep)
236  CurrentPosition++;
237  return CurrentPosition;
238 }
239 
240 //_________________________________________________________________________________
241 inline int StringLine::GetEndWord(string Line,int CurrentPosition,char sep, char alt_sep)
242 {
243  int pos=Line.find(sep,CurrentPosition);
244  int alt_pos=-1;
245  if(alt_sep!='\0')
246  alt_pos=Line.find(alt_sep,CurrentPosition);
247  int real_pos=pos;
248  if(alt_pos>=0)
249  {
250  real_pos=min(pos,alt_pos);
251  if(pos==int(string::npos))real_pos=alt_pos;
252  }
253  if(real_pos==int(string::npos))
254  return Line.size();
255  return real_pos;
256 }
257 
258 //_________________________________________________________________________________
259 inline int StringLine::Find(const char *search,string Line)
260 {
261  size_t Pos=Line.find(search);
262  if(Pos != string::npos ) return Pos;
263  return -1;
264 }
265 
266 //_________________________________________________________________________________
267 inline int StringLine::rFind(const char *search,string Line)
268 {
269  size_t Pos=Line.rfind(search);
270  if(Pos != string::npos) return Pos;
271  return -1;
272 }
273 
274 //_________________________________________________________________________________
275 template <class out_T, class in_T>
276 inline out_T StringLine::convert(const in_T & t)
277 {
278  stringstream stream;
279  stream << t; // insert value to stream
280  out_T result; // store conversion's result here
281  stream >> result; // write value to result
282  return result;
283 }
284 
285 //_________________________________________________________________________________
286 inline string StringLine::ReplaceAll(string InLine, string ToReplace, string By)
287 {
288  int start=0;
289  int pos=InLine.find(ToReplace,start);
290  while(pos!=int(string::npos))
291  {
292  InLine.replace(pos,ToReplace.size(),By);
293  start=0;
294  pos=InLine.find(ToReplace,start);
295  }
296  return InLine;
297 }
298 //_________________________________________________________________________________
299 inline bool StringLine::IsDouble(const std::string& s)
300 {
301  std::istringstream i(s);
302  double temp;
303  return ( (i >> temp) ? true : false );
304 }
305 
306 #endif
static bool IsDouble(const std::string &s)
Definition: StringLine.hxx:299
static int Find(const char *search, string Line)
Find search in Line from the begining.
Definition: StringLine.hxx:259
static string ReplaceAll(string InLine, string ToReplace, string By)
Replace a sub-string by an other in a string.
Definition: StringLine.hxx:286
static bool ToNumber(T &t, const std::string &s, std::ios_base &(*f)(std::ios_base &))
try to convert a string to a number.
Definition: StringLine.hxx:121
static string NextWord(string Line, int &start, char sep=' ', char alt_sep='\0')
Find the next word in a line.
Definition: StringLine.hxx:155
static string PreviousWord(string Line, int &start, char sep=' ', char alt_sep='\0')
Find the previous word in a line.
Definition: StringLine.hxx:171
static int rFind(const char *search, string Line)
Find search in Line from the end.
Definition: StringLine.hxx:267
static int GetStartWord(string Line, int CurrentPosition, char sep=' ', char alt_sep='\0')
Find the start of a word in a line.
Definition: StringLine.hxx:219
Class extracting fields from a string / line.
Definition: StringLine.hxx:49
static out_T convert(const in_T &t)
convert a input type (in_T) to another (out_T).
Definition: StringLine.hxx:276
static void ToUpper(string &Line)
convert a string to Upper case
Definition: StringLine.hxx:211
static int GetEndWord(string Line, int CurrentPosition, char sep=' ', char alt_sep='\0')
Find the end of a word in a line.
Definition: StringLine.hxx:241
static void ToLower(string &Line)
convert a string to Lower case
Definition: StringLine.hxx:203

MURE Project, documentation generated by Doxygen 1.8.5 - Mon Nov 17 2014