GEMC  2.2
Geant4 Monte-Carlo Framework
string_utilities.h
Go to the documentation of this file.
1 
6 #ifndef string_utilities_H
7 #define string_utilities_H
8 
9 // Qt4 headers
10 #include <QString>
11 #include <QVariant>
12 
13 // C++ headers
14 #include <vector>
15 #include <iostream>
16 #include <string>
17 #include <sstream>
18 using namespace std;
19 
20 
21 inline string stringify(double x)
22 {
23  ostringstream o;
24  o << x;
25  return o.str();
26 }
27 
28 inline string stringify(int x)
29 {
30  ostringstream o;
31  o << x;
32  return o.str();
33 }
34 
35 
36 // from QVariant to string
37 // OS dependance
38 inline string qv_tostring(QVariant input)
39 {
40 
41  string output;
42 
43  // In Windows we need to initialize the string from toAscii
44  // In Posix we need the toStdString
45  #ifdef _MSC_VER
46  output = input.toString().toLatin1();
47  #else
48  output = input.toString().toStdString();
49  #endif
50 
51  return output;
52 }
53 
54 // from QString to string
55 // OS dependance
56 inline string qs_tostring(QString input)
57 {
58 
59  string output;
60 
61  // In Windows we need to initialize the string from toAscii
62  // In Posix we need the toStdString
63  #ifdef _MSC_VER
64  output = input.toLatin1();
65  #else
66  output = input.toStdString();
67  #endif
68 
69  return output;
70 }
71 
72 
73 //replaces a character to another characters.
74 inline string replaceCharWithChars(string input, string x, string y)
75 {
76 
77  string output = "";
78 
79  for(unsigned int k=0; k<input.size(); k++)
80  {
81  int replace = 1;
82  for(unsigned int j=0; j<x.size(); j++)
83  {
84 
85  if(input[k] == x[j])
86  {
87  output.append(y);
88  replace = 0;
89  }
90  }
91  if(replace) output += input[k];
92  }
93 
94  return output;
95 
96 }
97 
98 //replaces a string that has more than one character to another string
99 inline string replaceCharsWithChars(string input, string x, string y)
100 {
101 
102  string output = "";
103  unsigned long k = x.length();
104  unsigned long i = 0;
105 
106 
107 
108  while(i<input.size())
109  {
110  unsigned int j=0;
111  unsigned int l=0;
112  while(j<k)
113  {
114  if(input[i+j] == x[j])
115  l++;
116  j++;
117  }
118 
119  if(l==k)
120  {
121  output.append(y);
122  i = i+k;
123  }
124  else
125  {
126  output = output+input[i];
127  i++;
128  }
129  }
130 
131 
132  return output;
133 }
134 
135 vector< vector<string> > dimensionstype(string);
136 double get_number(string,int warn_no_unit=0);
137 string TrimSpaces(string);
138 vector<string> get_strings_except(string, string);
139 vector<string> get_strings(string);
140 vector<string> get_strings(string, string);
141 void print_vstring(vector<string>);
142 vector<string> get_info(string);
143 string get_variation(string);
144 bool is_main_variation(string);
145 
146 ostream &operator<<(ostream &stream, map<string, string>);
147 
148 
149 // returns a double from a QVariant
150 inline double get_number(QVariant input)
151 {
152  return get_number(TrimSpaces(qv_tostring(input)));
153 }
154 
155 
156 // returns a double from a string
157 // notice, atof should work but it doesn't work on some Mac
158 // atof may also not be thread safe
159 // solving all this with stringstream
160 inline double stringToDouble(string v)
161 {
162  stringstream ss(TrimSpaces(v));
163  double d;
164  ss >> d;
165  return d;
166 }
167 
168 
169 // returns a double from a string
170 // notice, atof should work but it doesn't work on some Mac
171 // atof may also not be thread safe
172 // solving all this with stringstream
173 // from QString to string
174 // OS dependance
175 inline double qs_toDouble(QString input)
176 {
177  return stringToDouble(qs_tostring(input));
178 }
179 
180 #endif
181 
182 
183 
184 
185 
vector< string > get_strings_except(string, string)
returns a vector of strings from a stringstream, space is delimiter, ignore string with second argume...
STL namespace.
string replaceCharsWithChars(string input, string x, string y)
double get_number(string, int warn_no_unit=0)
Returns number with dimension from string, i.e. 100*cm.
string qv_tostring(QVariant input)
string replaceCharWithChars(string input, string x, string y)
vector< string > get_info(string)
get information from strings such as "5*GeV, 2*deg, 10*deg"
double qs_toDouble(QString input)
string stringify(double x)
void print_vstring(vector< string >)
prints each element of a string vector
vector< string > get_strings(string)
returns a vector of strings from a stringstream, space is delimiter
string qs_tostring(QString input)
vector< vector< string > > dimensionstype(string)
Returns dimensions nomenclature for different solid type.
Definition: detector.cc:874
bool is_main_variation(string)
returns 1 if the string "main:" is found on the input
double stringToDouble(string v)
string get_variation(string)
parse variation name from string
string TrimSpaces(string)
Removes leading and trailing spaces.