regex_replace_example.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_replace_example.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: regex_replace example:
  16. * converts a C++ file to syntax highlighted HTML.
  17. */
  18. #include <boost/regex.hpp>
  19. #include <iostream>
  20. #include <fstream>
  21. #include <sstream>
  22. #include <string>
  23. #include <iterator>
  24. #include <fstream>
  25. #include <iostream>
  26. // purpose:
  27. // takes the contents of a file and transform to
  28. // syntax highlighted code in html format
  29. boost::regex e1, e2;
  30. extern const char* expression_text;
  31. extern const char* format_string;
  32. extern const char* pre_expression;
  33. extern const char* pre_format;
  34. extern const char* header_text;
  35. extern const char* footer_text;
  36. void load_file(std::string& s, std::istream& is)
  37. {
  38. s.erase();
  39. if(is.bad()) return;
  40. s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail()));
  41. char c;
  42. while(is.get(c))
  43. {
  44. if(s.capacity() == s.size())
  45. s.reserve(s.capacity() * 3);
  46. s.append(1, c);
  47. }
  48. }
  49. int main(int argc, const char** argv)
  50. {
  51. try{
  52. e1.assign(expression_text);
  53. e2.assign(pre_expression);
  54. for(int i = 1; i < argc; ++i)
  55. {
  56. std::cout << "Processing file " << argv[i] << std::endl;
  57. std::ifstream fs(argv[i]);
  58. std::string in;
  59. load_file(in, fs);
  60. fs.close();
  61. std::string out_name = std::string(argv[i]) + std::string(".htm");
  62. std::ofstream os(out_name.c_str());
  63. os << header_text;
  64. // strip '<' and '>' first by outputting to a
  65. // temporary string stream
  66. std::ostringstream t(std::ios::out | std::ios::binary);
  67. std::ostream_iterator<char> oi(t);
  68. boost::regex_replace(oi, in.begin(), in.end(), e2, pre_format, boost::match_default | boost::format_all);
  69. // then output to final output stream
  70. // adding syntax highlighting:
  71. std::string s(t.str());
  72. std::ostream_iterator<char> out(os);
  73. boost::regex_replace(out, s.begin(), s.end(), e1, format_string, boost::match_default | boost::format_all);
  74. os << footer_text;
  75. os.close();
  76. }
  77. }
  78. catch(...)
  79. { return -1; }
  80. return 0;
  81. }
  82. const char* pre_expression = "(<)|(>)|(&)|\\r";
  83. const char* pre_format = "(?1&lt;)(?2&gt;)(?3&amp;)";
  84. const char* expression_text = // preprocessor directives: index 1
  85. "(^[[:blank:]]*#(?:[^\\\\\\n]|\\\\[^\\n[:punct:][:word:]]*[\\n[:punct:][:word:]])*)|"
  86. // comment: index 2
  87. "(//[^\\n]*|/\\*.*?\\*/)|"
  88. // literals: index 3
  89. "\\<([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\\.)?[[:digit:]]+(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\\>|"
  90. // string literals: index 4
  91. "('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|"
  92. // keywords: index 5
  93. "\\<(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import"
  94. "|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall"
  95. "|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool"
  96. "|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete"
  97. "|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto"
  98. "|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected"
  99. "|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast"
  100. "|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned"
  101. "|using|virtual|void|volatile|wchar_t|while)\\>"
  102. ;
  103. const char* format_string = "(?1<font color=\"#008040\">$&</font>)"
  104. "(?2<I><font color=\"#000080\">$&</font></I>)"
  105. "(?3<font color=\"#0000A0\">$&</font>)"
  106. "(?4<font color=\"#0000FF\">$&</font>)"
  107. "(?5<B>$&</B>)";
  108. const char* header_text = "<HTML>\n<HEAD>\n"
  109. "<TITLE>Auto-generated html formated source</TITLE>\n"
  110. "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1252\">\n"
  111. "</HEAD>\n"
  112. "<BODY LINK=\"#0000ff\" VLINK=\"#800080\" BGCOLOR=\"#ffffff\">\n"
  113. "<P> </P>\n<PRE>";
  114. const char* footer_text = "</PRE>\n</BODY>\n\n";