#include <iostream> #include <string> #include <fstream> #include <sstream> #include <iterator> using namespace std; #include <boost/regex.hpp> const char * expressions = // comments "(//[^\\n]*|/\\*.*?\\*/)|" // precompilers "(^[[:blank:]]*#[^\\n]*)|" // string literals "(\"(?:[^\\\\\"]|\\\\.)*\"|'(?:[^\\\\']|\\\\.)*')|" // floating point literal "(\\<[[:digit:]]+\\.[[:digit:]]+)|" // integer literal "(\\<[[:digit:]]+\\>)|" // boolean literal "((?:\\<true\\>)|(?:\\<false\\>))|" // keywords "\\<(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import" "|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall" "|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool" "|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete" "|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto" "|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected" "|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast" "|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned" "|using|virtual|void|volatile|wchar_t|while)\\>"; const char * format = "(?1<font color = \"#999999\"><i>$&</i></font>)" "(?2<font color = \"#000099\">$&</font>)" "(?3<font color = \"#009900\">$&</font>)" "(?4<font color = \"#996600\">$&</font>)" "(?5<font color = \"#999900\">$&</font>)" "(?6<font color = \"#660000\"><b>$&</b></font>)" "(?7<font color = \"#336699\"><b>$&</b></font>)"; string help = "Usage:" "\n\nccode2html <input file> <output file>" "\n\nIf output filename is omitted its name it will be <input file>.html"; string code_tag_start = "<PRE>"; string code_tag_end = "</PRE>"; void ccode2html (string input, string output); string test(); extern const char* pre_expression = "(<)|(>)|(&)|\\r"; extern const char* pre_format = "(?1\\<)(?2\\>)(?3&)"; int main (int argc, char *argv[]) { string input_file; string output_file; if (argc > 3 || argc==1) { cout << help; exit(0); } else if (argc==2) { input_file=argv[1]; output_file=input_file+".html"; ccode2html (input_file, output_file); } else { input_file=argv[1]; output_file=argv[2]; ccode2html (input_file, output_file); } return 0; } // ccode2html method void ccode2html (string input, string output) { try { boost::regex e1, e2; e2.assign(pre_expression); e1.assign(expressions); fstream in (input.c_str(), fstream::in); fstream out (output.c_str(), fstream::out); out << "<pre>"; string ins; char c; while (in.get (c)) { ins.append (1, c); } ostringstream t(std::ios::out | std::ios::binary); ostream_iterator<char, char> io(t); boost::regex_replace(io, ins.begin(), ins.end(), e2, pre_format, boost::match_default | boost::format_all); string s(t.str()); std::ostream_iterator<char, char> o(out); boost::regex_replace(o, s.begin(), s.end(), e1, format, boost::match_default | boost::format_all); out << "</pre>"; cout << s; cout << input << endl; cout << output << endl; test(); } catch (char * exc) { cout << "errors:" << endl; cout << exc << endl; } }
You need to create an account or log in to post comments to this site.