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