资源简介

采用C++编写将C语言文件代码高亮用html语言输出

资源截图

代码片段和文件信息

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

typedef map::iterator map_iter;
map g_mapHighlight;
map g_mapTokenRegex;
map g_mapTokenType;

//配置信息
const int MAX_CONFIG=200;
const char *g_szConfigFile=“codeHl.ini“;
char g_szLayout[MAX_CONFIG]=“background:#F3F3F3;padding:2px 10px;margin:10px;border:1px solid #CCC;font-size:14px“;
char g_szFont[MAX_CONFIG]=“宋体“;
char g_szKeyword[MAX_CONFIG]=“color:#0000FF;“;
char g_szComment[MAX_CONFIG]=“color:#008000;“;
char g_szPreprocessor[MAX_CONFIG]=“color:#0000FF;“;

long getFileLength(ifstream& in);
void init();
string& highlight(string& str);
string& htmlEncode(string &str);
void sourceToHTML(char *szsFilechar *szdFile);
void writeConfig();
void readConfig();

int main(int argc char *argv[]) {  
if (argc<2)
{
cout<<“Usage:codeHl.exe fileName\n“;
return 0;
}
sourceToHTML(argv[1]“a.html“);
return 0;
}

long getFileLength(ifstream& in)
{
long psOld=in.tellg();
in.seekg(0ios::end);
long ps=in.tellg();
in.seekg(psOld);
return ps;
}
void init()
{
TCHAR szPath[MAX_PATH]; 
GetWindowsDirectory(szPathMAX_PATH);
strcat_s(szPathMAX_PATH“\\codeHl.ini“);
//配置文件不存在
if ( _access(szPath0)==-1 )
writeConfig();
else
readConfig();

//语法高亮方案
g_mapHighlight.insert(make_pair( “Keyword“g_szKeyword ));
g_mapHighlight.insert(make_pair( “Comment“g_szComment ));
g_mapHighlight.insert(make_pair( “Preprocessor“g_szPreprocessor ));

//C++关键字
char *szKeywords[]={
“auto““bool““break““case““catch““char““class““const““continue“
“default““delete““do““double““else““enum““explicit““export““extern“
“false““float““for““friend““goto““if““inline““int““long““mutable“
“namespace““new““operator““private““protected““public““register““return“
“short““signed““sizeof““static““static_cast““struct““switch“
“template““this““throw““true““try““typedef““typename““union““unsigned““using“
“virtual““void““volatile““wchar_t““while“
};
//初始化标识符对应的类型及其正则查找语法
for(int i=0;i {
g_mapTokenRegex.insert(make_pair( szKeywords[i]string(“\\b(“)+szKeywords[i]+“)\\b“ ));
g_mapTokenType.insert(make_pair( szKeywords[i]“Keyword“ ));
}
//初始化注释类型和正则查找语法
g_mapTokenRegex.insert(make_pair( “/**/“ “(/\\*(.|\\s)*?\\*/)“ ));
g_mapTokenType.insert(make_pair( “/**/“ “Comment“ ));
g_mapTokenRegex.insert(make_pair( “//“ “(//.*?)\r“ ));
g_mapTokenType.insert(make_pair( “//“ “Comment“ ));
//预处理
g_mapTokenRegex.insert(make_pair( “#“ “(#.*?\\s)“ ));
g_mapTokenType.insert(make_pair( “#“ “Preprocessor“ ));
}

string& htmlEncode(string &str)
{
regex reg(“<“);
str=regex_replace(strreg“<“);
reg=“>“;
str=regex_replace(strreg“>

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        476  2009-06-09 11:18  readme.txt

     文件     236544  2009-06-09 11:03  codeHl.exe

     文件       5091  2009-06-09 11:02  codeHl.cpp

----------- ---------  ---------- -----  ----

               242111                    3


评论

共有 条评论