]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/example/snippets/regex_search_example.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_search_example.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: regex_search example: searches a cpp file for class definitions.
21 #include <boost/regex.hpp>
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
28 typedef std::map
<std::string
, std::string::difference_type
, std::less
<std::string
> > map_type
;
31 // possibly leading whitespace:
33 // possible template declaration:
34 "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
36 "(class|struct)[[:space:]]*"
37 // leading declspec macros etc:
41 "[[:blank:]]*\\([^)]*\\)"
46 "(\\<\\w*\\>)[[:space:]]*"
47 // template specialisation parameters
48 "(<[^;:{]+>)?[[:space:]]*"
49 // terminate in { or :
50 "(\\{|:[^;\\{()]*\\{)";
53 boost::regex
expression(re
);
55 void IndexClasses(map_type
& m
, const std::string
& file
)
57 std::string::const_iterator start
, end
;
60 boost::match_results
<std::string::const_iterator
> what
;
61 boost::match_flag_type flags
= boost::match_default
;
62 while(boost::regex_search(start
, end
, what
, expression
, flags
))
64 // what[0] contains the whole string
65 // what[5] contains the class name.
66 // what[6] contains the template specialisation if any.
67 // add class name and position to map:
68 m
[std::string(what
[5].first
, what
[5].second
) + std::string(what
[6].first
, what
[6].second
)] =
69 what
[5].first
- file
.begin();
70 // update search position:
71 start
= what
[0].second
;
73 flags
|= boost::match_prev_avail
;
74 flags
|= boost::match_not_bob
;
84 void load_file(std::string
& s
, std::istream
& is
)
88 s
.reserve(static_cast<std::string::size_type
>(is
.rdbuf()->in_avail()));
92 if(s
.capacity() == s
.size())
93 s
.reserve(s
.capacity() * 3);
98 int main(int argc
, const char** argv
)
101 for(int i
= 1; i
< argc
; ++i
)
103 cout
<< "Processing file " << argv
[i
] << endl
;
105 std::ifstream
fs(argv
[i
]);
108 IndexClasses(m
, text
);
109 cout
<< m
.size() << " matches found" << endl
;
110 map_type::iterator c
, d
;
115 cout
<< "class \"" << (*c
).first
<< "\" found at index: " << (*c
).second
<< endl
;