Diana Software
QStringHandler.cc
Go to the documentation of this file.
1 #include "QStringHandler.hh"
2 
3 #include "QString.hh"
4 #include "QBool.hh"
5 
6 #include "TRandom.h"
7 #include "TTimeStamp.h"
8 
9 #include <iomanip>
10 #include <sstream>
11 #include <stdlib.h>
12 
13 #include <algorithm>
14 #include <vector>
15 #include <list>
16 #include <ctime>
17 
18 //_____________________________________________________________________________
19 std::string QStringHandler::RemoveFromString(const std::string &text,const char *chars) {
20  std::string ret(text);
21  for(size_t i=0;i<strlen(chars);i++)
22  ret.erase(std::remove(ret.begin(),ret.end(),chars[i]),ret.end());
23  return ret;
24 }
25 
26 //_____________________________________________________________________________
27 std::string QStringHandler::ToLower(const std::string &text) {
28  std::string ret(text);
29  std::transform(ret.begin(),ret.end(),ret.begin(),(int (*)(int))std::tolower);
30  return ret;
31 }
32 
33 //_____________________________________________________________________________
34 std::string QStringHandler::ToUpper(const std::string &text) {
35  std::string ret(text);
36  std::transform(ret.begin(),ret.end(),ret.begin(),(int (*)(int))std::toupper);
37  return ret;
38 }
39 
40 //_____________________________________________________________________________
41 void QStringHandler::Escape(std::string& text, char toBeEscaped)
42 {
43  size_t position = 0;
44  while(1)
45  {
46  if((position = text.find(toBeEscaped, position)) == std::string::npos)
47  break;
48  text.insert(position, 1, '\\');
49  position += 2;
50  }
51 }
52 
53 
54 //_____________________________________________________________________________
55 void QStringHandler::UnEscape(std::string& text, char escaped)
56 {
57  size_t position = 0;
58  while(1)
59  {
60  if((position = text.find(escaped, position)) == std::string::npos)
61  break;
62 
63  if( (position > 0) && (text.at(position-1) == '\\') )
64  text.erase(position-1, 1);
65 
66  position++;
67  }
68 }
69 
70 
71 //_____________________________________________________________________________
72 void QStringHandler::Replace(std::string& text, char oldChar, char newChar)
73 {
74  size_t position = 0;
75  while(1)
76  {
77  if( (position = text.find(oldChar, position)) == std::string::npos)
78  break;
79 
80  // do not replace oldChar if escaped
81  if( (position == 0) || (text.at(position-1) != '\\') )
82  text.replace(position, 1, 1, newChar);
83 
84  position++;
85  }
86 }
87 
88 //_____________________________________________________________________________
89 size_t QStringHandler::Split(std::string source,
90  std::list<std::string>& splitted,
91  char separator)
92 {
93  splitted.clear();
95 
96  while(1)
97  {
98  size_t pos = 0;
99  while(1)
100  { // loop to avoid splitting at escaped separators
101  pos = source.find(separator, pos);
102  if(std::string::npos == pos)
103  break;
104  if(pos != 0 && source.at(pos-1) == '\\')
105  pos++;
106  else
107  break;
108  }
109 
110  std::string element = source.substr(0, pos);
111 
113 
114  if( !(element.empty()) )
115  splitted.push_back(element);
116 
117  if(pos == std::string::npos)
118  break;
119 
120  source = source.substr(pos+1);
121  }
122 
123  return splitted.size();
124 }
125 
126 
127 //____________________________________________________________________________
128 bool QStringHandler::StringToInt(std::string in, int &out, bool strictCheck)
129 {
131 
132  char *check;
133  int base = (("0x" == in.substr(0, 2))? 16 : 10); // hex or decimal format
134  const char* cStr = in.c_str();
135 
136  out = ::strtol(cStr, &check, base);
137 
138  if(cStr == check) // nothing converted
139  return false;
140 
141  return ( ('\0' == (*check)) || (!strictCheck) );
142 }
143 
144 
145 //____________________________________________________________________________
146 bool QStringHandler::StringToLong(std::string in, Long64_t &out,
147  bool strictCheck)
148 {
150 
151  char *check;
152  int base = (("0x" == in.substr(0, 2))? 16 : 10); // hex or decimal format
153  const char* cStr = in.c_str();
154 
155  out = ::strtoll(cStr, &check, base);
156 
157  if(cStr == check) // nothing converted
158  return false;
159 
160  return ( ('\0' == (*check)) || (!strictCheck) );
161 }
162 
163 
164 //____________________________________________________________________________
165 bool QStringHandler::StringToBool(std::string in, bool &out)
166 {
168 
169  if( "true" == in
170  || "t" == in
171  || "TRUE" == in
172  || "T" == in
173  || "1" == in)
174  {
175  out = true;
176  return true;
177  }
178 
179  if("false" == in
180  || "f" == in
181  || "FALSE" == in
182  || "F" == in
183  || "0" == in)
184  {
185  out = false;
186  return true;
187  }
188 
189  return false;
190 }
191 
192 //_____________________________________________________________________________
193 bool QStringHandler::StringToDouble(std::string in,
194  double &out,
195  bool strictCheck)
196 {
198 
199  char *check;
200  const char* cStr = in.c_str();
201  out = ::strtod(cStr, &check);
202 
203  if(cStr == check) // nothing converted
204  return false;
205 
206  return ( ('\0' == (*check)) || (!strictCheck) );
207 }
208 
209 
210 //_____________________________________________________________________________
211 bool QStringHandler::StringToChar(const std::string& src, char& dest)
212 {
213  int iVal;
214 
215  if (!QStringHandler::StringToInt(src,iVal))
216  return false;
217 
218  if(iVal < 0 || iVal > 255)
219  return false;
220 
221  dest = (char)iVal;
222  return true;
223 }
224 
225 
226 //_____________________________________________________________________________
227 std::string QStringHandler::IntToString(int val, int width, char pad)
228 {
229  std::ostringstream text;
230  if(width > 0)
231  text << std::setw(width) << std::setfill(pad);
232  text << val;
233  return text.str();
234 }
235 
236 std::string QStringHandler::LongToString(Long64_t val, int width, char pad)
237 {
238  std::ostringstream text;
239  if(width > 0)
240  text << std::setw(width) << std::setfill(pad);
241  text << val;
242  return text.str();
243 }
244 
245 std::string QStringHandler::BoolToString(bool val)
246 {
247  return val ? "true" : "false";
248 }
249 
250 //_____________________________________________________________________________
251 std::string QStringHandler::DoubleToString(double val, int nDigits, bool scientific)
252 {
253  if(nDigits < 1)
254  nDigits = 1;
255  std::ostringstream text;
256  // set default output format (it is not std::fixed neither std::scientific)
257  text.unsetf(std::ios_base::floatfield);
258 
259  if(scientific)
260  text << std::scientific << std::setprecision(nDigits - 1) << val;
261  else
262  text << std::setprecision(nDigits) << val;
263  return text.str();
264 }
265 
266 
267 //_____________________________________________________________________________
268 std::string QStringHandler::IntToHexString(int value)
269 {
270  std::ostringstream hexa;
271  hexa << "0x" << std::hex << std::setw(4) << std::setfill('0') << value;
272  return hexa.str();
273 }
274 
275 std::string QStringHandler::Resize(const std::string& str, size_t len)
276 {
277  std::string s;
278  if (str.size() < len ) {
279  s = str;
280  int i = len - s.size();
281  while(i--) s += " ";
282  } else {
283  s = str.substr(0,len);
284  }
285  return s;
286 }
287 
288 //_____________________________________________________________________________
289 std::string QStringHandler::TimeFormat(const char *fmt) {
290  return TimeFormat(fmt,::time(0));
291 }
292 
293 //_____________________________________________________________________________
294 std::string QStringHandler::TimeFormat(const char *fmt,time_t t0) {
295  char buffer[512];
296  ::strftime(buffer,512,fmt,::localtime(&t0));
297  return std::string(buffer);
298 }
299 
300 
301 
302 //_____________________________________________________________________________
303 std::string QStringHandler::SqlString(const Diana::QBool& boolVal,
304  bool modeSelect)
305 {
306  std::string retString = "";
307 
308  if(modeSelect)
309  retString += "IS ";
310  if(boolVal.IsValid())
311  retString += ((boolVal == true)?"true":"false");
312  else
313  retString += "NULL";
314  return retString;
315 }
316 
317 //_____________________________________________________________________________
318 std::string QStringHandler::SqlString(const std::vector<int>& vecVal,
319  bool modeSelect, bool writeEmptyBra)
320 {
321  std::stringstream retString;
322 
323  //if mode is selection
324  if(modeSelect){
325  if( !writeEmptyBra && vecVal.size() == 0 )
326  retString << "IS ";
327  else
328  retString << "=";
329  }
330  //if QStdVector is empty (and want to write NULL)
331  if( !writeEmptyBra && vecVal.size() == 0 )
332  retString << "NULL";
333  //if QStdVector is not empty
334  else{
335  retString << "'{";
336  for(uint i = 0; i < vecVal.size(); i++)
337  retString << (i!=0 ? "," : "") << vecVal[i];
338  retString << "}'";
339  }
340  return retString.str();
341 }
342 
343 //_____________________________________________________________________________
344 std::string QStringHandler::SqlString(const Diana::QString& strVal,
345  const std::string& selectName)
346 {
347  std::stringstream retString;
348 
349  if(selectName.empty()) // insert query, just set NULL or strVal value
350  {
351  if(strVal.IsValid())
352  return "'" + strVal.GetValue() + "'";
353  else
354  return "NULL";
355  }
356  else // select query, also check empty string for strValue
357  {
358  if(strVal.IsValid())
359  retString << selectName << "='" << (std::string)strVal << "'";
360  else
361  retString << "(" << selectName << " IS NULL OR "
362  << selectName << "='')";
363  }
364  return retString.str();
365 }
366 
367 //_____________________________________________________________________________
368 std::string QStringHandler::RandomString(UInt_t length,Bool_t caseSensitive,Bool_t startLetter) {
369  static TRandom *mRandom = NULL;
370  if(mRandom == NULL) {
371  TTimeStamp A;
372  mRandom = new TRandom(A.GetNanoSec());
373  }
374  const int dict_length = (caseSensitive ? 62 : 36);
375  const char *dictionary = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
376  std::string ret;
377  if(startLetter){
378  ret+=dictionary[(UInt_t)mRandom->Uniform(10,dict_length)];
379  length-=1;
380  }
381  while(length){
382  ret+=dictionary[(UInt_t)mRandom->Uniform(0,dict_length)];
383  length-=1;
384  }
385  return ret;
386 }
387 
388 template std::string QStringHandler::Join(const std::string &,std::vector<std::string>::iterator,std::vector<std::string>::iterator);
389 template std::string QStringHandler::Join(const std::string &,std::vector<std::string>::const_iterator,std::vector<std::string>::const_iterator);
390 template std::string QStringHandler::Join(const std::string &,std::list<std::string>::iterator,std::list<std::string>::iterator);
391 template std::string QStringHandler::Join(const std::string &,std::list<std::string>::const_iterator,std::list<std::string>::const_iterator);
392 
393 template std::string QStringHandler::Join(const std::string &,std::vector<int>::iterator,std::vector<int>::iterator);
394 template std::string QStringHandler::Join(const std::string &,std::vector<int>::const_iterator,std::vector<int>::const_iterator);
395 template std::string QStringHandler::Join(const std::string &,std::list<int>::iterator,std::list<int>::iterator);
396 template std::string QStringHandler::Join(const std::string &,std::list<int>::const_iterator,std::list<int>::const_iterator);
397 
398 template std::string QStringHandler::Join(const std::string &,std::vector<unsigned int>::iterator,std::vector<unsigned int>::iterator);
399 template std::string QStringHandler::Join(const std::string &,std::vector<unsigned int>::const_iterator,std::vector<unsigned int>::const_iterator);
400 template std::string QStringHandler::Join(const std::string &,std::list<unsigned int>::iterator,std::list<unsigned int>::iterator);
401 template std::string QStringHandler::Join(const std::string &,std::list<unsigned int>::const_iterator,std::list<unsigned int>::const_iterator);
402 
403 template std::string QStringHandler::Join(const std::string &,std::vector<long>::iterator,std::vector<long>::iterator);
404 template std::string QStringHandler::Join(const std::string &,std::vector<long>::const_iterator,std::vector<long>::const_iterator);
405 template std::string QStringHandler::Join(const std::string &,std::list<long>::iterator,std::list<long>::iterator);
406 template std::string QStringHandler::Join(const std::string &,std::list<long>::const_iterator,std::list<long>::const_iterator);
407 
408 template std::string QStringHandler::Join(const std::string &,std::vector<unsigned long>::iterator,std::vector<unsigned long>::iterator);
409 template std::string QStringHandler::Join(const std::string &,std::vector<unsigned long>::const_iterator,std::vector<unsigned long>::const_iterator);
410 template std::string QStringHandler::Join(const std::string &,std::list<unsigned long>::iterator,std::list<unsigned long>::iterator);
411 template std::string QStringHandler::Join(const std::string &,std::list<unsigned long>::const_iterator,std::list<unsigned long>::const_iterator);
412 
413 template std::string QStringHandler::Join(const std::string &,std::vector<float>::iterator,std::vector<float>::iterator);
414 template std::string QStringHandler::Join(const std::string &,std::vector<float>::const_iterator,std::vector<float>::const_iterator);
415 template std::string QStringHandler::Join(const std::string &,std::list<float>::iterator,std::list<float>::iterator);
416 template std::string QStringHandler::Join(const std::string &,std::list<float>::const_iterator,std::list<float>::const_iterator);
417 
418 template std::string QStringHandler::Join(const std::string &,std::vector<double>::iterator,std::vector<double>::iterator);
419 template std::string QStringHandler::Join(const std::string &,std::vector<double>::const_iterator,std::vector<double>::const_iterator);
420 template std::string QStringHandler::Join(const std::string &,std::list<double>::iterator,std::list<double>::iterator);
421 template std::string QStringHandler::Join(const std::string &,std::list<double>::const_iterator,std::list<double>::const_iterator);
422 
double t0
Definition: CheckOF.C:26
std::string RandomString(UInt_t length, Bool_t caseSensitive=true, Bool_t startLetter=false)
Return a random string of lenght.
bool StringToChar(const std::string &in, char &out)
convert string to signed char (i.e. 8-bit int)
std::string IntToHexString(int value)
convert int value to string using hex representation
std::string SqlString(const Diana::QBaseType< T > &baseVal, bool modeSelect)
build string valid for sql select or insert query starting from a QBaseType variable
std::string DoubleToString(double val, int nDigits=4, bool scientific=true)
convert double value to string
void Escape(std::string &text, char toBeEscaped)
escape text by adding backslash before each instance of toBeEscaped character
std::string LongToString(Long64_t val, int width=0, char pad='0')
convert long64 value to string
std::string BoolToString(bool val)
convert bool value to string ("true" or "false")
std::string IntToString(int val, int width=0, char pad='0')
convert int value to string
bool StringToBool(std::string in, bool &out)
convert string to bool
std::string TimeFormat(const char *fmt="%b %d %Y %H:%M:%S")
Get the current time formated as a string.
std::string ToUpper(const std::string &text)
Convert to upper case.
size_t Split(std::string source, std::list< std::string > &splitted, char separtator)
split source into a list of substrings separated by separator
std::string & DoubleSwallowSpaces(std::string &s)
remove spaces and tabs from both the beginning and the end of s
bool StringToDouble(std::string in, double &out, bool strictCheck)
convert string to double
bool StringToLong(std::string in, Long64_t &out, bool strictCheck)
convert string to Long64_t
bool StringToInt(std::string in, int &out, bool strictCheck)
convert string to integer
std::string Join(const std::string &joiner, ForwardIterator begin, ForwardIterator end)
Join a list of things into a string.
std::string ToLower(const std::string &text)
Convert to lower case.
std::string Resize(const std::string &s, size_t len)
resize a string to len, adding spaces if necessary
std::string RemoveFromString(const std::string &text, const char *chars)
Remove some characters from the string.
void UnEscape(std::string &text, char escaped)
unescape text by removing backslash preceding each instance of escaped character
void Replace(std::string &text, char oldChar, char newChar)
replace oldChar with newChar in text. Escaped characters are not replaced.