FOSSology  4.4.0
Open Source License Compliance by Open Source Software
regexConfParser.cc
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2015 Siemens AG
3  Author: Maximilian Huber
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
11 #include "regexConfParser.hpp"
12 #include <string>
13 #include <iostream>
14 
15 using namespace std;
16 
23 RegexMap readConfStreamToMap(std::istringstream& stream,
24  const bool isVerbosityDebug)
25 {
26  map<string, string> regexMap;
27  for (string line; getline(stream, line); )
28  addRegexToMap(regexMap, line, isVerbosityDebug);
29 
30  return regexMap;
31 }
32 
36 RegexMap readConfStreamToMap(std::ifstream& stream,
37  const bool isVerbosityDebug)
38 {
39  map<string, string> regexMap;
40  for (string line; getline(stream, line); )
41  addRegexToMap(regexMap, line, isVerbosityDebug);
42  stream.close();
43  return regexMap;
44 }
45 
53 void addRegexToMap(/*in and out*/ RegexMap& regexMap,
54  const std::string& regexDesc,
55  const bool isVerbosityDebug)
56 {
57  if (regexDesc[0] == '#')
58  return;
59 
60  istringstream is_line(regexDesc);
61  string key, value;
62  if (getline(is_line, key, '='))
63  {
64  if(getline(is_line, value))
65  {
66  value=replaceTokens(regexMap, value);
67  regexMap[key]=value;
68  if (isVerbosityDebug)
69  cout << "loaded or updated regex definition: " << key << " -> \"" << value << "\"" << endl;
70  }
71  else
72  {
73  cout << "empty regex definition in conf: \"" << regexDesc << "\"" << endl;
74  }
75  }
76  else
77  {
78  cout << "bad regex definition in conf: \"" << regexDesc << "\"" << endl;
79  }
80 }
81 
89 string replaceTokens(/*in*/ RegexMap& regexMap,
90  const string& constInput)
91 {
92 #define RGX_SEPARATOR_LEFT "__"
93 #define RGX_SEPARATOR_RIGHT RGX_SEPARATOR_LEFT
94 #define RGX_SEPARATOR_LEN 2
95 
96  string input(constInput);
97  stringstream output;
98 
99  size_t pos = 0;
100  string token;
101  while ((pos = input.find(RGX_SEPARATOR_LEFT)) != string::npos) // find start of the next token
102  {
103  output << input.substr(0, pos);
104  input.erase(0, pos + RGX_SEPARATOR_LEN);
105 
106  if ((pos = input.find(RGX_SEPARATOR_RIGHT)) != string::npos) // find end of token
107  {
108  output << regexMap[input.substr(0, pos)];
109  input.erase(0, pos + RGX_SEPARATOR_LEN);
110 
111  }
112  else
113  {
114  cout << "uneven number of delimiters: " << constInput << endl;
115  }
116  }
117  output << input;
118  return output.str();
119 }
void addRegexToMap(RegexMap &regexMap, const std::string &regexDesc, const bool isVerbosityDebug)
Given a single line as 'key=value' pair, create a RegexMap.
RegexMap readConfStreamToMap(std::istringstream &stream, const bool isVerbosityDebug)
Read a string stream and crate a RegexMap.
string replaceTokens(RegexMap &regexMap, const string &constInput)
Removes tokens separated by RGX_SEPARATOR_LEFT in constInput using regexMap.