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_3.cpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: regex_grep example 3: searches a cpp file for class definitions,
17   *                using a bound member function callback.
18   */
19 
20 #include <boost/regex.hpp>
21 #include <string>
22 #include <map>
23 #include <functional>
24 #include <boost/detail/workaround.hpp>
25 
26 // purpose:
27 // takes the contents of a file in the form of a string
28 // and searches for all the C++ class definitions, storing
29 // their locations in a map of strings/int's
30 
31 typedef std::map<std::string, std::string::difference_type, std::less<std::string> > map_type;
32 
33 const char* re =
34    // possibly leading whitespace:
35    "^[[:space:]]*"
36    // possible template declaration:
37    "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
38    // class or struct:
39    "(class|struct)[[:space:]]*"
40    // leading declspec macros etc:
41    "("
42       "\\<\\w+\\>"
43       "("
44          "[[:blank:]]*\\([^)]*\\)"
45       ")?"
46       "[[:space:]]*"
47    ")*"
48    // the class name
49    "(\\<\\w*\\>)[[:space:]]*"
50    // template specialisation parameters
51    "(<[^;:{]+>)?[[:space:]]*"
52    // terminate in { or :
53    "(\\{|:[^;\\{()]*\\{)";
54 
55 
56 class class_index
57 {
58    boost::regex expression;
59    map_type index;
60    std::string::const_iterator base;
61 
62    bool grep_callback(boost::match_results<std::string::const_iterator> what);
63 public:
get_map()64    map_type& get_map() { return index; }
65    void IndexClasses(const std::string& file);
class_index()66    class_index()
67       : expression(re) {}
68 };
69 
grep_callback(boost::match_results<std::string::const_iterator> what)70 bool class_index::grep_callback(boost::match_results<std::string::const_iterator> what)
71 {
72    // what[0] contains the whole string
73    // what[5] contains the class name.
74    // what[6] contains the template specialisation if any.
75    // add class name and position to map:
76    index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
77                what[5].first - base;
78    return true;
79 }
80 
IndexClasses(const std::string & file)81 void class_index::IndexClasses(const std::string& file)
82 {
83    std::string::const_iterator start, end;
84    start = file.begin();
85    end = file.end();
86    base = start;
87 #if BOOST_WORKAROUND(_MSC_VER, < 1300) && !defined(_STLP_VERSION)
88    boost::regex_grep(std::bind1st(std::mem_fun1(&class_index::grep_callback), this),
89             start,
90             end,
91             expression);
92 #elif defined(BOOST_NO_CXX98_BINDERS)
93    boost::regex_grep(std::bind(&class_index::grep_callback, this, std::placeholders::_1),
94             start,
95             end,
96             expression);
97 #else
98    boost::regex_grep(std::bind1st(std::mem_fun(&class_index::grep_callback), this),
99             start,
100             end,
101             expression);
102 #endif
103 }
104 
105 
106 #include <fstream>
107 #include <iostream>
108 
109 using namespace std;
110 
load_file(std::string & s,std::istream & is)111 void load_file(std::string& s, std::istream& is)
112 {
113    s.erase();
114    if(is.bad()) return;
115    s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail()));
116    char c;
117    while(is.get(c))
118    {
119       if(s.capacity() == s.size())
120          s.reserve(s.capacity() * 3);
121       s.append(1, c);
122    }
123 }
124 
main(int argc,const char ** argv)125 int main(int argc, const char** argv)
126 {
127    std::string text;
128    for(int i = 1; i < argc; ++i)
129    {
130       cout << "Processing file " << argv[i] << endl;
131       std::ifstream fs(argv[i]);
132       load_file(text, fs);
133       fs.close();
134       class_index idx;
135       idx.IndexClasses(text);
136       cout << idx.get_map().size() << " matches found" << endl;
137       map_type::iterator c, d;
138       c = idx.get_map().begin();
139       d = idx.get_map().end();
140       while(c != d)
141       {
142          cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl;
143          ++c;
144       }
145    }
146    return 0;
147 }
148 
149 
150 
151 
152 
153 
154