Nektar++
ParseUtils.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: ParseUtils.cpp
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// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: This file contains various parsing utilities, primarily used
32// by SpatialDomains to process input files.
33//
34////////////////////////////////////////////////////////////////////////////////
35
38#include <boost/lexical_cast.hpp>
39#include <boost/spirit/include/qi_auto.hpp>
40#include <boost/spirit/include/qi_core.hpp>
41#include <sstream>
42
43namespace qi = boost::spirit::qi;
44namespace fusion = boost::fusion;
45
46namespace Nektar
47{
48
49/**
50 * @brief Helper functors for holding a vector of numbers to be parsed by
51 * boost::spirit.
52 *
53 * @see ParseUtils::GenerateSeqVector
54 */
55template <typename T> struct PushBackFunctor
56{
57 PushBackFunctor(std::vector<T> &in) : m_vec(in)
58 {
59 }
60
61 /**
62 * @brief Pushes back values onto #m_vec as given by @p num.
63 */
64 void operator()(T num) const
65 {
66 m_vec.push_back(num);
67 }
68
69 /**
70 * @brief Pushes back values onto #m_vec between the range supplied by @p
71 * num. Valid for only integer types.
72 */
73 void operator()(fusion::vector<T, T> num) const
74 {
75 static_assert(std::is_integral<T>::value, "Integer type required.");
76 for (T i = fusion::at_c<0>(num); i <= fusion::at_c<1>(num); ++i)
77 {
78 m_vec.push_back(i);
79 }
80 }
81
82private:
83 /// Storage vector that will hold parsed variables from boost::spirit.
84 std::vector<T> &m_vec;
85};
86
87/**
88 * @brief Takes a comma-separated compressed string and converts it to entries
89 * in a vector.
90 *
91 * This routine is the inverse of ParseUtils::GenerateSeqString. For example,
92 *
93 * std::string input = "1-4,6-8,5,2,3";
94 * std::vector<unsigned int> output;
95 * ParseUtils::GenerateSeqString(input, output);
96 *
97 * produces an `output` vector with the entries `{1,2,3,4,6,7,8,5,2,3}`.
98 *
99 * @param str Input CSV string of unsigned integers.
100 * @param out Output vector.
101 *
102 * @see ParseUtils::GenerateSeqString
103 */
104bool ParseUtils::GenerateSeqVector(const std::string &str,
105 std::vector<unsigned int> &out)
106{
107 PushBackFunctor<unsigned int> f1(out), f2(out);
108
109 auto it = str.begin();
110 bool success = qi::phrase_parse(
111 it, str.end(),
112 ((qi::uint_ >> '-' >> qi::uint_)[f2] | qi::uint_[f1]) % ',',
113 qi::ascii::space);
114
115 return success && it == str.end();
116}
117
118/**
119 * @brief Takes a comma-separated string and converts it to entries in a vector.
120 *
121 * This routine splits up a comma-separated string and returns a vector with the
122 * entries. Template specialisations should be defined in this file (and not in
123 * the header file) as the use of boost::spirit::qi makes compilation times
124 * quite slow.
125 *
126 * @param str Input CSV string.
127 * @param out Output vector.
128 */
129template <typename T>
130bool ParseUtils::GenerateVector(const std::string &str, std::vector<T> &out)
131{
132 auto it = str.begin();
133 bool success =
134 qi::phrase_parse(it, str.end(), qi::auto_ % ',', qi::ascii::space, out);
135 return success && it == str.end();
136}
137
138template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<int>(
139 const std::string &str, std::vector<int> &out);
140template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<long>(
141 const std::string &str, std::vector<long> &out);
142template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<unsigned int>(
143 const std::string &str, std::vector<unsigned int> &out);
144template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<double>(
145 const std::string &str, std::vector<double> &out);
146template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<float>(
147 const std::string &str, std::vector<float> &out);
148
149/**
150 * @brief Specialised version of ParseUtils::GenerateVector for std::string.
151 *
152 * This routine specialises for the std::string data type as this type is not
153 * supported by boost::spirit::qi::auto_.
154 */
155template <>
157 const std::string &str, std::vector<std::string> &out)
158{
159 auto it = str.begin();
160 bool success = qi::phrase_parse(it, str.end(), +~qi::char_(",") % ',',
161 qi::ascii::space, out);
162 return success && it == str.end();
163}
164
165/**
166 *
167 */
168bool ParseUtils::GenerateVariableSet(const std::string &str,
169 const std::vector<std::string> &variables,
170 std::set<int> &out)
171{
172 std::vector<int> tempvect;
173 GenerateVariableVector(str, variables, tempvect);
174 out.clear();
175 for (auto i : tempvect)
176 {
177 out.insert(i);
178 }
179 return true;
180}
181
182/**
183 *
184 */
186 const std::string &str, const std::vector<std::string> &variables,
187 std::vector<int> &out)
188{
189 out.clear();
190 std::vector<std::string> vars;
192 "Failed to interpret variable numbers or names from " + str);
193 for (std::string s : vars)
194 {
195 int v = -1;
196 try
197 {
198 v = boost::lexical_cast<int>(s);
199 }
200 catch (const boost::bad_lexical_cast &)
201 {
202 auto index = find(variables.begin(), variables.end(), s);
203 v = index - variables.begin();
204 }
205 if (v < 0 || v >= variables.size())
206 {
207 WARNINGL0(false, "Warning: variable " + s + " not found");
208 }
209 else
210 {
211 out.push_back(v);
212 }
213 }
214 return true;
215}
216
217} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
#define WARNINGL0(condition, msg)
Definition: ErrorUtil.hpp:215
#define LIB_UTILITIES_EXPORT
static bool GenerateVariableSet(const std::string &str, const std::vector< std::string > &variables, std::set< int > &out)
Generate a set of variable locations.
Definition: ParseUtils.cpp:168
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:130
static bool GenerateSeqVector(const std::string &str, std::vector< unsigned int > &out)
Takes a comma-separated compressed string and converts it to entries in a vector.
Definition: ParseUtils.cpp:104
static bool GenerateVariableVector(const std::string &str, const std::vector< std::string > &variables, std::vector< int > &out)
Definition: ParseUtils.cpp:185
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:475
Helper functors for holding a vector of numbers to be parsed by boost::spirit.
Definition: ParseUtils.cpp:56
void operator()(T num) const
Pushes back values onto m_vec as given by num.
Definition: ParseUtils.cpp:64
void operator()(fusion::vector< T, T > num) const
Pushes back values onto m_vec between the range supplied by num. Valid for only integer types.
Definition: ParseUtils.cpp:73
std::vector< T > & m_vec
Storage vector that will hold parsed variables from boost::spirit.
Definition: ParseUtils.cpp:84
PushBackFunctor(std::vector< T > &in)
Definition: ParseUtils.cpp:57