All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Parser.cxx
Go to the documentation of this file.
1 #ifndef __FLASHMATCHBASE_PARSER_CXX__
2 #define __FLASHMATCHBASE_PARSER_CXX__
3 
4 #include "PSetUtils.h"
5 namespace flashmatch {
6  namespace parser{
7 
8  template<> std::string FromString( const std::string& value)
9  {
10  std::string res(value);
11  if(res.empty()) return res;
12 
13  if(res.find("\"") == 0) res = res.substr(1);
14  if(res.empty()) return res;
15 
16  if(res.rfind("\"") == (res.length()-1)) res = res.substr(0,res.length()-1);
17  return res;
18  }
19 
20  template<> float FromString( const std::string& value )
21  { return std::stof(value); }
22 
23  template<> double FromString( const std::string& value )
24  { return std::stod(value); }
25 
26  template<> unsigned short FromString( const std::string& value)
27  { return std::stoul(value); }
28 
29  template<> unsigned int FromString( const std::string& value)
30  { return std::stoul(value); }
31 
32  template<> unsigned long FromString( const std::string& value)
33  { return std::stoul(value); }
34 
35  template<> short FromString( const std::string& value )
36  { return std::stoi(value); }
37 
38  template<> int FromString( const std::string& value )
39  { return std::stoi(value); }
40 
41  template<> long FromString( const std::string& value )
42  { return std::stol(value); }
43 
44  template<> bool FromString(const std::string& value )
45  {
46  std::string tmp=value;
47  std::transform(tmp.begin(),tmp.end(),tmp.begin(),::tolower);
48  if( value == "true" || value == "1" ) return true;
49  if( value == "false" || value == "0" ) return false;
50  std::cerr << "Invalid bool expression: " << tmp << std::endl;
51  throw std::exception();
52  return false;
53  }
54 
55  template<> std::vector< std::string > FromString (const std::string& value )
56  {
57  //std::cout<<value<<std::endl;
58  std::vector<std::string> res;
59  if(value.find("[") != 0 || (value.rfind("]")+1) != value.size()) {
60  std::string msg;
61  std::cerr << "Invalid vector expression: " << value << std::endl;
62  throw std::exception();
63  }
64  size_t index = 1;
65  while((index+1) < value.size()) {
66  size_t next_index = value.find(",",index);
67  if(next_index >= value.size()) break;
68  std::string cand = value.substr(index,next_index-index);
69  res.emplace_back(cand);
70  index = next_index + 1;
71  }
72  if( (index+1) < value.size() )
73  res.push_back(value.substr(index,value.size()-index-1));
74 
75  for(auto& s : res) {
76  if(s.find("\"")==0) s=s.substr(1,s.size()-1);
77  if(s.rfind("\"")+1 == s.size()) s = s.substr(0,s.size()-1);
78  }
79  //for(auto const& s : res) std::cout<<s<<std::endl;
80  return res;
81  }
82 
83  template<> std::vector< float > FromString< std::vector< float > > (const std::string& value )
84  {
85  auto str_v = FromString<std::vector<std::string> >(value);
86  std::vector<float> res;
87  res.reserve(str_v.size());
88  for(auto const& v : str_v)
89  res.push_back( FromString<float>(v) );
90  return res;
91  }
92 
93  template<> std::vector< double > FromString< std::vector< double > > (const std::string& value )
94  {
95  auto str_v = FromString<std::vector<std::string> >(value);
96  std::vector<double> res;
97  res.reserve(str_v.size());
98  for(auto const& v : str_v)
99  res.push_back( FromString<double>(v) );
100  return res;
101  }
102 
103  template<> std::vector< short > FromString< std::vector< short > > (const std::string& value )
104  {
105  auto str_v = FromString<std::vector<std::string> >(value);
106  std::vector<short> res;
107  res.reserve(str_v.size());
108  for(auto const& v : str_v)
109  res.push_back( FromString<short>(v) );
110  return res;
111  }
112 
113  template<> std::vector< int > FromString< std::vector< int > > (const std::string& value )
114  {
115  auto str_v = FromString<std::vector<std::string> >(value);
116  std::vector<int> res;
117  res.reserve(str_v.size());
118  for(auto const& v : str_v)
119  res.push_back( FromString<int>(v) );
120  return res;
121  }
122 
123  template<> std::vector< long > FromString< std::vector< long > > (const std::string& value )
124  {
125  auto str_v = FromString<std::vector<std::string> >(value);
126  std::vector<long> res;
127  res.reserve(str_v.size());
128  for(auto const& v : str_v)
129  res.push_back( FromString<long>(v) );
130  return res;
131  }
132 
133  template<> std::vector< unsigned short > FromString< std::vector< unsigned short > > (const std::string& value )
134  {
135  auto str_v = FromString<std::vector<std::string> >(value);
136  std::vector<unsigned short> res;
137  res.reserve(str_v.size());
138  for(auto const& v : str_v)
139  res.push_back( FromString<unsigned short>(v) );
140  return res;
141  }
142 
143  template<> std::vector< unsigned int > FromString< std::vector< unsigned int > > (const std::string& value )
144  {
145  auto str_v = FromString<std::vector<std::string> >(value);
146  std::vector<unsigned int> res;
147  res.reserve(str_v.size());
148  for(auto const& v : str_v)
149  res.push_back( FromString<unsigned int>(v) );
150  return res;
151  }
152 
153  template<> std::vector< unsigned long > FromString< std::vector< unsigned long > > (const std::string& value )
154  {
155  auto str_v = FromString<std::vector<std::string> >(value);
156  std::vector<unsigned long> res;
157  res.reserve(str_v.size());
158  for(auto const& v : str_v)
159  res.push_back( FromString<unsigned long>(v) );
160  return res;
161  }
162 
163  template<> std::vector< bool > FromString< std::vector< bool > > (const std::string& value )
164  {
165  auto str_v = FromString<std::vector<std::string> >(value);
166  std::vector<bool> res;
167  res.reserve(str_v.size());
168  for(auto const& v : str_v)
169  res.push_back( FromString<bool>(v) );
170  return res;
171  }
172 
173  template<> std::string ToString<std::string>(const std::string& value)
174  {
175  std::string res(value);
176  if(res.empty()) return res;
177 
178  if(res.find("\"") == 0) res = res.substr(1);
179  if(res.empty()) return res;
180 
181  if(res.rfind("\"") == (res.length()-1)) res = res.substr(0,res.length()-1);
182  return res;
183  }
184 
185  }
186 }
187 #endif
188 
static constexpr Sample_t transform(Sample_t sample)
BEGIN_PROLOG could also be cerr
bool FromString< bool >(const std::string &value)
Parse flashmatch::PSet configuration file content.
float FromString< float >(const std::string &value)
Parse flashmatch::PSet configuration file content.
std::string FromString(const std::string &value)
Definition: Parser.cxx:8
int FromString< int >(const std::string &value)
Parse flashmatch::PSet configuration file content.
unsigned int FromString< unsigned int >(const std::string &value)
Parse flashmatch::PSet configuration file content.
Utility functions in Base/PSet.
long FromString< long >(const std::string &value)
Parse flashmatch::PSet configuration file content.
double FromString< double >(const std::string &value)
Parse flashmatch::PSet configuration file content.
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
unsigned short FromString< unsigned short >(const std::string &value)
Parse flashmatch::PSet configuration file content.
unsigned long FromString< unsigned long >(const std::string &value)
Parse flashmatch::PSet configuration file content.
short FromString< short >(const std::string &value)
Parse flashmatch::PSet configuration file content.
temporary value