]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/example/snippets/regex_grep_example_3.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_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.
20 #include <boost/regex.hpp>
24 #include <boost/detail/workaround.hpp>
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
31 typedef std::map
<std::string
, std::string::difference_type
, std::less
<std::string
> > map_type
;
34 // possibly leading whitespace:
36 // possible template declaration:
37 "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
39 "(class|struct)[[:space:]]*"
40 // leading declspec macros etc:
44 "[[:blank:]]*\\([^)]*\\)"
49 "(\\<\\w*\\>)[[:space:]]*"
50 // template specialisation parameters
51 "(<[^;:{]+>)?[[:space:]]*"
52 // terminate in { or :
53 "(\\{|:[^;\\{()]*\\{)";
58 boost::regex expression
;
60 std::string::const_iterator base
;
62 bool grep_callback(boost::match_results
<std::string::const_iterator
> what
);
64 map_type
& get_map() { return index
; }
65 void IndexClasses(const std::string
& file
);
70 bool class_index::grep_callback(boost::match_results
<std::string::const_iterator
> what
)
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
)] =
81 void class_index::IndexClasses(const std::string
& file
)
83 std::string::const_iterator start
, end
;
87 #if BOOST_WORKAROUND(_MSC_VER, < 1300) && !defined(_STLP_VERSION)
88 boost::regex_grep(std::bind1st(std::mem_fun1(&class_index::grep_callback
), this),
92 #elif defined(BOOST_NO_CXX98_BINDERS)
93 boost::regex_grep(std::bind(&class_index::grep_callback
, this, std::placeholders::_1
),
98 boost::regex_grep(std::bind1st(std::mem_fun(&class_index::grep_callback
), this),
111 void load_file(std::string
& s
, std::istream
& is
)
115 s
.reserve(static_cast<std::string::size_type
>(is
.rdbuf()->in_avail()));
119 if(s
.capacity() == s
.size())
120 s
.reserve(s
.capacity() * 3);
125 int main(int argc
, const char** argv
)
128 for(int i
= 1; i
< argc
; ++i
)
130 cout
<< "Processing file " << argv
[i
] << endl
;
131 std::ifstream
fs(argv
[i
]);
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();
142 cout
<< "class \"" << (*c
).first
<< "\" found at index: " << (*c
).second
<< endl
;