PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/regex/example/snippets/regex_search_example.cpp

https://bitbucket.org/vaporoid/boost
C++ | 129 lines | 74 code | 22 blank | 33 comment | 5 complexity | 2e16f45442ad2a3f687cc8ec0f52dea2 MD5 | raw file
  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_search_example.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: regex_search example: searches a cpp file for class definitions.
  16. */
  17. #include <string>
  18. #include <map>
  19. #include <boost/regex.hpp>
  20. // purpose:
  21. // takes the contents of a file in the form of a string
  22. // and searches for all the C++ class definitions, storing
  23. // their locations in a map of strings/int's
  24. typedef std::map<std::string, std::string::difference_type, std::less<std::string> > map_type;
  25. const char* re =
  26. // possibly leading whitespace:
  27. "^[[:space:]]*"
  28. // possible template declaration:
  29. "(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
  30. // class or struct:
  31. "(class|struct)[[:space:]]*"
  32. // leading declspec macros etc:
  33. "("
  34. "\\<\\w+\\>"
  35. "("
  36. "[[:blank:]]*\\([^)]*\\)"
  37. ")?"
  38. "[[:space:]]*"
  39. ")*"
  40. // the class name
  41. "(\\<\\w*\\>)[[:space:]]*"
  42. // template specialisation parameters
  43. "(<[^;:{]+>)?[[:space:]]*"
  44. // terminate in { or :
  45. "(\\{|:[^;\\{()]*\\{)";
  46. boost::regex expression(re);
  47. void IndexClasses(map_type& m, const std::string& file)
  48. {
  49. std::string::const_iterator start, end;
  50. start = file.begin();
  51. end = file.end();
  52. boost::match_results<std::string::const_iterator> what;
  53. boost::match_flag_type flags = boost::match_default;
  54. while(boost::regex_search(start, end, what, expression, flags))
  55. {
  56. // what[0] contains the whole string
  57. // what[5] contains the class name.
  58. // what[6] contains the template specialisation if any.
  59. // add class name and position to map:
  60. m[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
  61. what[5].first - file.begin();
  62. // update search position:
  63. start = what[0].second;
  64. // update flags:
  65. flags |= boost::match_prev_avail;
  66. flags |= boost::match_not_bob;
  67. }
  68. }
  69. #include <iostream>
  70. #include <fstream>
  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. map_type m;
  92. std::ifstream fs(argv[i]);
  93. load_file(text, fs);
  94. fs.close();
  95. IndexClasses(m, text);
  96. cout << m.size() << " matches found" << endl;
  97. map_type::iterator c, d;
  98. c = m.begin();
  99. d = m.end();
  100. while(c != d)
  101. {
  102. cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl;
  103. ++c;
  104. }
  105. }
  106. return 0;
  107. }