Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ParseUtils.hpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ParseUtils.hpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: This file contains various parsing utilities, primarily used
33 // by SpatialDomains to process input files.
34 //
35 //
36 ////////////////////////////////////////////////////////////////////////////////
37 #ifndef NEKTAR_LIBUTILITIES_PARSEUTILS_HPP
38 #define NEKTAR_LIBUTILITIES_PARSEUTILS_HPP
39 
41 #include <vector>
42 #include <sstream>
43 
44 #include <boost/version.hpp>
46 #if( BOOST_VERSION / 100 % 1000 >= 36 )
47 #include <boost/spirit/include/classic_core.hpp>
48 #include <boost/spirit/include/classic_push_back_actor.hpp>
49 
50 using namespace boost::spirit::classic;
51 #else
52 #include <boost/spirit/core.hpp>
53 #include <boost/spirit/actor/push_back_actor.hpp>
54 
55 using namespace boost::spirit;
56 #endif
57 
58 namespace Nektar
59 {
60  class ParseUtils
61  {
62  public:
63  static bool ParseRealAssignment(const char *const str, std::string &symbol, NekDouble &value)
64  {
65  SymbolFunctor symbolFunctor(&symbol);
66  ValueFunctor valueFunctor(&value);
67 
68  return parse(str,
69  // Begin grammar
70  (
71  lexeme_d[alpha_p >> *alnum_p][symbolFunctor] >> "=" >> real_p[valueFunctor]
72  )
73  ,
74  // End grammar
75 
76  space_p).full;
77  }
78 
79  static bool GenerateSeqVector(const char *const str, std::vector<unsigned int> &vec)
80  {
81  // Functors used to parse the sequence.
82  fctor1 functor1(&vec);
83  fctor2 functor2(&vec);
84 
85  return parse(str,
86  // Begin grammar
87  (
88  uint_p[functor1] >> !('-' >> uint_p[functor2]) >>
89  *(',' >> uint_p[functor1] >> !('-' >> uint_p[functor2]))
90  )
91  ,
92  // End grammar
93 
94  space_p).full;
95  }
96 
97  static bool GenerateOrderedVector(const char *const str, std::vector<unsigned int> &vec)
98  {
99  // Functors used to parse the sequence.
100  fctor1 functor1(&vec);
101 
102  return parse(str,
103  // Begin grammar
104  (
105  uint_p[functor1] >> *(',' >> uint_p[functor1])
106  )
107  ,
108  // End grammar
109 
110  space_p).full;
111  }
112 
113  static bool GenerateOrderedVector(const char *const str, std::vector<NekDouble> &vec)
114  {
115  // Functors used to parse the sequence.
116  fctor4 functor4(&vec);
117 
118  return parse(str,
119  // Begin grammar
120  (
121  real_p[functor4] >> *(',' >> real_p[functor4])
122  )
123  ,
124  // End grammar
125  space_p).full;
126  }
127 
128  static bool GenerateUnOrderedVector(const char *const str, std::vector<NekDouble> &vec)
129  {
130  // Functors used to parse the sequence.
131  fctor5 functor5(&vec);
132 
133  return parse(str,
134  // Begin grammar
135  (
136  real_p[functor5] >> *(',' >> real_p[functor5])
137  )
138  ,
139  // End grammar
140  space_p).full;
141  }
142 
143  static bool GenerateOrderedStringVector(const char *const str, std::vector<std::string> &vec)
144  {
145  // Functors used to parse the sequence.
146  fctor3 functor3(&vec);
147 
148  return parse(str,
149  // Begin grammar
150  (
151  (+(print_p - ','))[functor3] >> *(',' >> (+(print_p - ','))[functor3])
152  )
153  ,
154  // End grammar
155 
156  space_p).full;
157  }
158 
159  static std::string GenerateSeqString(const std::vector<unsigned int> &elmtids)
160  {
161  std::stringstream idStringStream;
162  bool setdash = true;
163  unsigned int endval;
164 
165  if (elmtids.size() == 0)
166  {
167  return std::string("");
168  }
169 
170  idStringStream << elmtids[0];
171  for (int i = 1; i < elmtids.size(); ++i)
172  {
173  if (elmtids[i] == elmtids[i - 1] + 1)
174  {
175  if (setdash)
176  {
177  idStringStream << "-";
178  setdash = false;
179  }
180 
181  if (i == elmtids.size() - 1) // last element
182  {
183  idStringStream << elmtids[i];
184  }
185  else
186  {
187  endval = elmtids[i];
188  }
189  }
190  else
191  {
192  if (setdash == false) // finish off previous dash sequence
193  {
194  idStringStream << endval;
195  setdash = true;
196  }
197 
198  idStringStream << "," << elmtids[i];
199  }
200  }
201 
202  return idStringStream.str();
203  }
204 
205  private:
206 
208  {
209  SymbolFunctor(std::string *symbol):
210  m_symbol(symbol)
211  {
212  }
213 
214  void operator()(const char *beg, const char *end) const
215  {
216  m_symbol->assign(beg, end-beg);
217  }
218 
219  private:
220  std::string *m_symbol;
221  };
222 
224  {
226  m_value(value)
227  {
228  }
229 
230  void operator()(NekDouble val) const
231  {
232  *m_value = val;
233  }
234 
235  private:
237  };
238 
239  struct fctor1
240  {
241  fctor1(std::vector<unsigned int> *vec):
242  m_vector(vec)
243  {
244  }
245 
246  void operator()(unsigned int n) const
247  {
248 #ifdef NOTREQUIRED //SJS: I do not think we need this check
249  if (!m_vector->empty())
250  {
251  unsigned int prevElem = m_vector->back();
252 
253  if (n > prevElem)
254  {
255  m_vector->push_back(n);
256  }
257  }
258  else
259 #endif
260  {
261  m_vector->push_back(n);
262  }
263  }
264 
265  private:
266  std::vector<unsigned int> *m_vector;
267  fctor1();
268  };
269 
270  struct fctor2
271  {
272  fctor2(std::vector<unsigned int> *vec):
273  m_vector(vec)
274  {
275  }
276 
277  void operator()(unsigned int n) const
278  {
279  unsigned int prevElem = m_vector->back();
280 
281  for (unsigned int i=prevElem+1; i<=n; ++i)
282  {
283  m_vector->push_back(i);
284  }
285  }
286 
287  private:
288  std::vector<unsigned int> *m_vector;
289  };
290 
291  struct fctor3
292  {
293  fctor3(std::vector<std::string> *vec):
294  m_vector(vec)
295  {
296  }
297 
298  void operator()(char const* first, char const* last) const
299  {
300  m_vector->push_back(std::string(first, last));
301  }
302 
303  private:
304  std::vector<std::string> *m_vector;
305  };
306 
307  // Probably should template fctor1 if that is possible?
308  struct fctor4
309  {
310  fctor4(std::vector<NekDouble> *vec):
311  m_vector(vec)
312  {
313  }
314 
315  void operator()(NekDouble n) const
316  {
317  if (!m_vector->empty())
318  {
319  NekDouble prevElem = m_vector->back();
320 
321  if (n > prevElem)
322  {
323  m_vector->push_back(n);
324  }
325  }
326  else
327  {
328  m_vector->push_back(n);
329  }
330  }
331 
332  private:
333  std::vector<NekDouble> *m_vector;
334  fctor4();
335  };
336 
337  struct fctor5
338  {
339  fctor5(std::vector<NekDouble> *vec):
340  m_vector(vec)
341  {
342  }
343 
344  void operator()(NekDouble n) const
345  {
346  m_vector->push_back(n);
347  }
348 
349  private:
350  std::vector<NekDouble> *m_vector;
351  fctor5();
352  };
353 
354  };
355 }
356 
357 #endif //NEKTAR_LIBUTILITIES_PARSEUTILS_HPP
static bool GenerateOrderedStringVector(const char *const str, std::vector< std::string > &vec)
Definition: ParseUtils.hpp:143
fctor5(std::vector< NekDouble > *vec)
Definition: ParseUtils.hpp:339
static bool GenerateOrderedVector(const char *const str, std::vector< unsigned int > &vec)
Definition: ParseUtils.hpp:97
static std::string GenerateSeqString(const std::vector< unsigned int > &elmtids)
Definition: ParseUtils.hpp:159
fctor1(std::vector< unsigned int > *vec)
Definition: ParseUtils.hpp:241
std::vector< unsigned int > * m_vector
Definition: ParseUtils.hpp:288
static bool GenerateSeqVector(const char *const str, std::vector< unsigned int > &vec)
Definition: ParseUtils.hpp:79
void operator()(const char *beg, const char *end) const
Definition: ParseUtils.hpp:214
void operator()(NekDouble n) const
Definition: ParseUtils.hpp:315
std::vector< std::string > * m_vector
Definition: ParseUtils.hpp:304
void operator()(unsigned int n) const
Definition: ParseUtils.hpp:246
static bool ParseRealAssignment(const char *const str, std::string &symbol, NekDouble &value)
Definition: ParseUtils.hpp:63
fctor3(std::vector< std::string > *vec)
Definition: ParseUtils.hpp:293
double NekDouble
void operator()(char const *first, char const *last) const
Definition: ParseUtils.hpp:298
void operator()(NekDouble n) const
Definition: ParseUtils.hpp:344
fctor2(std::vector< unsigned int > *vec)
Definition: ParseUtils.hpp:272
SymbolFunctor(std::string *symbol)
Definition: ParseUtils.hpp:209
std::vector< unsigned int > * m_vector
Definition: ParseUtils.hpp:266
static bool GenerateOrderedVector(const char *const str, std::vector< NekDouble > &vec)
Definition: ParseUtils.hpp:113
std::vector< NekDouble > * m_vector
Definition: ParseUtils.hpp:333
static bool GenerateUnOrderedVector(const char *const str, std::vector< NekDouble > &vec)
Definition: ParseUtils.hpp:128
void operator()(unsigned int n) const
Definition: ParseUtils.hpp:277
void operator()(NekDouble val) const
Definition: ParseUtils.hpp:230
fctor4(std::vector< NekDouble > *vec)
Definition: ParseUtils.hpp:310
std::vector< NekDouble > * m_vector
Definition: ParseUtils.hpp:350