regex_grep_example_3.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE regex_grep_example_3.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: regex_grep example 3: searches a cpp file for class definitions,
  16. * using a bound member function callback.
  17. */
  18. #include <boost/regex.hpp>
  19. #include <string>
  20. #include <map>
  21. #include <functional>
  22. #include <boost/detail/workaround.hpp>
  23. // purpose:
  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
  27. typedef std::map<std::string, std::string::difference_type, std::less<std::string> > map_type;
  28. const char* re =
  29. // possibly leading whitespace:
  30. "^[[:space:]]*"
  31. // possible template declaration:
  32. "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
  33. // class or struct:
  34. "(class|struct)[[:space:]]*"
  35. // leading declspec macros etc:
  36. "("
  37. "\\<\\w+\\>"
  38. "("
  39. "[[:blank:]]*\\([^)]*\\)"
  40. ")?"
  41. "[[:space:]]*"
  42. ")*"
  43. // the class name
  44. "(\\<\\w*\\>)[[:space:]]*"
  45. // template specialisation parameters
  46. "(<[^;:{]+>)?[[:space:]]*"
  47. // terminate in { or :
  48. "(\\{|:[^;\\{()]*\\{)";
  49. class class_index
  50. {
  51. boost::regex expression;
  52. map_type index;
  53. std::string::const_iterator base;
  54. bool grep_callback(boost::match_results<std::string::const_iterator> what);
  55. public:
  56. map_type& get_map() { return index; }
  57. void IndexClasses(const std::string& file);
  58. class_index()
  59. : expression(re) {}
  60. };
  61. bool class_index::grep_callback(boost::match_results<std::string::const_iterator> what)
  62. {
  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. index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
  68. what[5].first - base;
  69. return true;
  70. }
  71. void class_index::IndexClasses(const std::string& file)
  72. {
  73. std::string::const_iterator start, end;
  74. start = file.begin();
  75. end = file.end();
  76. base = start;
  77. #if BOOST_WORKAROUND(_MSC_VER, < 1300) && !defined(_STLP_VERSION)
  78. boost::regex_grep(std::bind1st(std::mem_fun1(&class_index::grep_callback), this),
  79. start,
  80. end,
  81. expression);
  82. #elif defined(BOOST_NO_CXX98_BINDERS)
  83. boost::regex_grep(std::bind(&class_index::grep_callback, this, std::placeholders::_1),
  84. start,
  85. end,
  86. expression);
  87. #else
  88. boost::regex_grep(std::bind1st(std::mem_fun(&class_index::grep_callback), this),
  89. start,
  90. end,
  91. expression);
  92. #endif
  93. }
  94. #include <fstream>
  95. #include <iostream>
  96. using namespace std;
  97. void load_file(std::string& s, std::istream& is)
  98. {
  99. s.erase();
  100. if(is.bad()) return;
  101. s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail()));
  102. char c;
  103. while(is.get(c))
  104. {
  105. if(s.capacity() == s.size())
  106. s.reserve(s.capacity() * 3);
  107. s.append(1, c);
  108. }
  109. }
  110. int main(int argc, const char** argv)
  111. {
  112. std::string text;
  113. for(int i = 1; i < argc; ++i)
  114. {
  115. cout << "Processing file " << argv[i] << endl;
  116. std::ifstream fs(argv[i]);
  117. load_file(text, fs);
  118. fs.close();
  119. class_index idx;
  120. idx.IndexClasses(text);
  121. cout << idx.get_map().size() << " matches found" << endl;
  122. map_type::iterator c, d;
  123. c = idx.get_map().begin();
  124. d = idx.get_map().end();
  125. while(c != d)
  126. {
  127. cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl;
  128. ++c;
  129. }
  130. }
  131. return 0;
  132. }