Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tf_parse.h
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
20#ifndef _SOURCE_TF_PARSE_H_
21#define _SOURCE_TF_PARSE_H_
22
23#include <types/tf_cast.h>
24
25#include <sstream>
26#include <string>
27#include <vector>
28
29
30namespace TissueForge::parse {
31
32
36 void logMessageNoKwarg();
37
43 void logMessageNoKwargFound(const std::string &s);
44
51 bool has_kwarg(const std::string &arg);
52
60 bool has_kwarg(const std::string &arg, const std::string &kwarg);
61
69 bool has_kwarg(const std::vector<std::string> &args, const std::string &kwarg);
70
79 std::string get_kwarg(const std::string &arg);
80
90 std::string kwarg_findStr(const std::vector<std::string> &args, const std::string &kwarg);
91
101 std::string kwarg_strVal(const std::string &arg, const std::string &kwarg);
102
113 template<typename T>
114 T kwarg_getVal(const std::string &arg, const std::string &kwarg) {
115 if(!has_kwarg(arg, kwarg)) {
116 logMessageNoKwargFound(kwarg);
117 return 0;
118 }
119
120 return TissueForge::cast<std::string, T>(kwarg_strVal(arg, kwarg));
121 }
122
133 template<typename T>
134 T kwarg_getVal(const std::vector<std::string> &args, const std::string &kwarg) {
135 for(auto &s : args)
136 if(has_kwarg(s, kwarg)) return kwarg_getVal<T>(s, kwarg);
137
138 logMessageNoKwargFound(kwarg);
139 return 0;
140 }
141
150 std::pair<std::string, std::string> kwarg_getNameVal(const std::string &arg);
151
160 template<typename T>
161 std::string kwarg_valSetStr(const std::string &kwarg, const T &t) {
162 return kwarg + "=" + TissueForge::cast<T, std::string>(t);
163 }
164
174 std::string kwargVal(const std::string &kwarg, const std::string &keyword);
175
183 std::string kwargVal(const std::vector<std::string> &kwargs, const std::string &keyword);
184
185 // Vector format: vec{a, b, c} <-> str=a,b,c
186
194 template<typename T>
195 std::vector<T> strToVec(const std::string &s) {
196 std::vector<T> result;
197
198 std::stringstream ss(s);
199 std::string str;
200 while(getline(ss, str, ',')) result.push_back(TissueForge::cast<std::string, T>(str));
201
202 return result;
203 }
204
212 template<typename T>
213 std::string vecToStr(const std::vector<T> &v) {
214 std::string result;
215 if(v.size() == 0) return result;
216
217 result = TissueForge::cast<T, std::string>(v[0]);
218 if(v.size() == 1) return result;
219
220 for(int i = 1; i < v.size(); ++i) result += "," + TissueForge::cast<T, std::string>(v[i]);
221
222 return result;
223 }
224
225 // Heterogeneous map: {"A", {a, b, c}}, {"B", {1, 2, 3}} <-> {A=a,b,c;B=1,2,3}
226
234 bool has_mapKwarg(const std::string &arg, const std::string &kwarg);
235
243 bool has_mapKwarg(const std::vector<std::string> &args, const std::string &kwarg);
244
254 std::string kwarg_findMapStr(const std::vector<std::string> &args, const std::string &kwarg);
255
262 std::string mapStrip(const std::string &arg);
263
271 std::string mapStrip(const std::string &arg, const std::string &kwarg);
272
279 std::vector<std::string> mapStrToStrVec(const std::string &s);
280
287 std::string mapVecToStr(const std::vector<std::string> &v);
288
296 std::string kwarg_strMapVal(const std::string &arg, const std::string &kwarg);
297
305 std::string kwarg_strMapVal(const std::vector<std::string> &args, const std::string &kwarg);
306
313 std::pair<std::string, std::vector<std::string>> kwarg_getNameMapVals(const std::string &arg);
314
315};
316
317#endif // _SOURCE_TF_PARSE_H_