]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/example/snippets/regex_merge_example.cpp
3 * Copyright (c) 1998-2002
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)
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE regex_merge_example.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: regex_merge example:
17 * converts a C++ file to syntax highlighted HTML.
20 #include <boost/regex.hpp>
30 // takes the contents of a file and transform to
31 // syntax highlighted code in html format
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
;
41 void load_file(std::string
& s
, std::istream
& is
)
45 s
.reserve(static_cast<std::string::size_type
>(is
.rdbuf()->in_avail()));
49 if(s
.capacity() == s
.size())
50 s
.reserve(s
.capacity() * 3);
55 int main(int argc
, const char** argv
)
58 e1
.assign(expression_text
);
59 e2
.assign(pre_expression
);
60 for(int i
= 1; i
< argc
; ++i
)
62 std::cout
<< "Processing file " << argv
[i
] << std::endl
;
63 std::ifstream
fs(argv
[i
]);
67 std::string out_name
= std::string(argv
[i
]) + std::string(".htm");
68 std::ofstream
os(out_name
.c_str());
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_merge(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_merge(out
, s
.begin(), s
.end(), e1
, format_string
, boost::match_default
| boost::format_all
);
89 const char* pre_expression
= "(<)|(>)|\\r";
90 const char* pre_format
= "(?1<)(?2>)";
93 const char* expression_text
= // preprocessor directives: index 1
94 "(^[[:blank:]]*#(?:[^\\\\\\n]|\\\\[^\\n[:punct:][:word:]]*[\\n[:punct:][:word:]])*)|"
96 "(//[^\\n]*|/\\*.*?\\*/)|"
98 "\\<([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\\.)?[[:digit:]]+(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\\>|"
99 // string literals: index 4
100 "('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|"
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)\\>"
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>)"
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"
123 "<BODY LINK=\"#0000ff\" VLINK=\"#800080\" BGCOLOR=\"#ffffff\">\n"
126 const char* footer_text
= "</PRE>\n</BODY>\n\n";