votca 2024-dev
Loading...
Searching...
No Matches
tokenizer.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18#ifndef VOTCA_TOOLS_TOKENIZER_H
19#define VOTCA_TOOLS_TOKENIZER_H
20
21// Standard includes
22#include <boost/algorithm/string/case_conv.hpp>
23#include <string>
24#include <vector>
25
26// Third party includes
27#include "eigen.h"
28#include "lexical_cast.h"
29#include <boost/tokenizer.hpp>
30#include <type_traits>
31namespace votca {
32namespace tools {
33
34namespace internal {
35
36template <typename T>
37struct type {};
38
39template <typename T,
40 typename std::enable_if_t<std::is_arithmetic<T>::value &&
41 !std::is_same<T, bool>::value,
42 bool> = true>
43inline T convert_impl(const std::string &s, type<T>) {
44 return lexical_cast<T>(s, "Cannot create arithmetic type from" + s);
45}
46
47template <typename T,
48 typename std::enable_if_t<
49 std::is_constructible<T, std::string>::value, bool> = true>
50inline T convert_impl(const std::string &s, type<T>) {
51 return T(s);
52}
53
54inline bool convert_impl(const std::string &s, type<bool>) {
55 if (boost::to_lower_copy(s) == "true" || s == "1") {
56 return true;
57 } else if (boost::to_lower_copy(s) == "false" || s == "0") {
58 return false;
59 } else {
60 throw std::runtime_error("'" + s + "' cannot be converted to bool.");
61 }
62}
63
64} // namespace internal
65
72class Tokenizer {
73 public:
74 using iterator = boost::tokenizer<boost::char_separator<char>>::iterator;
75
85 Tokenizer(const std::string &str, const char *separators) : str_(str) {
86 boost::char_separator<char> sep(separators);
87 tok_ = std::make_unique<boost::tokenizer<boost::char_separator<char>>>(str_,
88 sep);
89 }
90 Tokenizer(const std::string &str, const std::string &separators)
91 : Tokenizer(str, separators.c_str()){};
92
97 iterator begin() { return tok_->begin(); }
102 iterator end() { return tok_->end(); }
103
108 template <class T = std::string>
109 std::vector<T> ToVector() {
110 std::vector<T> result;
111 for (auto &seg : *this) {
112 result.push_back(internal::convert_impl(seg, internal::type<T>{}));
113 }
114 return result;
115 }
116
117 private:
118 std::unique_ptr<boost::tokenizer<boost::char_separator<char>>> tok_;
119 std::string str_;
120};
121
122// Matches a string against a wildcard string such as &quot;*.*&quot; or
123// &quot;bl?h.*&quot; etc. This is good for file globbing or to match hostmasks.
124int wildcmp(const char *wild, const char *string);
125int wildcmp(const std::string &wild, const std::string &string);
126
127namespace internal {
128
129template <class T>
130inline std::vector<T> convert_impl(const std::string &s, type<std::vector<T>>) {
131 return Tokenizer(s, " ,\n\t").ToVector<T>();
132}
133
134template <class T>
135inline Eigen::Matrix<T, Eigen::Dynamic, 1> convert_impl(
136 const std::string &s, type<Eigen::Matrix<T, Eigen::Dynamic, 1>>) {
137 std::vector<T> tmp = convert_impl(s, type<std::vector<T>>{});
138 return Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1>>(tmp.data(),
139 tmp.size());
140}
141
142template <class T>
143inline Eigen::Matrix<T, 3, 1> convert_impl(const std::string &s,
144 type<Eigen::Matrix<T, 3, 1>>) {
145 std::vector<T> tmp = convert_impl(s, type<std::vector<T>>{});
146 if (tmp.size() != 3) {
147 throw std::runtime_error("Vector has " + std::to_string(tmp.size()) +
148 " instead of 3 entries");
149 }
150 return {tmp[0], tmp[1], tmp[2]};
151}
152} // namespace internal
153
154template <class T>
155T convertFromString(const std::string &s) {
157}
158
159} // namespace tools
160} // namespace votca
161
162#endif // VOTCA_TOOLS_TOKENIZER_H
break string into words
Definition tokenizer.h:72
iterator end()
end iterator
Definition tokenizer.h:102
iterator begin()
iterator to first element
Definition tokenizer.h:97
Tokenizer(const std::string &str, const std::string &separators)
Definition tokenizer.h:90
boost::tokenizer< boost::char_separator< char > >::iterator iterator
Definition tokenizer.h:74
Tokenizer(const std::string &str, const char *separators)
startup tokenization
Definition tokenizer.h:85
std::vector< T > ToVector()
store all words in a vector of type T, does type conversion.
Definition tokenizer.h:109
std::unique_ptr< boost::tokenizer< boost::char_separator< char > > > tok_
Definition tokenizer.h:118
T convert_impl(const std::string &s, type< T >)
Definition tokenizer.h:43
int wildcmp(const char *wild, const char *string)
Definition tokenizer.cc:28
T convertFromString(const std::string &s)
Definition tokenizer.h:155
base class for all analysis tools
Definition basebead.h:33