YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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>
11#include <yarp/os/Network.h>
12#include <yarp/os/LogStream.h>
13#include <iostream>
15
16namespace yarp::manager {
17
18
20{
21 typedef std::map<std::string, std::string> VarMap;
22
23 VarMap variables;
24 ErrorLogger* logger;
25 OSTRINGSTREAM war;
26public:
28
29 bool addVariable(const std::string& key, const std::string& value)
30 {
31 if (key.empty())
32 {
33 war << "TextParser: empty key on variable setting..";
34 if (logger) {
35 logger->addWarning(war);
36 }
37 return false;
38 }
39 variables[key] = parseText(value.c_str());
40 return true;
41 }
42
43 std::string parseText(const char *element)
44 {
45
46 std::string ret, startKeyword, endKeyword;
47 size_t s, e;
48
49 ret = "";
50
51 if(element)
52 {
53 ret = element;
54 startKeyword = "$ENV{";
55 endKeyword = "}";
56 bool badSymbol = ret.find("$") != std::string::npos;
57 s = ret.find(startKeyword);
58 e = ret.find(endKeyword, s);
59
60 if(s != std::string::npos && e != std::string::npos)
61 {
62 std::string envName, envValue;
63
64 envName = ret.substr(s + startKeyword.size(), e - s -startKeyword.size());
65 envValue = yarp::conf::environment::get_string(envName);
66 ret = ret.substr(0, s)+ envValue + ret.substr(e + endKeyword.size(), ret.size() - endKeyword.size());
67 return parseText(ret.c_str());
68 }
69
70 ret = element;
71 startKeyword = "${";
72 endKeyword = "}";
73 s = ret.find(startKeyword);
74 e = ret.find(endKeyword, s);
75
76 if(s != std::string::npos && e != std::string::npos)
77 {
78 std::string envName, envValue;
79
80 envName = ret.substr(s + startKeyword.size(), e - s -startKeyword.size());
81 envValue = variables[envName];
82 ret = ret.substr(0, s)+ envValue + ret.substr(e + endKeyword.size(), ret.size() - endKeyword.size());
83 return parseText(ret.c_str());
84 }
85
86 if(badSymbol)
87 {
88 war << "use of symbol '$' detected but no keyword understood.. possible use: ${foo} for internal variable or $ENV{foo} for environment variable";
89 if (logger) {
90 logger->addWarning(war);
91 }
92 }
93 }
94
95 return ret;
96
97 }
98};
99} // namespace yarp::manager
100
101#endif // TEXTPARSER_H
bool ret
Singleton class ErrorLogger.
Definition utility.h:58
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:29
std::string parseText(const char *element)
Definition textparser.h:43
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition environment.h:66
std::stringstream OSTRINGSTREAM
Definition utility.h:50