posts - 32, comments - 59, trackbacks - 0, articles - 2
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
         很多时候都要把C/C++代码发到网页中来,但是直接拷贝、粘贴后的效果并不好,许多符号因为是html的特殊字符而造成显示的混乱(最常见的就是'<'和'>'符号),所以就想把C/C++文件简单处理成html代码,当然早就有人做过了,不过觉得自己想想如何实现还是挺不错的!
        要实现的要求是:
        1、高亮显示C/C++保留字,比如int、char、sizeof等,(可用蓝色显示)
         2、对于#include开始的行用绿色显示
         3、数字用红色显示
         4、自动处理缩进,控制换行。也就是符合C/C++书写的常用规范,比如如下格式
        
int x1, x2 = 3;
for(; x2 <= 10; x2++)
{
    cout
<< x2<< endl;
}

          5、要把'<'、'>'转换成&lt;和&gt;
   

          其实实现起来也不难,大体思路如下:
          一行一行的扫描,如果是字母就组合成词,然后检验是不是保留字,是的话蓝色显示,html代码就是加上<font color=#0000FF>保留字</font>即可,不是保留字就输出。如果是数字,就拼数,然后红色显示。
           如果是#就整行绿色输出(要注意转换<、>)。
         如何控制缩进是个问题,可以设置int型全局变量tables,表示缩进的程度,遇到{时tables值增加,}时减少。
        处理';'时要注意的是,如果不是在括号里面那么就要换行。


想想其实就像一个小的编译器了,呵呵,以后有时间再继续吧。

今天简单做了一下,代码如下,但是功能尚不完善:

  /*----------------------------
     c/c++2html.c
     author:JackRain
     date:08-13-2005
     test in c-free 3.5
----------------------------*/
#include 
<stdio.h>
#include 
<ctype.h>
char szbaoliu[][10= {"bool","char","class","do","double","else","float","goto","if","int","long",
"short","struct","sizeof","uion","unsigned"}
;
int tables = 0;
int kuohaos = 0;
bool isps = false;
bool isyinhao = false;
bool isover = false;
bool isfenhao = false;
bool testarray(char array[])
{
    
for(int i = 0;i<16;i++)
    
{
        
if(strcmp(array,szbaoliu[i])==0)
        
return true;
    }

    
return false;
}

int main()
{
    FILE 
*c_file,*html_file;
    
int i=0;
    
char rbuffer[1024],wbuffer[1024],*cpoint, vararray[15],numarray[15];
    c_file 
= fopen("readfile.cpp","rb");
    html_file 
= fopen("readfile.html","a+");
    fputs(
"<html><head><title>C/C++2Html</title>\n</head>\n<body>\n",html_file);
    fputs(
"<table align=center border=0 bgcolor=#ffffff width=660><tr><td><pre>\n",html_file);
    
while(fgets(rbuffer, 1024, c_file)!=NULL)
    
{
        cpoint 
= rbuffer;
        i 
= 0;
        
/*while(*cpoint!='\0')
        {
            if(*cpoint!=' ')   //虑掉空格
            wbuffer[i++] = *(cpoint++);
            else
            cpoint++;
        }
        wbuffer[i] = '\0';
        cpoint = wbuffer;  
*/

        
if(tables&&*cpoint!='}')
             fputs(
"   ",html_file);
        
while(*cpoint != '\0')
        
{
            
if(isalpha(*cpoint))
            
{
                isover 
= true;
                i 
= 0;
                
while((isalnum(*cpoint)||*cpoint=='_')&&*cpoint != '\0')
                
{
                    vararray[i
++= *(cpoint++);
                }

                vararray[i] 
= '\0';
                
if(!isyinhao&&testarray(vararray))
                
{
                    fprintf(html_file, 
"<font color=#0000FF>%s</font>",vararray);
                }

                
else
                    fputs(vararray, html_file);
                    
if(*cpoint!=';'&&!isyinhao)
                    fputc(
' ',html_file);
                
if(*cpoint == '\0')
                    
continue;
            }

            
else if(isdigit(*cpoint))
            
{
                isover 
=true;
                i 
= 0;
                
while(isalnum(*cpoint)&&*cpoint != '\0')
                
{
                    numarray[i
++= *(cpoint++);
                }

                numarray[i] 
= '\0';
                
if(!isyinhao)
                fprintf(html_file, 
"<font color=#ff0000>%s</font>", numarray);
                
else
                fputs(numarray,html_file);
                
if(*cpoint == '\0')
                    
continue;
            }


            
else if(*cpoint == '#')
            
{
                
if(isover)
                
{
                    fputc(
'#',html_file);
                    cpoint
++;continue;
                }

                fputs(
"<font color=green>", html_file);
                
while(*cpoint!='\0')
                
{
                    
if(*cpoint == '<')
                    
{
                        fputs(
"&lt;",html_file);
                    }

                    
else if(*cpoint == '>')
                        fputs(
"&gt;",html_file);
                    
else
                        fputc(
*cpoint, html_file);
                    cpoint
++;
                }

                fputs(
"</font>",html_file);
                
for(i = 0;i < tables; i++)
                    fputs(
"   ",html_file);
                
break;
            }

            
else  switch(*cpoint)
            
{case ';':
                fputc(
*cpoint, html_file);
                
if(!kuohaos&&*(cpoint+1)!='\r'&&*(cpoint+1)!='\n')
                
{
                    fputc(
'\n',html_file);
                
for(i = 0; i<tables-1;i++)
                    fputs(
"   ",html_file);}

                    cpoint
++;
            
break;
            
case '}':
                tables
--;
                fputc(
*cpoint,html_file);
                
if(*(cpoint+1)!='\n'&&!isalpha(*(cpoint+1)))
                
{
                    
for(i = 0; i< tables-1;i++)
                        fputs(
"   ",html_file);
                }

                cpoint
++;
            
break;
            
case  '/':
            
if(*(cpoint+1)=='/')
                
{
                    fputs(
"<font color=#dedfdf>",html_file);
                    
while(*cpoint!='\0')
                    
{
                        
if(*cpoint =='>')
                            fputs(
"&gt;",html_file);
                        
else if(*cpoint == '<')
                            fputs(
"&lt;",html_file);
                        
else
                            fputc(
*cpoint,html_file);
                        cpoint
++;
                    }

                    fputs(
"</font>",html_file);
                    
for(i=0;i<tables-1;i++)
                        fputs(
"   ",html_file);
                        
break;
                }

                
else if(*(cpoint+1)=='*')
                
{
                     fputs(
"<font color=#dedfdf>/",html_file);
                    
// isps = true;
                }

                
else if(*(cpoint-1)=='*')
                
{
                    fputs(
"/</font>",html_file);
                }

                cpoint
++;
            
break;
            
case '(':
                 kuohaos
++;
                 fputc(
'(',html_file);
                 cpoint
++;
            
break;
            
case ')':
                 kuohaos
--;
                 fputc(
')',html_file);
                 cpoint
++;
            
break;
            
case '<':
                 fputs(
"&lt;",html_file);
                 cpoint
++;
            
break;
            
case '>':
                 fputs(
"&gt;",html_file);
                 cpoint
++;
            
break;
            
case ',':
                 fputs(
" ,",html_file);
                 cpoint
++;
            
break;
            
case '\n':
                
for(i = 0; i< tables -1;i++)
                fputs(
"   ",html_file);
                cpoint 
++;
            
break;
            
case '\r':fputc('\n',html_file);cpoint++;break;
            
case '\'':
                if(*(cpoint-1)!='\\')
                
{
                    
if(isyinhao)
                    
{
                        isyinhao 
= false;
                        fputs(
"\'</font>",html_file);
                    }

                    
else
                    
{
                        isyinhao 
= true;
                        fputs(
"<font color=#dedfdf>\'",html_file);
                    }

                    
                }

                
else
                
{
                    
if(isfenhao)
                    fputs(
"\'</font>",html_file);
                    
else
                    fputc(
'\'',html_file);
                    if(isyinhao)
                    isyinhao 
=false;
                    
else
                    isyinhao 
= true;
                }

                cpoint
++;
                
break;
            
case '\"':
                
if(*(cpoint-1)!='\\')
                
{
                    
if(isyinhao)
                    
{
                        isyinhao 
= false;
                        fputs(
"\'</font>",html_file);
                    }

                    
else
                    
{
                        isyinhao 
= true;
                        fputs(
"<font color=#dedfdf>\"",html_file);
                    }


                }

                
else
                
{
                    
if(isfenhao)
                    
{
                        fputs(
"\"</font>",html_file);
                    }

                    
else
                    fputc(
'\"',html_file);
                }

                cpoint
++;
                
break;
                
case ' ':cpoint++;break;
                
case '{':tables++;fputc('{',html_file);cpoint++;break;
                
case '\\'if(*(cpoint+1)=='\\'{fputs("\\\\",html_file);cpoint+=2;isfenhao=true;break;}
                
else{fputc('\\',html_file);cpoint++;isfenhao=false;break;}
                
            
default:
            
if(*cpoint!='\0'){
                    fputc(
*cpoint,html_file);
                    cpoint
++;
                    
break;    }
 }

        }

    }

    fputs(
"\n</pre></td></tr></table>\n</body></html>",html_file);
    fclose(c_file);
    fclose(html_file);
    
return 0;
}

在遇到引号和\\时会有问题,当然如果出现中文也会有问题。这些问题等有时间在修改。另外代码也需要优化才行

Feedback

# re: 关于把C/C++代码转换成Html的思考  回复  更多评论   

2008-07-30 20:37 by 小龙
我来踩咯哦

# re: 关于把C/C++代码转换成Html的思考  回复  更多评论   

2008-10-08 11:57 by 。。
不错
只有注册用户登录后才能发表评论。