YARP
Yet Another Robot Platform
textparser.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 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::getEnvironment(envName.c_str());
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
LogStream.h
yarp::manager::TextParser::TextParser
TextParser()
Definition: textparser.h:31
Network.h
yarp::conf::environment::getEnvironment
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a variable from the environment.
Definition: environment.h:31
yarp::manager::OSTRINGSTREAM
std::stringstream OSTRINGSTREAM
Definition: utility.h:52
yarp::manager::ErrorLogger
Singleton class ErrorLogger.
Definition: utility.h:60
utility.h
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::manager::TextParser
Definition: textparser.h:24
yarp::manager::ErrorLogger::Instance
static ErrorLogger * Instance()
Singleton class ErrorLogger.
Definition: utility.cpp:102
yarp::manager::ErrorLogger::addWarning
void addWarning(const char *szWarning)
Definition: utility.cpp:108
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
environment.h
yarp::manager::TextParser::parseText
std::string parseText(const char *element)
Definition: textparser.h:45
yarp::manager::TextParser::addVariable
bool addVariable(const std::string &key, const std::string &value)
Definition: textparser.h:33