FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
regexConfProvider.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  */
21 #include "regexConfProvider.hpp"
22 
23 using namespace std;
24 
31 map<string,RegexMap> RegexConfProvider::_regexMapMap = {};
32 
38 bool testIfFileExists(const string& filename)
39 {
40  ifstream f(filename);
41  bool exists = f.good();
42  f.close();
43  return exists;
44 }
45 
58 string getRegexConfFile(const string& identity)
59 {
60  string confInSameDir(identity + ".conf");
61 
62  string confRelativeToTestDir("../../agent/" + identity + ".conf");
63 
64  string confInInstallDir((sysconfigdir ? string(sysconfigdir) : "/usr/local/share/fossology/")
65  + identity + "/agent/" + identity + ".conf");
66 
67  if(testIfFileExists( confInSameDir ))
68  {
69  return confInSameDir;
70  }
71  else if(testIfFileExists( confRelativeToTestDir ))
72  {
73  return confRelativeToTestDir;
74  }
75  else
76  {
77  return confInInstallDir;
78  }
79 }
80 
84 RegexConfProvider::RegexConfProvider(const bool isVerbosityDebug)
85  : _isVerbosityDebug(isVerbosityDebug) {}
86 
93 bool RegexConfProvider::getRegexConfStream(const string& identity,
94  /*out*/ ifstream& stream)
95 {
96  string confFile = getRegexConfFile(identity);
97 
99  cout << "try to open conf: " << confFile << endl;
100  stream.open(confFile.c_str());
101 
102  return stream.is_open();
103 }
104 
110 void RegexConfProvider::maybeLoad(const std::string& identity)
111 {
112  map<string,RegexMap>& rmm = RegexConfProvider::_regexMapMap;
113  if (rmm.find(identity) == rmm.end())
114  {
115 #pragma omp critical(rmm)
116  {
117  if (rmm.find(identity) == rmm.end())
118  {
119  ifstream stream;
120  if (getRegexConfStream(identity, stream))
121  {
122  rmm[identity] = readConfStreamToMap(stream, _isVerbosityDebug);
123  stream.close();
124  }
125  else
126  {
127  cout << "cannot open regex definitions in conf: " << getRegexConfFile(identity) << endl;
128  }
129  }
130  else if (_isVerbosityDebug)
131  {
132  cout << "the identity " << identity << " is already loaded" << endl;
133  }
134  }
135  }
136 }
137 
142 void RegexConfProvider::maybeLoad(const string& identity,
143  istringstream& stream)
144 {
145  map<string,RegexMap>& rmm = RegexConfProvider::_regexMapMap;
146  if (rmm.find(identity) == rmm.end())
147  {
148 #pragma omp critical(rmm)
149  {
150  if (rmm.find(identity) == rmm.end())
151  {
152  rmm[identity] = readConfStreamToMap(stream, _isVerbosityDebug);
153  }
154  else if (_isVerbosityDebug)
155  {
156  cout << "the identity " << identity << " is already loaded" << endl;
157  }
158  }
159  }
160 }
161 
168 const char* RegexConfProvider::getRegexValue(const string& identity,
169  const string& key)
170 {
171  const string* rv;
172 #pragma omp critical(rmm)
173  {
174  rv = &(RegexConfProvider::_regexMapMap[identity][key]);
175  }
176  return (*rv).c_str();
177 }
RegexMap readConfStreamToMap(std::istringstream &stream, const bool isVerbosityDebug)
Read a string stream and crate a RegexMap.
RegexConfProvider(const bool isVerbosityDebug=false)
Constructor to set verbosity level.
static std::map< std::string, RegexMap > _regexMapMap
Map to store RegexMap with a string key.
char * sysconfigdir
const char * getRegexValue(const std::string &name, const std::string &key)
Get the regex as string from the RegexMap.
bool testIfFileExists(const string &filename)
Check if a given file exists.
string getRegexConfFile(const string &identity)
Get the regex conf file.
bool getRegexConfStream(const std::string &identity, std::ifstream &stream)
Get file stream for regex conf file.
int exists
Default not exists.
Definition: run_tests.c:31
void maybeLoad(const std::string &identity)
Check if identity already loaded in RegexMap, if not load them.