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  * \file scanners.hpp
20  * \brief Utilities to help scanners
21  */
22 #ifndef SCANNERS_HPP_
23 #define SCANNERS_HPP_
24 
25 #include <fstream>
26 using std::ifstream;
27 using std::istream;
28 #include <string>
29 using std::string;
30 #include <list>
31 using std::list;
32 
33 bool ReadFileToString(const string& fileName, string& out);
34 
35 /**
36  * \struct match
37  * \brief Store the results of a regex match
38  */
39 struct match {
40   /**
41    * \var int start
42    * Start position of match
43    * \var int end
44    * End position of match
45    */
46   const int start, end;
47   /**
48    * \var
49    * Type of the match
50    */
51   const string& type;
matchmatch52   match(const int s, const int e, const string& t) : start(s), end(e), type(t) { }
53 } ;
54 
55 bool operator==(const match& m1, const match& m2);
56 bool operator!=(const match& m1, const match& m2);
57 
58 /**
59  * \class scanner
60  * \brief Abstract class to provide interface to scanners
61  */
62 class scanner
63 {
64 public:
~scanner()65   virtual ~scanner() {};
66 
67   /**
68    * \brief Scan the given string and add matches to results
69    * \param[in]  s       String to scan
70    * \param[out] results Copyright matches are appended to this list
71    */
72   virtual void ScanString(const string& s, list<match>& results) const = 0;
73 
74   /**
75    * \brief Helper function to scan file
76    *
77    * Reads file contents to string and pass to ScanString()
78    * \param[in]  fileName File name to scan
79    * \param[out] results  Copyright matches are appended to this list
80    */
ScanFile(const string & fileName,list<match> & results) const81   virtual void ScanFile(const string& fileName, list<match>& results) const
82   {
83     string s;
84     ReadFileToString(fileName, s);
85     ScanString(s, results);
86   }
87 } ;
88 
89 #endif
90 
91