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