FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
OjoAgent.cc
1 /*
2  * Copyright (C) 2019, Siemens AG
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  */
17 
18 #include "OjoAgent.hpp"
19 
20 using namespace std;
21 
28  regLicenseList(
29  boost::regex(SPDX_LICENSE_LIST, boost::regex_constants::icase)),
30  regLicenseName(
31  boost::regex(SPDX_LICENSE_NAMES, boost::regex_constants::icase)),
32  regDualLicense(
33  boost::regex(SPDX_DUAL_LICENSE, boost::regex_constants::icase))
34 {
35 }
36 
48 vector<ojomatch> OjoAgent::processFile(const string &filePath,
49  OjosDatabaseHandler &databaseHandler)
50 {
51  ifstream stream(filePath);
52  std::stringstream sstr;
53  sstr << stream.rdbuf();
54  if (stream.fail())
55  {
56  throw std::runtime_error(filePath);
57  }
58  stream.close();
59  const string fileContent = sstr.str();
60  vector<ojomatch> licenseList;
61  vector<ojomatch> licenseNames;
62 
63  scanString(fileContent, regLicenseList, licenseList, 0, false);
64  for (auto m : licenseList)
65  {
66  scanString(m.content, regLicenseName, licenseNames, m.start, false);
67  scanString(m.content, regDualLicense, licenseNames, m.start, true);
68  }
69 
70  findLicenseId(licenseNames, databaseHandler);
71  filterMatches(licenseNames);
72 
73  return licenseNames;
74 }
75 
83 vector<ojomatch> OjoAgent::processFile(const string &filePath)
84 {
85  ifstream stream(filePath);
86  std::stringstream sstr;
87  sstr << stream.rdbuf();
88  if (stream.fail())
89  {
90  throw std::runtime_error(filePath);
91  }
92  stream.close();
93  const string fileContent = sstr.str();
94  vector<ojomatch> licenseList;
95  vector<ojomatch> licenseNames;
96 
97  scanString(fileContent, regLicenseList, licenseList, 0, false);
98  for (auto m : licenseList)
99  {
100  scanString(m.content, regLicenseName, licenseNames, m.start, false);
101  scanString(m.content, regDualLicense, licenseNames, m.start, true);
102  }
103 
104  // Remove duplicate matches for CLI run
105  vector<ojomatch>::iterator uniqueListIt = std::unique(licenseNames.begin(),
106  licenseNames.end());
107  licenseNames.resize(std::distance(licenseNames.begin(), uniqueListIt));
108 
109  return licenseNames;
110 }
111 
120 void OjoAgent::scanString(const string &text, boost::regex reg,
121  vector<ojomatch> &result, unsigned int offset, bool isDualTest)
122 {
123  string::const_iterator end = text.end();
124  string::const_iterator pos = text.begin();
125 
126  while (pos != end)
127  {
128  // Find next match
129  boost::smatch res;
130  if (boost::regex_search(pos, end, res, reg))
131  {
132  string content = "Dual-license";
133  if (! isDualTest)
134  {
135  content = res[1].str();
136  }
137  // Found match
138  result.push_back(
139  ojomatch(offset + res.position(1),
140  offset + res.position(1) + res.length(1),
141  res.length(1),
142  content));
143  pos = res[0].second;
144  offset += res.position() + res.length();
145  }
146  else
147  {
148  // No match found
149  break;
150  }
151  }
152 }
153 
158 void OjoAgent::filterMatches(vector<ojomatch> &matches)
159 {
160  // Remvoe entries with license_fk < 1
161  matches.erase(
162  std::remove_if(matches.begin(), matches.end(), [](ojomatch match)
163  { return match.license_fk <= 0;}), matches.end());
164 }
165 
171 void OjoAgent::findLicenseId(vector<ojomatch> &matches,
172  OjosDatabaseHandler &databaseHandler)
173 {
174  // Update license_fk
175  for (size_t i = 0; i < matches.size(); ++i)
176  {
177  matches[i].license_fk = databaseHandler.getLicenseIdForName(
178  matches[i].content);
179  }
180 }
unsigned long getLicenseIdForName(std::string const &rfShortName)
Get the license id for a given short name.
const boost::regex regDualLicense
Definition: OjoAgent.hpp:51
const boost::regex regLicenseName
Definition: OjoAgent.hpp:51
Store the results of a regex match.
Definition: scanners.hpp:39
OjoAgent()
Definition: OjoAgent.cc:27
#define SPDX_LICENSE_NAMES
Regex to filter license names from list of license list.
Definition: ojoregex.hpp:44
#define SPDX_DUAL_LICENSE
Regex to check if Dual-license.
Definition: ojoregex.hpp:51
#define SPDX_LICENSE_LIST
Regex to filter the list of licenses.
Definition: ojoregex.hpp:35
void findLicenseId(std::vector< ojomatch > &matches, OjosDatabaseHandler &databaseHandler)
Definition: OjoAgent.cc:171
const boost::regex regLicenseList
Definition: OjoAgent.hpp:51
void scanString(const std::string &text, boost::regex reg, std::vector< ojomatch > &result, unsigned int offset, bool isDualTest)
Definition: OjoAgent.cc:120
void filterMatches(std::vector< ojomatch > &matches)
Definition: OjoAgent.cc:158
Store the results of a regex match.
Definition: ojomatch.hpp:27