parse_utils.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #include "parse_utils.h"
11 #include <mrpt/core/exceptions.h>
12 #include <mrpt/system/filesystem.h>
13 #include <mrpt/system/os.h>
15 #include <algorithm>
16 #include <cstdlib>
17 #include <iostream>
18 
19 using namespace mvsim;
20 
21 static std::string parseEnvVars(const std::string& text)
22 {
24 
25  const auto start = text.find("$env{");
26  if (start == std::string::npos) return text;
27 
28  const std::string pre = text.substr(0, start);
29  const std::string post = text.substr(start + 5);
30 
31  const auto post_end = post.find('}');
32  if (post_end == std::string::npos)
33  {
35  "Column=%u: Cannot find matching `}` for `$env{` in: `%s`",
36  static_cast<unsigned int>(start), text.c_str());
37  }
38 
39  const auto varname = post.substr(0, post_end);
40  std::string varvalue;
41  const char* v = ::getenv(varname.c_str());
42  if (v != nullptr)
43  varvalue = std::string(v);
44  else
45  {
47  "parseEnvVars(): Undefined environment variable found: $env{%s}",
48  varname.c_str());
49  }
50 
51  return parseEnvVars(pre + varvalue + post.substr(post_end + 1));
53 }
54 
55 static std::string parseVars(
56  const std::string& text,
57  const std::map<std::string, std::string>& variableNamesValues)
58 {
60 
61  const auto start = text.find("${");
62  if (start == std::string::npos) return text;
63 
64  const std::string pre = text.substr(0, start);
65  const std::string post = text.substr(start + 2);
66 
67  const auto post_end = post.find('}');
68  if (post_end == std::string::npos)
69  {
71  "Column=%u: Cannot find matching `}` for `${` in: `%s`",
72  static_cast<unsigned int>(start), text.c_str());
73  }
74 
75  const auto varname = post.substr(0, post_end);
76  std::string varvalue;
77  if (const auto it = variableNamesValues.find(varname);
78  it != variableNamesValues.end())
79  {
80  varvalue = it->second;
81  }
82  else
83  {
85  "parseEnvVars(): Undefined variable found: $env{%s}",
86  varname.c_str());
87  }
88 
89  return parseEnvVars(pre + varvalue + post.substr(post_end + 1));
91 }
92 
93 static std::string parseCmdRuns(const std::string& text)
94 {
96 
97  const auto start = text.find("$(");
98  if (start == std::string::npos) return text;
99 
100  const std::string pre = text.substr(0, start);
101  const std::string post = text.substr(start + 2);
102 
103  const auto post_end = post.find(')');
104  if (post_end == std::string::npos)
105  {
107  "Column=%u: Cannot find matching `)` for `$(` in: `%s`",
108  static_cast<unsigned int>(start), text.c_str());
109  }
110 
111  const auto cmd = post.substr(0, post_end);
112 
113  // Launch command and get console output:
114  std::string cmdOut;
115 
116  int ret = mrpt::system::executeCommand(cmd, &cmdOut);
117  if (ret != 0)
118  {
120  "Error (retval=%i) executing external command: `%s`", ret,
121  cmd.c_str());
122  }
123  // Clear whitespaces:
124  cmdOut = mrpt::system::trim(cmdOut);
125  cmdOut.erase(std::remove(cmdOut.begin(), cmdOut.end(), '\r'), cmdOut.end());
126  cmdOut.erase(std::remove(cmdOut.begin(), cmdOut.end(), '\n'), cmdOut.end());
127 
128  return parseCmdRuns(pre + cmdOut + post.substr(post_end + 1));
130 }
131 
132 std::string mvsim::parse(
133  const std::string& input,
134  const std::map<std::string, std::string>& variableNamesValues)
135 {
136  std::string s = input;
137 
138  std::string prevValue = s;
139  for (int iter = 0; iter < 10; iter++)
140  {
141  s = parseVars(s, variableNamesValues);
142  s = parseEnvVars(s);
143  s = parseCmdRuns(s);
144  // We may need to iterate since, in general, each expression generator
145  // might generate another kind of expression:
146  if (s == prevValue) break;
147  prevValue = s;
148  }
149 
150  return s;
151 }
const GLdouble * v
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLenum GLenum GLenum input
GLuint start
static std::string parseCmdRuns(const std::string &text)
Definition: parse_utils.cpp:93
int BASE_IMPEXP executeCommand(const std::string &command, std::string *output=NULL, const std::string &mode="r")
#define MRPT_TRY_END
GLdouble s
static std::string parseVars(const std::string &text, const std::map< std::string, std::string > &variableNamesValues)
Definition: parse_utils.cpp:55
std::string parse(const std::string &input, const std::map< std::string, std::string > &variableNamesValues={})
static std::string parseEnvVars(const std::string &text)
Definition: parse_utils.cpp:21
TCLAP::CmdLine cmd("mvsim", ' ',"version", false)
#define MRPT_TRY_START
std::string BASE_IMPEXP trim(const std::string &str)


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51