]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/example/snippets/regex_iterator_example.cpp
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_iterator_example_2.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: regex_iterator example 2: searches a cpp file for class definitions,
20 #include <boost/regex.hpp>
29 // takes the contents of a file in the form of a string
30 // and searches for all the C++ class definitions, storing
31 // their locations in a map of strings/int's
33 typedef std::map
<std::string
, std::string::difference_type
, std::less
<std::string
> > map_type
;
36 // possibly leading whitespace:
38 // possible template declaration:
39 "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
41 "(class|struct)[[:space:]]*"
42 // leading declspec macros etc:
46 "[[:blank:]]*\\([^)]*\\)"
51 "(\\<\\w*\\>)[[:space:]]*"
52 // template specialisation parameters
53 "(<[^;:{]+>)?[[:space:]]*"
54 // terminate in { or :
55 "(\\{|:[^;\\{()]*\\{)";
58 boost::regex
expression(re
);
61 bool regex_callback(const boost::match_results
<std::string::const_iterator
>& what
)
63 // what[0] contains the whole string
64 // what[5] contains the class name.
65 // what[6] contains the template specialisation if any.
66 // add class name and position to map:
67 class_index
[what
[5].str() + what
[6].str()] = what
.position(5);
71 void load_file(std::string
& s
, std::istream
& is
)
75 s
.reserve(static_cast<std::string::size_type
>(is
.rdbuf()->in_avail()));
79 if(s
.capacity() == s
.size())
80 s
.reserve(s
.capacity() * 3);
85 int main(int argc
, const char** argv
)
88 for(int i
= 1; i
< argc
; ++i
)
90 cout
<< "Processing file " << argv
[i
] << endl
;
91 std::ifstream
fs(argv
[i
]);
94 // construct our iterators:
95 boost::sregex_iterator
m1(text
.begin(), text
.end(), expression
);
96 boost::sregex_iterator m2
;
97 std::for_each(m1
, m2
, ®ex_callback
);
99 cout
<< class_index
.size() << " matches found" << endl
;
100 map_type::iterator c
, d
;
101 c
= class_index
.begin();
102 d
= class_index
.end();
105 cout
<< "class \"" << (*c
).first
<< "\" found at index: " << (*c
).second
<< endl
;
108 class_index
.erase(class_index
.begin(), class_index
.end());