YARP
Yet Another Robot Platform
textparser.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef TEXTPARSER_H
7 #define TEXTPARSER_H
8 #include <string>
9 #include <map>
10 #include <yarp/conf/environment.h>
11 #include <yarp/os/Network.h>
12 #include <yarp/os/LogStream.h>
13 #include <iostream>
14 #include <yarp/manager/utility.h>
15 
16 namespace yarp{
17 namespace manager{
18 
19 
21 {
22  typedef std::map<std::string, std::string> VarMap;
23 
24  VarMap variables;
25  ErrorLogger* logger;
26  OSTRINGSTREAM war;
27 public:
29 
30  bool addVariable(const std::string& key, const std::string& value)
31  {
32  if (key.empty())
33  {
34  war << "TextParser: empty key on variable setting..";
35  if (logger) {
36  logger->addWarning(war);
37  }
38  return false;
39  }
40  variables[key] = parseText(value.c_str());
41  return true;
42  }
43 
44  std::string parseText(const char *element)
45  {
46 
47  std::string ret, startKeyword, endKeyword;
48  size_t s, e;
49 
50  ret = "";
51 
52  if(element)
53  {
54  ret = element;
55  startKeyword = "$ENV{";
56  endKeyword = "}";
57  bool badSymbol = ret.find("$") != std::string::npos;
58  s = ret.find(startKeyword);
59  e = ret.find(endKeyword, s);
60 
61  if(s != std::string::npos && e != std::string::npos)
62  {
63  std::string envName, envValue;
64 
65  envName = ret.substr(s + startKeyword.size(), e - s -startKeyword.size());
66  envValue = yarp::conf::environment::get_string(envName);
67  ret = ret.substr(0, s)+ envValue + ret.substr(e + endKeyword.size(), ret.size() - endKeyword.size());
68  return parseText(ret.c_str());
69  }
70 
71  ret = element;
72  startKeyword = "${";
73  endKeyword = "}";
74  s = ret.find(startKeyword);
75  e = ret.find(endKeyword, s);
76 
77  if(s != std::string::npos && e != std::string::npos)
78  {
79  std::string envName, envValue;
80 
81  envName = ret.substr(s + startKeyword.size(), e - s -startKeyword.size());
82  envValue = variables[envName];
83  ret = ret.substr(0, s)+ envValue + ret.substr(e + endKeyword.size(), ret.size() - endKeyword.size());
84  return parseText(ret.c_str());
85  }
86 
87  if(badSymbol)
88  {
89  war << "use of symbol '$' detected but no keyword understood.. possible use: ${foo} for internal variable or $ENV{foo} for environment variable";
90  if (logger) {
91  logger->addWarning(war);
92  }
93  }
94  }
95 
96  return ret;
97 
98  }
99 };
100 }//manager
101 }//yarp
102 
103 #endif // TEXTPARSER_H
bool ret
Singleton class ErrorLogger.
Definition: utility.h:57
void addWarning(const char *szWarning)
Definition: utility.cpp:104
static ErrorLogger * Instance()
Singleton class ErrorLogger.
Definition: utility.cpp:98
bool addVariable(const std::string &key, const std::string &value)
Definition: textparser.h:30
std::string parseText(const char *element)
Definition: textparser.h:44
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:68
std::stringstream OSTRINGSTREAM
Definition: utility.h:49
The main, catch-all namespace for YARP.
Definition: dirs.h:16