• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright (c) 1998-2002
4  * John Maddock
5  *
6  * Use, modification and distribution are subject to the
7  * Boost Software License, Version 1.0. (See accompanying file
8  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9  *
10  */
11 
12  /*
13   *   LOCATION:    see http://www.boost.org for most recent version.
14   *   FILE         regex_grep_example_1.cpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: regex_grep example 1: searches a cpp file for class definitions.
17   */
18 
19 #include <boost/regex.hpp>
20 #include <string>
21 #include <map>
22 
23 // purpose:
24 // takes the contents of a file in the form of a string
25 // and searches for all the C++ class definitions, storing
26 // their locations in a map of strings/int's
27 
28 typedef std::map<std::string, std::string::difference_type, std::less<std::string> > map_type;
29 
30 const char* re =
31    // possibly leading whitespace:
32    "^[[:space:]]*"
33    // possible template declaration:
34    "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
35    // class or struct:
36    "(class|struct)[[:space:]]*"
37    // leading declspec macros etc:
38    "("
39       "\\<\\w+\\>"
40       "("
41          "[[:blank:]]*\\([^)]*\\)"
42       ")?"
43       "[[:space:]]*"
44    ")*"
45    // the class name
46    "(\\<\\w*\\>)[[:space:]]*"
47    // template specialisation parameters
48    "(<[^;:{]+>)?[[:space:]]*"
49    // terminate in { or :
50    "(\\{|:[^;\\{()]*\\{)";
51 
52 boost::regex expression(re);
53 
54 class IndexClassesPred
55 {
56    map_type& m;
57    std::string::const_iterator base;
58 public:
IndexClassesPred(map_type & a,std::string::const_iterator b)59    IndexClassesPred(map_type& a, std::string::const_iterator b) : m(a), base(b) {}
operator ()(const boost::match_results<std::string::const_iterator> & what)60    bool operator()(const boost::match_results<std::string::const_iterator>& what)
61    {
62       // what[0] contains the whole string
63       // what[5] contains the class name.
64       // what[6] contains the template specialisation if any.
65       // add class name and position to map:
66       m[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
67                what[5].first - base;
68       return true;
69    }
70 private:
71    IndexClassesPred& operator=(const IndexClassesPred&);
72 };
73 
IndexClasses(map_type & m,const std::string & file)74 void IndexClasses(map_type& m, const std::string& file)
75 {
76    std::string::const_iterator start, end;
77    start = file.begin();
78    end = file.end();
79    boost::regex_grep(IndexClassesPred(m, start), start, end, expression);
80 }
81 
82 
83 #include <fstream>
84 #include <iostream>
85 
86 using namespace std;
87 
load_file(std::string & s,std::istream & is)88 void load_file(std::string& s, std::istream& is)
89 {
90    s.erase();
91    if(is.bad()) return;
92    s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail()));
93    char c;
94    while(is.get(c))
95    {
96       if(s.capacity() == s.size())
97          s.reserve(s.capacity() * 3);
98       s.append(1, c);
99    }
100 }
101 
main(int argc,const char ** argv)102 int main(int argc, const char** argv)
103 {
104    std::string text;
105    for(int i = 1; i < argc; ++i)
106    {
107       cout << "Processing file " << argv[i] << endl;
108       map_type m;
109       std::ifstream fs(argv[i]);
110       load_file(text, fs);
111       fs.close();
112       IndexClasses(m, text);
113       cout << m.size() << " matches found" << endl;
114       map_type::iterator c, d;
115       c = m.begin();
116       d = m.end();
117       while(c != d)
118       {
119          cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl;
120          ++c;
121       }
122    }
123    return 0;
124 }
125 
126 
127 
128 
129 
130 
131 
132