FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
regscan.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 "regscan.hpp"
20 
25 regexScanner::regexScanner(const string& type,
26  const string& identity,
27  int index)
28  : _type(type),
29  _identity(identity),
30  _index(index)
31 {
33  rcp.maybeLoad(_identity);
34  _reg = rx::regex(rcp.getRegexValue(_identity, _type),
35  rx::regex_constants::icase);
36 }
37 
44 regexScanner::regexScanner(const string& type,
45  std::istringstream& stream,
46  int index)
47  : _type(type),
48  _identity(type),
49  _index(index)
50 {
52  rcp.maybeLoad(_identity,stream);
53  _reg = rx::regex(rcp.getRegexValue(_identity, _type),
54  rx::regex_constants::icase);
55 }
56 
62 void regexScanner::ScanString(const string& s, list<match>& results) const
63 {
64  // Read file into one string
65  string::const_iterator end = s.end();
66  string::const_iterator pos = s.begin();
67  unsigned int intPos = 0;
68 
69  while (pos != end)
70  {
71  // Find next match
72  rx::smatch res;
73  if (rx::regex_search(pos, end, res, _reg))
74  {
75  // Found match
76  results.push_back(match(intPos + res.position(_index),
77  intPos + res.position(_index) + res.length(_index),
78  _type));
79  pos = res[0].second;
80  intPos += res.position() + res.length();
81  }
82  else
83  // No match found
84  break;
85  }
86 }
87 
Provide regex using conf file.
rx::regex _reg
Definition: regscan.hpp:37
Store the results of a regex match.
Definition: scanners.hpp:39
const string _identity
Definition: regscan.hpp:44
const string _type
Definition: regscan.hpp:44
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 &str, list< match > &results) const
Scan a string using regex defined during initialization.
Definition: regscan.cc:62
regexScanner(const string &type, const string &identity, int index=0)
Initialize RegexConfProvider and regex based on type and identity.
Definition: regscan.cc:25
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.