]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/example/snippets/regex_grep_example_4.cpp
3 * Copyright (c) 1998-2002
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)
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE regex_grep_example_4.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: regex_grep example 4: searches a cpp file for class definitions,
17 * using a C++ Builder closure as a callback.
24 #include <boost/regex.hpp>
28 // takes the contents of a file in the form of a string
29 // and searches for all the C++ class definitions, storing
30 // their locations in a map of strings/int's
32 typedef std::map
<std::string
, int, std::less
<std::string
> > map_type
;
35 // possibly leading whitespace:
37 // possible template declaration:
38 "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
40 "(class|struct)[[:space:]]*"
41 // leading declspec macros etc:
45 "[[:blank:]]*\\([^)]*\\)"
50 "(\\<\\w*\\>)[[:space:]]*"
51 // template specialisation parameters
52 "(<[^;:{]+>)?[[:space:]]*"
53 // terminate in { or :
54 "(\\{|:[^;\\{()]*\\{)";
59 boost::regex expression
;
61 std::string::const_iterator base
;
62 typedef boost::match_results
<std::string::const_iterator
> arg_type
;
64 bool grep_callback(const boost::match_results
<std::string::const_iterator
>& what
);
66 map_type
& get_map() { return index
; }
67 typedef bool (__closure
* grep_callback_type
)(const arg_type
&);
68 void IndexClasses(const std::string
& file
);
75 bool class_index::grep_callback(const boost::match_results
<std::string::const_iterator
>& what
)
77 // what[0] contains the whole string
78 // what[5] contains the class name.
79 // what[6] contains the template specialisation if any.
80 // add class name and position to map:
81 index
[std::string(what
[5].first
, what
[5].second
) + std::string(what
[6].first
, what
[6].second
)] =
86 void class_index::IndexClasses(const std::string
& file
)
88 std::string::const_iterator start
, end
;
92 class_index::grep_callback_type cl
= &(this->grep_callback
);
105 void load_file(std::string
& s
, std::istream
& is
)
109 s
.reserve(is
.rdbuf()->in_avail());
113 if(s
.capacity() == s
.size())
114 s
.reserve(s
.capacity() * 3);
119 int main(int argc
, const char** argv
)
122 for(int i
= 1; i
< argc
; ++i
)
124 cout
<< "Processing file " << argv
[i
] << endl
;
125 std::ifstream
fs(argv
[i
]);
129 i
.IndexClasses(text
);
130 cout
<< i
.get_map().size() << " matches found" << endl
;
131 map_type::iterator c
, d
;
132 c
= i
.get_map().begin();
133 d
= i
.get_map().end();
136 cout
<< "class \"" << (*c
).first
<< "\" found at index: " << (*c
).second
<< endl
;
143 #else // __BORLANDC__