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/core/ignore_unused.hpp>
39#include <boost/lexical_cast.hpp>
40#include <boost/spirit/include/qi_auto.hpp>
41#include <boost/spirit/include/qi_core.hpp>
42#include <sstream>
43
44namespace qi = boost::spirit::qi;
45namespace fusion = boost::fusion;
46
47namespace Nektar
48{
49
50/**
51 * @brief Helper functors for holding a vector of numbers to be parsed by
52 * boost::spirit.
53 *
54 * @see ParseUtils::GenerateSeqVector
55 */
56template <typename T> struct PushBackFunctor
57{
58 PushBackFunctor(std::vector<T> &in) : m_vec(in)
59 {
60 }
61
62 /**
63 * @brief Pushes back values onto #m_vec as given by @p num.
64 */
65 void operator()(T num) const
66 {
67 m_vec.push_back(num);
68 }
69
70 /**
71 * @brief Pushes back values onto #m_vec between the range supplied by @p
72 * num. Valid for only integer types.
73 */
74 void operator()(fusion::vector<T, T> num) const
75 {
76 static_assert(std::is_integral<T>::value, "Integer type required.");
77 for (T i = fusion::at_c<0>(num); i <= fusion::at_c<1>(num); ++i)
78 {
79 m_vec.push_back(i);
80 }
81 }
82
83private:
84 /// Storage vector that will hold parsed variables from boost::spirit.
85 std::vector<T> &m_vec;
86};
87
88/**
89 * @brief Takes a comma-separated compressed string and converts it to entries
90 * in a vector.
91 *
92 * This routine is the inverse of ParseUtils::GenerateSeqString. For example,
93 *
94 * std::string input = "1-4,6-8,5,2,3";
95 * std::vector<unsigned int> output;
96 * ParseUtils::GenerateSeqString(input, output);
97 *
98 * produces an `output` vector with the entries `{1,2,3,4,6,7,8,5,2,3}`.
99 *
100 * @param str Input CSV string of unsigned integers.
101 * @param out Output vector.
102 *
103 * @see ParseUtils::GenerateSeqString
104 */
105bool ParseUtils::GenerateSeqVector(const std::string &str,
106 std::vector<unsigned int> &out)
107{
108 PushBackFunctor<unsigned int> f1(out), f2(out);
109
110 auto it = str.begin();
111 bool success = qi::phrase_parse(
112 it, str.end(),
113 ((qi::uint_ >> '-' >> qi::uint_)[f2] | qi::uint_[f1]) % ',',
114 qi::ascii::space);
115
116 return success && it == str.end();
117}
118
119/**
120 * @brief Takes a comma-separated string and converts it to entries in a vector.
121 *
122 * This routine splits up a comma-separated string and returns a vector with the
123 * entries. Template specialisations should be defined in this file (and not in
124 * the header file) as the use of boost::spirit::qi makes compilation times
125 * quite slow.
126 *
127 * @param str Input CSV string.
128 * @param out Output vector.
129 */
130template <typename T>
131bool ParseUtils::GenerateVector(const std::string &str, std::vector<T> &out)
132{
133 auto it = str.begin();
134 bool success =
135 qi::phrase_parse(it, str.end(), qi::auto_ % ',', qi::ascii::space, out);
136 return success && it == str.end();
137}
138
139template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<int>(
140 const std::string &str, std::vector<int> &out);
141template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<long>(
142 const std::string &str, std::vector<long> &out);
143template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<unsigned int>(
144 const std::string &str, std::vector<unsigned int> &out);
145template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<double>(
146 const std::string &str, std::vector<double> &out);
147template LIB_UTILITIES_EXPORT bool ParseUtils::GenerateVector<float>(
148 const std::string &str, std::vector<float> &out);
149
150/**
151 * @brief Specialised version of ParseUtils::GenerateVector for std::string.
152 *
153 * This routine specialises for the std::string data type as this type is not
154 * supported by boost::spirit::qi::auto_.
155 */
156template <>
158 const std::string &str, std::vector<std::string> &out)
159{
160 auto it = str.begin();
161 bool success = qi::phrase_parse(it, str.end(), +~qi::char_(",") % ',',
162 qi::ascii::space, out);
163 return success && it == str.end();
164}
165
166bool ParseUtils::GenerateVariableSet(const std::string &str,
167 const std::vector<std::string> &variables,
168 std::set<int> &out)
169{
170 out.clear();
171 std::vector<std::string> vars;
173 "Failed to interpret variable numbers or names from " + str);
174 for (std::string s : vars)
175 {
176 int v = -1;
177 try
178 {
179 v = boost::lexical_cast<int>(s);
180 }
181 catch (const boost::bad_lexical_cast &)
182 {
183 auto index = find(variables.begin(), variables.end(), s);
184 v = index - variables.begin();
185 }
186 if (v < 0 || v >= variables.size())
187 {
188 WARNINGL0(false, "Warning: variable " + s + " not found");
189 }
190 else
191 {
192 out.insert(v);
193 }
194 }
195 return true;
196}
197
198} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#define WARNINGL0(condition, msg)
Definition: ErrorUtil.hpp:222
#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:166
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:131
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:105
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:453
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
Helper functors for holding a vector of numbers to be parsed by boost::spirit.
Definition: ParseUtils.cpp:57
void operator()(T num) const
Pushes back values onto m_vec as given by num.
Definition: ParseUtils.cpp:65
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:74
std::vector< T > & m_vec
Storage vector that will hold parsed variables from boost::spirit.
Definition: ParseUtils.cpp:85
PushBackFunctor(std::vector< T > &in)
Definition: ParseUtils.cpp:58