FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
copyscan.cc
1 /*
2  * Copyright (C) 2015, Siemens AG
3  * Author: Florian Krügel
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  */
18 
19 #include "copyscan.hpp"
20 #include <cctype>
21 #include <algorithm>
22 #include "regexConfProvider.hpp"
23 
24 const string copyrightType("statement");
32 {
34  rcp.maybeLoad("copyright");
35 
36  regCopyright = rx::regex(rcp.getRegexValue("copyright","REG_COPYRIGHT"),
37  rx::regex_constants::icase);
38 
39  regException = rx::regex(rcp.getRegexValue("copyright","REG_EXCEPTION"),
40  rx::regex_constants::icase);
41  regNonBlank = rx::regex(rcp.getRegexValue("copyright","REG_NON_BLANK"));
42 
43  regSimpleCopyright = rx::regex(rcp.getRegexValue("copyright","REG_SIMPLE_COPYRIGHT"),
44  rx::regex_constants::icase);
45 }
46 
55 void hCopyrightScanner::ScanString(const string& s, list<match>& out) const
56 {
57 
58  string::const_iterator begin = s.begin();
59  string::const_iterator pos = begin;
60  string::const_iterator end = s.end();
61  while (pos != end)
62  {
63  // Find potential copyright statement
64  rx::smatch results;
65  if (!rx::regex_search(pos, end, results, regCopyright))
66  // No further copyright statement found
67  break;
68  string::const_iterator foundPos = results[0].first;
69 
70  if (!rx::regex_match(foundPos, end, regException))
71  {
82  string::const_iterator j = find(foundPos, end, '\n');
83  while (j != end)
84  {
85  string::const_iterator beginOfLine = j;
86  ++beginOfLine;
87  string::const_iterator endOfLine = find(beginOfLine, end, '\n');
88  if (rx::regex_search(beginOfLine, endOfLine, regSimpleCopyright)
89  || !rx::regex_match(beginOfLine, endOfLine, regNonBlank))
90  {
91  // Found end
92  break;
93  }
94  j = endOfLine;
95  }
96  if (j - foundPos >= 999)
97  // Truncate
98  out.push_back(match(foundPos - begin, (foundPos - begin) + 998, copyrightType));
99  else
100  {
101  out.push_back(match(foundPos - begin, j - begin, copyrightType));
102  }
103  pos = j;
104  }
105  else
106  {
107  // An exception: this is not a copyright statement: continue at the end of this statement
108  pos = results[0].second;
109  }
110  }
111 }
112 
Provide regex using conf file.
rx::regex regNonBlank
Definition: copyscan.hpp:45
rx::regex regSimpleCopyright
Definition: copyscan.hpp:45
Store the results of a regex match.
Definition: scanners.hpp:39
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:48
const char * getRegexValue(const std::string &name, const std::string &key)
Get the regex as string from the RegexMap.
void ScanString(const string &s, list< match > &results) const
Scan a given string for copyright statements.
Definition: copyscan.cc:55
hCopyrightScanner()
Constructor for default hCopyrightScanner.
Definition: copyscan.cc:31
rx::regex regCopyright
Definition: copyscan.hpp:45
rx::regex regException
Definition: copyscan.hpp:45
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
void maybeLoad(const std::string &identity)
Check if identity already loaded in RegexMap, if not load them.