regex_grep_example_4.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_4.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: regex_grep example 4: searches a cpp file for class definitions,
  16. * using a C++ Builder closure as a callback.
  17. */
  18. #ifdef __BORLANDC__
  19. #include <boost/regex.hpp>
  20. #include <string>
  21. #include <map>
  22. #include <functional>
  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, int, 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. typedef boost::match_results<std::string::const_iterator> arg_type;
  55. bool grep_callback(const boost::match_results<std::string::const_iterator>& what);
  56. public:
  57. map_type& get_map() { return index; }
  58. typedef bool (__closure* grep_callback_type)(const arg_type&);
  59. void IndexClasses(const std::string& file);
  60. class_index()
  61. : index(),
  62. expression(re)
  63. {}
  64. };
  65. bool class_index::grep_callback(const boost::match_results<std::string::const_iterator>& what)
  66. {
  67. // what[0] contains the whole string
  68. // what[5] contains the class name.
  69. // what[6] contains the template specialisation if any.
  70. // add class name and position to map:
  71. index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
  72. what[5].first - base;
  73. return true;
  74. }
  75. void class_index::IndexClasses(const std::string& file)
  76. {
  77. std::string::const_iterator start, end;
  78. start = file.begin();
  79. end = file.end();
  80. base = start;
  81. class_index::grep_callback_type cl = &(this->grep_callback);
  82. boost::regex_grep(cl,
  83. start,
  84. end,
  85. expression);
  86. }
  87. #include <fstream>
  88. #include <iostream>
  89. using namespace std;
  90. void load_file(std::string& s, std::istream& is)
  91. {
  92. s.erase();
  93. if(is.bad()) return;
  94. s.reserve(is.rdbuf()->in_avail());
  95. char c;
  96. while(is.get(c))
  97. {
  98. if(s.capacity() == s.size())
  99. s.reserve(s.capacity() * 3);
  100. s.append(1, c);
  101. }
  102. }
  103. int main(int argc, const char** argv)
  104. {
  105. std::string text;
  106. for(int i = 1; i < argc; ++i)
  107. {
  108. cout << "Processing file " << argv[i] << endl;
  109. std::ifstream fs(argv[i]);
  110. load_file(text, fs);
  111. fs.close();
  112. class_index i;
  113. i.IndexClasses(text);
  114. cout << i.get_map().size() << " matches found" << endl;
  115. map_type::iterator c, d;
  116. c = i.get_map().begin();
  117. d = i.get_map().end();
  118. while(c != d)
  119. {
  120. cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl;
  121. ++c;
  122. }
  123. }
  124. return 0;
  125. }
  126. #else // __BORLANDC__
  127. int main()
  128. {
  129. return 0;
  130. }
  131. #endif