Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

C++ Code To HTML

DZone's Guide to

C++ Code To HTML

·
Free Resource
generate html colored file from a source file


#include 
  
   
#include 
   
    
#include 
    
     
#include 
     
      
#include 
      
       
using namespace std;

#include 
       
        

const char * expressions =
	// comments
	"(//[^\\n]*|/\\*.*?\\*/)|"
	// precompilers
	"(^[[:blank:]]*#[^\\n]*)|"
	// string literals
	"(\"(?:[^\\\\\"]|\\\\.)*\"|'(?:[^\\\\']|\\\\.)*')|"
	// floating point literal
	"(\\<[[:digit:]]+\\.[[:digit:]]+)|"
	//  integer literal
	"(\\<[[:digit:]]+\\>)|"
	// boolean literal
	"((?:\\
        
         )|(?:\\
         
          ))|"
	// 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
          $&)"
	"(?2
          $&)"
	"(?3
          $&)"
	"(?4
          $&)"
	"(?5
          $&)"
	"(?6
          $&)"
	"(?7
          $&)";

string help = 
	"Usage:"
	"\n\nccode2html 
           
          "
	"\n\nIf output filename is omitted its name it will be .html";
string code_tag_start = "
";
string code_tag_end = "
"; 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 << "
";
		string ins;
		char c;
		while (in.get (c)) {
			ins.append (1, c);
		}
		ostringstream t(std::ios::out | std::ios::binary);
		ostream_iterator
            
              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
             
               o(out);
		boost::regex_replace(o, s.begin(), s.end(), e1, format,
				boost::match_default | boost::format_all);
		out << "
             
            
"; cout << s; cout << input << endl; cout << output << endl; test(); } catch (char * exc) { cout << "errors:" << endl; cout << exc << endl; } }
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}