FOSSology  4.4.0
Open Source License Compliance by Open Source Software
regexConfProvider.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 */
10 #include "regexConfProvider.hpp"
11 
12 using namespace std;
13 
20 map<string,RegexMap> RegexConfProvider::_regexMapMap = {};
21 
27 bool testIfFileExists(const string& filename)
28 {
29  ifstream f(filename);
30  bool exists = f.good();
31  f.close();
32  return exists;
33 }
34 
47 string getRegexConfFile(const string& identity)
48 {
49  string confInSameDir(identity + ".conf");
50 
51  string confRelativeToTestDir("../../../../src/copyright/agent/" + identity + ".conf");
52 
53  string confInInstallDir((sysconfigdir ? string(sysconfigdir) + "/mods-enabled/" : "/usr/local/share/fossology/")
54  + identity + "/agent/" + identity + ".conf");
55 
56  if(testIfFileExists( confInSameDir ))
57  {
58  return confInSameDir;
59  }
60  else if(testIfFileExists( confRelativeToTestDir ))
61  {
62  return confRelativeToTestDir;
63  }
64  else
65  {
66  return confInInstallDir;
67  }
68 }
69 
73 RegexConfProvider::RegexConfProvider(const bool isVerbosityDebug)
74  : _isVerbosityDebug(isVerbosityDebug) {}
75 
82 bool RegexConfProvider::getRegexConfStream(const string& identity,
83  /*out*/ ifstream& stream)
84 {
85  string confFile = getRegexConfFile(identity);
86 
88  cout << "try to open conf: " << confFile << endl;
89  stream.open(confFile.c_str());
90 
91  return stream.is_open();
92 }
93 
99 void RegexConfProvider::maybeLoad(const std::string& identity)
100 {
101  map<string,RegexMap>& rmm = RegexConfProvider::_regexMapMap;
102  if (rmm.find(identity) == rmm.end())
103  {
104 #pragma omp critical(rmm)
105  {
106  if (rmm.find(identity) == rmm.end())
107  {
108  ifstream stream;
109  if (getRegexConfStream(identity, stream))
110  {
111  rmm[identity] = readConfStreamToMap(stream, _isVerbosityDebug);
112  stream.close();
113  }
114  else
115  {
116  cout << "cannot open regex definitions in conf: " << getRegexConfFile(identity) << endl;
117  }
118  }
119  else if (_isVerbosityDebug)
120  {
121  cout << "the identity " << identity << " is already loaded" << endl;
122  }
123  }
124  }
125 }
126 
131 void RegexConfProvider::maybeLoad(const string& identity,
132  istringstream& stream)
133 {
134  map<string,RegexMap>& rmm = RegexConfProvider::_regexMapMap;
135  if (rmm.find(identity) == rmm.end())
136  {
137 #pragma omp critical(rmm)
138  {
139  if (rmm.find(identity) == rmm.end())
140  {
141  rmm[identity] = readConfStreamToMap(stream, _isVerbosityDebug);
142  }
143  else if (_isVerbosityDebug)
144  {
145  cout << "the identity " << identity << " is already loaded" << endl;
146  }
147  }
148  }
149 }
150 
157 const char* RegexConfProvider::getRegexValue(const string& identity,
158  const string& key)
159 {
160  const string* rv;
161 #pragma omp critical(rmm)
162  {
163  rv = &(RegexConfProvider::_regexMapMap[identity][key]);
164  }
165  return (*rv).c_str();
166 }
void maybeLoad(const std::string &identity)
Check if identity already loaded in RegexMap, if not load them.
bool getRegexConfStream(const std::string &identity, std::ifstream &stream)
Get file stream for regex conf file.
RegexConfProvider(const bool isVerbosityDebug=false)
Constructor to set verbosity level.
const char * getRegexValue(const std::string &name, const std::string &key)
Get the regex as string from the RegexMap.
static std::map< std::string, RegexMap > _regexMapMap
Map to store RegexMap with a string key.
char * sysconfigdir
RegexMap readConfStreamToMap(std::istringstream &stream, const bool isVerbosityDebug)
Read a string stream and crate a RegexMap.
string getRegexConfFile(const string &identity)
Get the regex conf file.
bool testIfFileExists(const string &filename)
Check if a given file exists.
int exists
Default not exists.
Definition: run_tests.c:20