Diana Software
QStringHandler.hh
Go to the documentation of this file.
1 
2 #ifndef _Q_STRING_HANDLER_HH_
3 #define _Q_STRING_HANDLER_HH_
4 
5 #include <string>
6 #include <list>
7 #include <sstream>
8 
9 #include "QBaseType.hh"
10 #include "QStdVector.hh"
11 
12 namespace Diana
13 {
14  class QString;
15  class QBool;
16 };
17 
18 
25 namespace QStringHandler
26 {
28  inline std::string& SwallowSpaces(std::string& s)
29  { return s.erase(0, s.find_first_not_of("\t ")); }
30 
32  inline std::string& RSwallowSpaces(std::string& s)
33  { return s.erase(s.find_last_not_of("\t ") + 1, std::string::npos); }
34 
36  inline std::string& DoubleSwallowSpaces(std::string& s)
37  { SwallowSpaces(s); return RSwallowSpaces(s); }
38 
40  std::string RemoveFromString(const std::string &text,const char *chars);
41 
43  std::string ToLower(const std::string &text);
45  std::string ToUpper(const std::string &text);
46 
51  void Escape(std::string& text, char toBeEscaped);
52 
57  void UnEscape(std::string& text, char escaped);
58 
63  void Replace(std::string& text, char oldChar, char newChar);
64 
76  size_t Split(std::string source,
77  std::list<std::string>& splitted,
78  char separtator);
79 
95  bool StringToInt(std::string in, int &out, bool strictCheck);
96 
103  bool StringToLong(std::string in, Long64_t &out, bool strictCheck);
104 
106  inline bool StringToInt(const std::string& in, int& out)
107  { return StringToInt(in, out, true); }
108 
118  bool StringToBool(std::string in, bool &out);
119 
127  bool StringToDouble(std::string in, double &out, bool strictCheck);
128 
130  inline bool StringToDouble(const std::string& in, double& out)
131  { return StringToDouble(in, out, true); }
132 
141  bool StringToChar(const std::string& in, char& out);
142 
143 
147  std::string IntToString(int val, int width = 0, char pad='0');
148 
152  std::string LongToString(Long64_t val, int width = 0, char pad='0');
153 
155  std::string BoolToString(bool val);
156 
172  std::string DoubleToString(double val, int nDigits = 4, bool scientific = true);
173 
179  std::string IntToHexString(int value);
180 
182  std::string Resize(const std::string& s, size_t len);
183 
185  std::string TimeFormat(const char *fmt="%b %d %Y %H:%M:%S");
187  std::string TimeFormat(const char *fmt,time_t t0);
188 
200  template<class T> std::string SqlString(const Diana::QBaseType<T>& baseVal,
201  bool modeSelect)
202  {
203  std::stringstream retSStream;
204  if(baseVal.IsValid())
205  retSStream << (modeSelect?"=":"")
206  << baseVal;
207  else
208  retSStream << (modeSelect?"IS NULL":"NULL");
209  return retSStream.str();
210  }
211 
223  std::string SqlString(const Diana::QBool& boolVal, bool modeSelect);
224 
237  std::string SqlString(const std::vector<int>& vecVal, bool modeSelect,
238  bool writeEmptyBra);
239 
249  std::string SqlString(const Diana::QString& strVal,
250  const std::string& selectName="");
251 
271  std::string RandomString(UInt_t length,Bool_t caseSensitive=true,Bool_t startLetter=false);
272 
273 
281  template <class ForwardIterator> std::string Join(const std::string &joiner,ForwardIterator begin,ForwardIterator end) {
282  std::stringstream ret;
283  ForwardIterator it = begin;
284  if(it==end) return ret.str();
285  ret << *(it++);
286  for(;it!=end;it++) ret << joiner << *it;
287  return ret.str();
288  }
289 
290  // QRootCintTrigger(QStringHandler)
291 };
292 
293 
294 #endif
double t0
Definition: CheckOF.C:26
bool wrapped into a QObject
Definition: QBool.hh:17
string wrapped into a QObject
Definition: QString.hh:18
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...
general purpose string manipulation functions
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
std::string & RSwallowSpaces(std::string &s)
remove spaces and tabs from the end of s
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
std::string & SwallowSpaces(std::string &s)
remove spaces and tabs from the beginning 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.