FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
regexConfParser.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015, Siemens AG
3  * Author: Maximilian Huber
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
22 #include "regexConfParser.hpp"
23 #include <string>
24 #include <iostream>
25 
26 using namespace std;
27 
34 RegexMap readConfStreamToMap(std::istringstream& stream,
35  const bool isVerbosityDebug)
36 {
37  map<string, string> regexMap;
38  for (string line; getline(stream, line); )
39  addRegexToMap(regexMap, line, isVerbosityDebug);
40 
41  return regexMap;
42 }
43 
47 RegexMap readConfStreamToMap(std::ifstream& stream,
48  const bool isVerbosityDebug)
49 {
50  map<string, string> regexMap;
51  for (string line; getline(stream, line); )
52  addRegexToMap(regexMap, line, isVerbosityDebug);
53  stream.close();
54  return regexMap;
55 }
56 
64 void addRegexToMap(/*in and out*/ RegexMap& regexMap,
65  const std::string& regexDesc,
66  const bool isVerbosityDebug)
67 {
68  if (regexDesc[0] == '#')
69  return;
70 
71  istringstream is_line(regexDesc);
72  string key, value;
73  if (getline(is_line, key, '='))
74  {
75  if(getline(is_line, value))
76  {
77  value=replaceTokens(regexMap, value);
78  regexMap[key]=value;
79  if (isVerbosityDebug)
80  cout << "loaded or updated regex definition: " << key << " -> \"" << value << "\"" << endl;
81  }
82  else
83  {
84  cout << "empty regex definition in conf: \"" << regexDesc << "\"" << endl;
85  }
86  }
87  else
88  {
89  cout << "bad regex definition in conf: \"" << regexDesc << "\"" << endl;
90  }
91 }
92 
100 string replaceTokens(/*in*/ RegexMap& regexMap,
101  const string& constInput)
102 {
103 #define RGX_SEPARATOR_LEFT "__"
104 #define RGX_SEPARATOR_RIGHT RGX_SEPARATOR_LEFT
105 #define RGX_SEPARATOR_LEN 2
106 
107  string input(constInput);
108  stringstream output;
109 
110  size_t pos = 0;
111  string token;
112  while ((pos = input.find(RGX_SEPARATOR_LEFT)) != string::npos) // find start of the next token
113  {
114  output << input.substr(0, pos);
115  input.erase(0, pos + RGX_SEPARATOR_LEN);
116 
117  if ((pos = input.find(RGX_SEPARATOR_RIGHT)) != string::npos) // find end of token
118  {
119  output << regexMap[input.substr(0, pos)];
120  input.erase(0, pos + RGX_SEPARATOR_LEN);
121 
122  }
123  else
124  {
125  cout << "uneven number of delimiters: " << constInput << endl;
126  }
127  }
128  output << input;
129  return output.str();
130 }
RegexMap readConfStreamToMap(std::istringstream &stream, const bool isVerbosityDebug)
Read a string stream and crate a RegexMap.
void addRegexToMap(RegexMap &regexMap, const std::string &regexDesc, const bool isVerbosityDebug)
Given a single line as &#39;key=value&#39; pair, create a RegexMap.
string replaceTokens(RegexMap &regexMap, const string &constInput)
Removes tokens separated by RGX_SEPARATOR_LEFT in constInput using regexMap.