regex_grep_example_2.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_2.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: regex_grep example 2: searches a cpp file for class definitions,
  16. * using a global callback function.
  17. */
  18. #include <boost/regex.hpp>
  19. #include <string>
  20. #include <map>
  21. #include <boost/regex.hpp>
  22. // purpose:
  23. // takes the contents of a file in the form of a string
  24. // and searches for all the C++ class definitions, storing
  25. // their locations in a map of strings/int's
  26. typedef std::map<std::string, std::string::difference_type, std::less<std::string> > map_type;
  27. const char* re =
  28. // possibly leading whitespace:
  29. "^[[:space:]]*"
  30. // possible template declaration:
  31. "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
  32. // class or struct:
  33. "(class|struct)[[:space:]]*"
  34. // leading declspec macros etc:
  35. "("
  36. "\\<\\w+\\>"
  37. "("
  38. "[[:blank:]]*\\([^)]*\\)"
  39. ")?"
  40. "[[:space:]]*"
  41. ")*"
  42. // the class name
  43. "(\\<\\w*\\>)[[:space:]]*"
  44. // template specialisation parameters
  45. "(<[^;:{]+>)?[[:space:]]*"
  46. // terminate in { or :
  47. "(\\{|:[^;\\{()]*\\{)";
  48. boost::regex expression(re);
  49. map_type class_index;
  50. std::string::const_iterator base;
  51. bool grep_callback(const boost::match_results<std::string::const_iterator>& what)
  52. {
  53. // what[0] contains the whole string
  54. // what[5] contains the class name.
  55. // what[6] contains the template specialisation if any.
  56. // add class name and position to map:
  57. class_index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
  58. what[5].first - base;
  59. return true;
  60. }
  61. void IndexClasses(const std::string& file)
  62. {
  63. std::string::const_iterator start, end;
  64. start = file.begin();
  65. end = file.end();
  66. base = start;
  67. boost::regex_grep(grep_callback, start, end, expression);
  68. }
  69. #include <fstream>
  70. #include <iostream>
  71. using namespace std;
  72. void load_file(std::string& s, std::istream& is)
  73. {
  74. s.erase();
  75. if(is.bad()) return;
  76. s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail()));
  77. char c;
  78. while(is.get(c))
  79. {
  80. if(s.capacity() == s.size())
  81. s.reserve(s.capacity() * 3);
  82. s.append(1, c);
  83. }
  84. }
  85. int main(int argc, const char** argv)
  86. {
  87. std::string text;
  88. for(int i = 1; i < argc; ++i)
  89. {
  90. cout << "Processing file " << argv[i] << endl;
  91. std::ifstream fs(argv[i]);
  92. load_file(text, fs);
  93. fs.close();
  94. IndexClasses(text);
  95. cout << class_index.size() << " matches found" << endl;
  96. map_type::iterator c, d;
  97. c = class_index.begin();
  98. d = class_index.end();
  99. while(c != d)
  100. {
  101. cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl;
  102. ++c;
  103. }
  104. class_index.erase(class_index.begin(), class_index.end());
  105. }
  106. return 0;
  107. }