| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "config-precomp.h" // Precompiled headers |
| 11 |
// |
| 12 |
#include <mrpt/config/config_parser.h> |
| 13 |
#include <mrpt/core/exceptions.h> |
| 14 |
#include <mrpt/expr/CRuntimeCompiledExpression.h> |
| 15 |
#include <mrpt/system/string_utils.h> |
| 16 |
|
| 17 |
#include <cstring> // strncmp |
| 18 |
#include <map> |
| 19 |
|
| 20 |
namespace mrpt::config::internal |
| 21 |
{ |
| 22 |
struct ParseContext |
| 23 |
{ |
| 24 |
std::map<std::string, std::string> defined_vars; |
| 25 |
std::map<std::string, double> defined_vars_values; |
| 26 |
unsigned int line_count = 1; |
| 27 |
}; |
| 28 |
std::string parse_process_var_eval(const ParseContext& pc, std::string expr); |
| 29 |
|
| 30 |
void parse_process_var_define( |
| 31 |
ParseContext& pc, const std::string& var_name, const std::string& var_value); |
| 32 |
|
| 33 |
} // namespace mrpt::config::internal |
| 34 |
|
| 35 |
namespace mci = mrpt::config::internal; |
| 36 |
|
| 37 |
// Return a string or a number (as string) if expr = "$eval{...}" |
| 38 |
std::string mci::parse_process_var_eval(const mci::ParseContext& pc, std::string expr) |
| 39 |
{ |
| 40 |
expr = mrpt::system::trim(expr); |
| 41 |
while (expr.size() > 5) |
| 42 |
{ |
| 43 |
auto p = expr.find("$env{"); |
| 44 |
if (p != std::string::npos) |
| 45 |
{ |
| 46 |
auto pend = expr.find("}", p); |
| 47 |
if (pend == std::string::npos) |
| 48 |
throw std::runtime_error( |
| 49 |
mrpt::format("Line %u: Expected closing `}` near: `%s`", pc.line_count, expr.c_str())); |
| 50 |
const auto substr = expr.substr(p + 5, pend - p - 5); |
| 51 |
std::string new_expr = expr.substr(0, p); |
| 52 |
auto env_val = ::getenv(substr.c_str()); |
| 53 |
if (env_val) new_expr += std::string(env_val); |
| 54 |
new_expr += expr.substr(pend + 1); |
| 55 |
new_expr.swap(expr); |
| 56 |
} |
| 57 |
else if ((p = expr.find("$eval{")) != std::string::npos) |
| 58 |
{ |
| 59 |
auto pend = expr.find("}", p); |
| 60 |
if (pend == std::string::npos) |
| 61 |
throw std::runtime_error( |
| 62 |
mrpt::format("Line %u: Expected closing `}` near: `%s`", pc.line_count, expr.c_str())); |
| 63 |
|
| 64 |
const auto substr = expr.substr(p + 6, pend - p - 6); |
| 65 |
mrpt::expr::CRuntimeCompiledExpression cexpr; |
| 66 |
cexpr.compile(substr, pc.defined_vars_values, mrpt::format("Line %u: ", pc.line_count)); |
| 67 |
|
| 68 |
std::string new_expr = expr.substr(0, p); |
| 69 |
new_expr += mrpt::format("%e", cexpr.eval()); |
| 70 |
new_expr += expr.substr(pend + 1); |
| 71 |
new_expr.swap(expr); |
| 72 |
} |
| 73 |
else |
| 74 |
break; // nothing else to evaluate |
| 75 |
} |
| 76 |
return expr; |
| 77 |
} |
| 78 |
|
| 79 |
void mci::parse_process_var_define( |
| 80 |
mci::ParseContext& pc, const std::string& var_name, const std::string& var_value) |
| 81 |
{ |
| 82 |
if (!var_name.empty()) |
| 83 |
{ |
| 84 |
pc.defined_vars[var_name] = var_value; |
| 85 |
if (!var_value.empty()) |
| 86 |
{ |
| 87 |
pc.defined_vars_values[var_name] = ::atof(parse_process_var_eval(pc, var_value).c_str()); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
std::string mrpt::config::config_parser(const std::string& input) |
| 93 |
{ |
| 94 |
const auto in_str = input.data(); |
| 95 |
const auto in_len = input.size(); |
| 96 |
|
| 97 |
std::string output; |
| 98 |
output.reserve(in_len); |
| 99 |
|
| 100 |
mci::ParseContext pc; |
| 101 |
size_t i = 0; |
| 102 |
while (i < in_len) |
| 103 |
{ |
| 104 |
const char c = in_str[i]; |
| 105 |
if (c == '\n') |
| 106 |
{ |
| 107 |
pc.line_count++; |
| 108 |
} |
| 109 |
|
| 110 |
if (c == '\\' && i < in_len - 1 && (in_str[i + 1] == '\r' || in_str[i + 1] == '\n')) |
| 111 |
{ |
| 112 |
// Skip the backslash + one newline: CR "\r", LF "\n", CR+LF |
| 113 |
// "\r\n" |
| 114 |
if (i < in_len - 2 && in_str[i + 1] == '\r' && in_str[i + 2] == '\n') |
| 115 |
{ |
| 116 |
// out_len += 0; |
| 117 |
i += 3; |
| 118 |
} |
| 119 |
else if (in_str[i + 1] == '\r' || in_str[i + 1] == '\n') |
| 120 |
{ |
| 121 |
// out_len += 0; |
| 122 |
i += 2; |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
throw std::runtime_error( |
| 127 |
"[mrpt::config::config_parser] parse error, shouldn't " |
| 128 |
"reach here!"); |
| 129 |
} |
| 130 |
} |
| 131 |
else |
| 132 |
{ |
| 133 |
// Handle "@define varname value" |
| 134 |
if (in_len > i + 7 && !::strncmp(in_str + i, "@define", 7)) |
| 135 |
{ |
| 136 |
// Extract rest of this line: |
| 137 |
i += 7; |
| 138 |
std::string var_name, var_value; |
| 139 |
bool in_var_name = false, done_var_name = false; |
| 140 |
while (i < in_len && in_str[i] != '\r' && in_str[i] != '\n') |
| 141 |
{ |
| 142 |
const char ch = in_str[i]; |
| 143 |
i++; |
| 144 |
if (ch != ' ' && ch != '\t') |
| 145 |
{ |
| 146 |
// not whitespace |
| 147 |
if (!in_var_name && !done_var_name) |
| 148 |
{ |
| 149 |
in_var_name = true; |
| 150 |
} |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
// whitespace |
| 155 |
if (in_var_name) |
| 156 |
{ |
| 157 |
in_var_name = false; |
| 158 |
done_var_name = true; |
| 159 |
} |
| 160 |
} |
| 161 |
if (in_var_name) |
| 162 |
{ |
| 163 |
var_name += ch; |
| 164 |
} |
| 165 |
if (done_var_name) |
| 166 |
{ |
| 167 |
var_value += ch; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
parse_process_var_define(pc, var_name, var_value); |
| 172 |
continue; |
| 173 |
} |
| 174 |
|
| 175 |
// Handle "${varname}" |
| 176 |
if (in_len > i + 4 && in_str[i] == '$' && in_str[i + 1] == '{') |
| 177 |
{ |
| 178 |
// extract varname: |
| 179 |
i += 2; |
| 180 |
std::string varname; |
| 181 |
bool end_ok = false; |
| 182 |
while (i < in_len && in_str[i] != '\n' && in_str[i] != '\r') |
| 183 |
{ |
| 184 |
const char ch = in_str[i]; |
| 185 |
i++; |
| 186 |
if (ch == '}') |
| 187 |
{ |
| 188 |
end_ok = true; |
| 189 |
break; |
| 190 |
} |
| 191 |
varname += ch; |
| 192 |
} |
| 193 |
if (!end_ok) |
| 194 |
{ |
| 195 |
throw std::runtime_error(mrpt::format( |
| 196 |
"Line %u: Expected closing `}` near: `%s`", pc.line_count, varname.c_str())); |
| 197 |
} |
| 198 |
|
| 199 |
const auto it = pc.defined_vars.find(varname); |
| 200 |
if (it == pc.defined_vars.end()) |
| 201 |
throw std::runtime_error( |
| 202 |
mrpt::format("Line %u: Unknown variable `${%s}`", pc.line_count, varname.c_str())); |
| 203 |
|
| 204 |
const auto str_out = parse_process_var_eval(pc, it->second); |
| 205 |
|
| 206 |
output += str_out; |
| 207 |
continue; |
| 208 |
} |
| 209 |
|
| 210 |
// Handle "$eval{expression}" |
| 211 |
if (in_len > i + 7 && !strncmp(in_str + i, "$eval{", 6)) |
| 212 |
{ |
| 213 |
// extract expression: |
| 214 |
std::string expr; |
| 215 |
bool end_ok = false; |
| 216 |
while (i < in_len && in_str[i] != '\n' && in_str[i] != '\r') |
| 217 |
{ |
| 218 |
const char ch = in_str[i]; |
| 219 |
i++; |
| 220 |
expr += ch; |
| 221 |
if (ch == '}') |
| 222 |
{ |
| 223 |
end_ok = true; |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
if (!end_ok) |
| 228 |
{ |
| 229 |
throw std::runtime_error(mrpt::format( |
| 230 |
"Line %u: Expected closing `}` near: `%s`", pc.line_count, expr.c_str())); |
| 231 |
} |
| 232 |
|
| 233 |
const std::string res = parse_process_var_eval(pc, expr); |
| 234 |
output += res; |
| 235 |
continue; |
| 236 |
} |
| 237 |
|
| 238 |
// Handle "$env{var}" |
| 239 |
if (in_len > i + 6 && !strncmp(in_str + i, "$env{", 5)) |
| 240 |
{ |
| 241 |
// extract expression: |
| 242 |
std::string expr; |
| 243 |
bool end_ok = false; |
| 244 |
while (i < in_len && in_str[i] != '\n' && in_str[i] != '\r') |
| 245 |
{ |
| 246 |
const char ch = in_str[i]; |
| 247 |
i++; |
| 248 |
expr += ch; |
| 249 |
if (ch == '}') |
| 250 |
{ |
| 251 |
end_ok = true; |
| 252 |
break; |
| 253 |
} |
| 254 |
} |
| 255 |
if (!end_ok) |
| 256 |
{ |
| 257 |
throw std::runtime_error(mrpt::format( |
| 258 |
"Line %u: Expected closing `}` near: `%s`", pc.line_count, expr.c_str())); |
| 259 |
} |
| 260 |
|
| 261 |
const std::string res = parse_process_var_eval(pc, expr); |
| 262 |
output += res; |
| 263 |
continue; |
| 264 |
} |
| 265 |
|
| 266 |
// Normal case: |
| 267 |
output += c; |
| 268 |
i++; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
return output; |
| 273 |
} |