]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/example/snippets/regex_grep_example_3.cpp
02aaa72402a7a3bfe9d873b809643b8d2a5895e2
[ceph.git] / ceph / src / boost / libs / regex / example / snippets / regex_grep_example_3.cpp
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 <string>
21 #include <map>
22 #include <boost/regex.hpp>
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:
64 map_type& get_map() { return index; }
65 void IndexClasses(const std::string& file);
66 class_index()
67 : expression(re) {}
68 };
69
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
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 #else
93 boost::regex_grep(std::bind1st(std::mem_fun(&class_index::grep_callback), this),
94 start,
95 end,
96 expression);
97 #endif
98 }
99
100
101 #include <fstream>
102 #include <iostream>
103
104 using namespace std;
105
106 void load_file(std::string& s, std::istream& is)
107 {
108 s.erase();
109 if(is.bad()) return;
110 s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail()));
111 char c;
112 while(is.get(c))
113 {
114 if(s.capacity() == s.size())
115 s.reserve(s.capacity() * 3);
116 s.append(1, c);
117 }
118 }
119
120 int main(int argc, const char** argv)
121 {
122 std::string text;
123 for(int i = 1; i < argc; ++i)
124 {
125 cout << "Processing file " << argv[i] << endl;
126 std::ifstream fs(argv[i]);
127 load_file(text, fs);
128 fs.close();
129 class_index idx;
130 idx.IndexClasses(text);
131 cout << idx.get_map().size() << " matches found" << endl;
132 map_type::iterator c, d;
133 c = idx.get_map().begin();
134 d = idx.get_map().end();
135 while(c != d)
136 {
137 cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl;
138 ++c;
139 }
140 }
141 return 0;
142 }
143
144
145
146
147
148