lxyppc 发表于 2009-9-18 01:33:37

[原创] 实用工具 在BBS中高亮显示C/C++中的关键字

[原创] 实用工具 在BBS中高亮显示C/C++中的关键字

CodePaint 1.0 源代码
ourdev_482069.zip(文件大小:116K) (原文件名:CodePaint.zip)
CodePaint 可执行程序
点击此处下载 ourdev_482132.zip(文件大小:171K) (原文件名:UIParser.zip)


由VS2003编译
词法分析部分是由lex编写
文件包中包含lex编译程序"flex.exe"

编译过程
1. 用CFlex.bat调用flex.exe,把Parser.l编译成Parser.cpp
2. 用VS编译工程

功能:
将C/C++中的关键字、字符串、数字以及注释用高亮的方式在论坛中显示出来
可以在代码前面加上行号,方便阅读

使用方法:
将源代码粘贴至上面的文本框中,点击Convert按钮
在下面的文本框中会生成BBS中的代码,同时内容被复制到剪切板中,在论坛中粘贴即可

http://cache.amobbs.com/bbs_upload782111/files_18/ourdev_482149.jpg
(原文件名:Pre-Cov.jpg)

http://cache.amobbs.com/bbs_upload782111/files_18/ourdev_482150.jpg
(原文件名:Post-Cov.jpg)


效果如下:
static int srcRemain = 0;   //< remain characters for the source text
static int srcLength = 0;   //< length of the source text
static char* lpSrc = NULL;//< source buffer pointer
/* lex parser function */
int   yylex(void);
void    ParseStart(void);
CComboBox* pLog = NULL;   //< combo box pointer used for log data
stringresult;             //< result generate by the parser
BOOL    bNeedLineNumber;    //< Flag used for output line number
char*   lnFormatComment;    //< line number format for block comment
char*   lnFormatNormal;   //< line number format for normal code
char*   currentFormat;      //< current format style, e.g. "ouravr"
int   curPos;             //< current source character position

加上行号后的效果
/*0001*/void CUIParserDlg::OnBnClickedConvert()</font>
/*0002*/{
<font color=#646400>/*0003*/    // TODO: Add your control notification handler code here
/*0004*/
/*0005*/    /** convert the Unicode string to mulit-byte for parser */
/*0006*/    CString str;
/*0007*/    m_sourceView.GetWindowText(str);
/*0008*/    if(m_textSrc)delete [] m_textSrc;
/*0009*/    m_textSrcLen = ::WideCharToMultiByte(CP_ACP,0,str,-1,NULL,0,NULL,NULL);
/*0010*/    m_textSrc = new char;
/*0011*/    if(!m_textSrc){
/*0012*/      AfxMessageBox(_T("Fail to allocate buffer for the text!"));
/*0013*/      return;
/*0014*/    }
/*0015*/    ::WideCharToMultiByte(CP_ACP,0,str,-1,m_textSrc,(int)m_textSrcLen,NULL,NULL);
</font>

watercat 发表于 2009-9-18 02:39:22

……真够累的,这点功能也需要个ActiveX控件啊?

lxyppc 发表于 2009-9-18 10:49:56

ActiveX控件?不解?这是一个独立的可执行程序

前面看到有人把代码改了颜色后发出来,于是就看看能不能做成语法高亮的形式
这个代码稍加修改也可以用在其它的论坛上

最好当然是论坛直接支持语法高亮,也不用打tag这么麻烦

yplin27 发表于 2009-9-18 11:25:50

感觉不出这工具的实用性,基本上把代码粘到随便一个编辑器上都可以有这效果(M$的就算了)。

如果是要在网页中显示高亮,也有很多js脚本可用,根本不需要这复制粘贴的步骤。

lxyppc 发表于 2009-9-18 11:54:01

楼上说的是自己能够控制网站代码的情况下,那可以用很多方法来着色

但是如果你访问的地方不支持语法高亮,只支持颜色标签,可以用这个东西

实用的确称不上
代码文档化方面doxygen做得很不错了
但是你得有自己能管理的服务器把它做的Html文档上传

mfkqqw 发表于 2009-9-18 11:58:58

有意思!

idodi 发表于 2009-9-18 13:27:53

真的不错啊,顶了

oldtom 发表于 2009-9-18 13:28:44

不错,看来楼主对yacc 和 lex比较熟悉吧?好东西。

lxyppc 发表于 2009-9-18 16:11:23

【7楼】 oldtom
不错,看来楼主对yacc 和 lex比较熟悉吧?好东西。
-----------------------------
以前做过一个源代码分析的工具,代码现在自己看都有点吃力了
现在做这个小玩意就当是复习一下吧,不过这个里面没有用到yacc的语法分析

bjj9217 发表于 2009-9-18 16:33:28

不错!

albert_w 发表于 2009-9-18 16:37:27

挺好的工具,以后求助的时候铁代码至少有人看了

ShangGuan 发表于 2009-9-18 18:15:29

不错。收下。

先试试。

/*0001*/
/*0002*/#include "config.h"
/*0003*/
/*0004*/TTimer   Timer1;
/*0005*/
/*0006*/int main()
/*0007*/{
/*0008*/    DDRB|=   _BV(0)|_BV(1)|_BV(2)|_BV(3);
/*0009*/    PORTB &= ~(_BV(0)|_BV(1)|_BV(2)|_BV(3));
/*0010*/    TMR_TicksInit();
/*0011*/
/*0012*/    sei();
/*0013*/
/*0014*/    while (1)
/*0015*/    {
/*0016*/      
/*0017*/      TMR_Delay(&Timer1,VE_TICKS_PER_SEC);
/*0018*/      PORTB^=_BV(0);
/*0019*/    }
/*0020*/
/*0021*/}
/*0022*/
/*0023*/SIGNAL(TIMER1_COMPA_vect)
/*0024*/{
/*0025*/    TMR_TicksHandler();            //系统节拍
/*0026*/}

Stitch 发表于 2009-9-18 18:58:55

Mark

qzf368 发表于 2009-9-22 22:50:47

Mark

myhonour 发表于 2009-9-23 21:03:01

good mark

kebaojun305 发表于 2009-9-24 22:28:57

mark

ShawnLinson 发表于 2009-9-25 10:45:37

mark。。。。

xcodes 发表于 2009-9-25 11:07:58

不错 好东西

hugeice 发表于 2009-9-25 11:29:39

不错,好多论坛一贴代码缩进都没有了,代码完全没法看,有这个就不怕了 :)

zcllom 发表于 2009-9-25 11:32:55

很好,很酷。
所以嵌入到19楼。

winter 发表于 2009-9-25 11:40:38

爽啊,感谢楼主,试试看

/*0001*/
/*0002*/byte KeyLED_SCAN(void)
/*0003*/{
/*0004*/    static byte keycon;
/*0005*/
/*0006*/    SETROW1();
/*0007*/    SETROW2();
/*0008*/    SETROW3();
/*0009*/    SETROW4();
/*0010*/    CLRLCOL1();
/*0011*/    CLRLCOL2();
/*0012*/    CLRLCOL3();
/*0013*/    CLRLCOL4();
/*0014*/    CLRLCOL5();
/*0015*/
/*0016*/    if (++KeyRowIndex > 3)        KeyRowIndex = 0;
/*0017*/
/*0018*/    switch (KeyRowIndex)
/*0019*/    {
/*0020*/    case 0:
/*0021*/      if (powerupFlag)
/*0022*/      {
/*0023*/            SETLCOL5();         //L1
/*0024*/      }
/*0025*/
/*0026*/      if (xxFlag)
/*0027*/      {
/*0028*/            SETLCOL3();                //L3
/*0029*/      }
/*0030*/      if (xxFlag)
/*0031*/      {
/*0032*/            SETLCOL4();                //L5
/*0033*/      }
/*0034*/
/*0035*/      if (xxFlag)                //L10
/*0036*/      {
/*0037*/            SETLCOL1();
/*0038*/      }
/*0039*/      CLRROW1();
/*0040*/
/*0041*/      keyflag0 = 0;
/*0042*/      if (!RDCOL1() )        return 1;
/*0043*/      if (!RDCOL2() )        return 2;
/*0044*/      if (!RDCOL3() )        return 7;
/*0045*/      keyflag0 = 1;
/*0046*/      break;
/*0047*/    case 1:
/*0048*/      if (xxFlag)                        SETLCOL5();         //L2
/*0049*/      if (xxFlag)                        SETLCOL1();                //L12
/*0050*/      if (xxFlag)                                        //L4
/*0051*/      {
/*0052*/                  SETLCOL3();
/*0053*/
/*0054*/      }
/*0055*/
/*0056*/      if (xxFlag)                                        //L6
/*0057*/      {
/*0058*/
/*0059*/                  SETLCOL2();
/*0060*/      }
/*0061*/
/*0062*/      if (xxFlag)
/*0063*/      {
/*0064*/                           //L15   
/*0065*/                SETLCOL4();
/*0066*/      }
/*0067*/      CLRROW2();
/*0068*/
/*0069*/      keyflag1 = 0;
/*0070*/      if (!RDCOL1() )        return 3;
/*0071*/      if (!RDCOL2() )        return 4;
/*0072*/      if (!RDCOL3() )        return 8;
/*0073*/      keyflag1 = 1;
/*0074*/      break;
/*0075*/    case 2:
/*0076*/
/*0077*/      if (xxFlag)
/*0078*/      {
/*0079*/            SETLCOL1();                                                //L11   
/*0080*/      }
/*0081*/
/*0082*/      if (xxFlag)                                                //L16       
/*0083*/      {
/*0084*/
/*0085*/                SETLCOL3();
/*0086*/      }
/*0087*/
/*0088*/      if (xxFlag)
/*0089*/      {
/*0090*/                                 //L13   
/*0091*/                SETLCOL2();
/*0092*/      }
/*0093*/
/*0094*/      if (xxFlag)
/*0095*/      {
/*0096*/                        //L8       
/*0097*/                SETLCOL5();
/*0098*/      }
/*0099*/      CLRROW3();
/*0100*/
/*0101*/      keyflag2 = 0;
/*0102*/      if (!RDCOL1() )        return 5;
/*0103*/      if (!RDCOL2() )        return 6;
/*0104*/      if (!RDCOL3() )        return 11;
/*0105*/      keyflag2 = 1;
/*0106*/
/*0107*/      break;
/*0108*/    case 3:
/*0109*/
/*0110*/      if (xxFlag)
/*0111*/      {
/*0112*/            if (VibLevel == 1)                                //L17       
/*0113*/                SETLCOL2();
/*0114*/            if (VibLevel == 2)                                //L18       
/*0115*/                SETLCOL4();
/*0116*/      }
/*0117*/      if (xxFlag)
/*0118*/      {
/*0119*/                                //L14   
/*0120*/                SETLCOL1();
/*0121*/      }
/*0122*/
/*0123*/      if (xxFlag)
/*0124*/      {
/*0125*/            if (xxFlag)                                //L9       
/*0126*/                SETLCOL5();
/*0127*/
/*0128*/      }
/*0129*/      CLRROW4();
/*0130*/
/*0131*/      keyflag3 = 0;
/*0132*/      if (!RDCOL1() )        return 9;
/*0133*/      if (!RDCOL2() )        return 10;
/*0134*/      if (!RDCOL3() )        return 12;
/*0135*/      keyflag3 = 1;
/*0136*/
/*0137*/      break;
/*0138*/    default:
/*0139*/      break;
/*0140*/    }
/*0141*/
/*0142*/    if (!keybit&&keyflag0&&keyflag1&&keyflag2&&keyflag3)
/*0143*/    {
/*0144*/      if (++keycon > 7)
/*0145*/      {
/*0146*/            keybit = 1;
/*0147*/            keycon = 0;
/*0148*/      }
/*0149*/
/*0150*/    }
/*0151*/    return 0;
/*0152*/}

hugeice 发表于 2009-9-25 14:45:43

试试,原是代码:

// get time
UINT32 GetMillisecond()
{
        UINT32 ret;
        LARGE_INTEGER liFrequency;
        if ( QueryPerformanceFrequency(&liFrequency) )
        {
                LARGE_INTEGER liCounter;
                QueryPerformanceCounter(&liCounter);
                ret = (UINT32)((1000 * liCounter.QuadPart) / liFrequency.QuadPart);
        }
        else
        {
                ret = GetTickCount();
        }
        return ret;
}

hugeice 发表于 2009-9-25 14:46:10

转换后的代码:

// get time
UINT32 GetMillisecond()
{
        UINT32 ret;
        LARGE_INTEGER liFrequency;
        if ( QueryPerformanceFrequency(&liFrequency) )
        {
                LARGE_INTEGER liCounter;
                QueryPerformanceCounter(&liCounter);
                ret = (UINT32)((1000 * liCounter.QuadPart) / liFrequency.QuadPart);
        }
        else
        {
                ret = GetTickCount();
        }
        return ret;
}

hugeice 发表于 2009-9-25 14:46:47

转换,加行号的代码:

/*0001*/// get time
/*0002*/UINT32 GetMillisecond()
/*0003*/{
/*0004*/        UINT32 ret;
/*0005*/        LARGE_INTEGER liFrequency;
/*0006*/        if ( QueryPerformanceFrequency(&liFrequency) )
/*0007*/        {
/*0008*/                LARGE_INTEGER liCounter;
/*0009*/                QueryPerformanceCounter(&liCounter);
/*0010*/                ret = (UINT32)((1000 * liCounter.QuadPart) / liFrequency.QuadPart);
/*0011*/        }
/*0012*/        else
/*0013*/        {
/*0014*/                ret = GetTickCount();
/*0015*/        }
/*0016*/        return ret;
/*0017*/}

hugeice 发表于 2009-9-25 14:47:36

楼主,我用tab键缩进显示不正确!

lxyppc 发表于 2009-9-25 18:58:30

【24楼】 hugeice 戬峰
积分:11
派别:
等级:------
来自:麻城
楼主,我用tab键缩进显示不正确!
-----------------------------
嗯,很多地方Tab表示的位置都不一样,有的地方根本就不支持Tab
而基本上写程序的人都会将Tab缩进改成空格缩进,一些编辑器也支持将Tab转换成空格
所以在这个小程序中没有实现对Tab的支持

谢谢大家的支持

lxyppc 发表于 2009-9-26 00:27:56

CodePaint 1.1
新增功能
1. 支持论坛风格选择,可在Ini文件中增加风格
2. 可以选择是否自动复制到剪切板
3. 可以选择是否将Tab换成空格 (网友“hugeice 戬峰”提出的需求)

CodePaint 1.1 源代码
点击此处下载 ourdev_485539.zip(文件大小:120K) (原文件名:UIParser.zip)
CodePaint 可执行程序
点击此处下载 ourdev_485540.zip(文件大小:147K) (原文件名:CodePaint.zip)

由VS2003编译
词法分析部分是由lex编写
文件包中包含lex编译程序"flex.exe"

编译过程
1. 用CFlex.bat调用flex.exe,把Parser.l编译成Parser.cpp
2. 用VS编译工程

功能:
将C/C++中的关键字、字符串、数字以及注释用高亮的方式在论坛中显示出来
可以在代码前面加上行号,方便阅读


http://cache.amobbs.com/bbs_upload782111/files_19/ourdev_485541.jpg
(原文件名:UIParse.jpg)

zcllom 发表于 2009-9-26 08:17:11

好球

banyan_city 发表于 2009-9-26 15:23:05

不错

lbn2006 发表于 2009-9-26 20:29:08

不错,MARK

usingavr 发表于 2009-10-10 07:59:34

用VIM打开源码,然后用TOhtml命令就可以输出HTML格式的源码,可以通过改变VIM的颜色方案来改变输出的颜色方案

几乎可以支持所有的语言!!!

zlutian 发表于 2009-10-10 08:02:48

好东西, mark

powerg7 发表于 2009-10-10 08:35:22

喜欢,本人也爱做小工具

qlb1234 发表于 2009-10-11 12:34:00

好东西,顶起!

hephi 发表于 2009-10-24 07:43:45

MARK

dukedz 发表于 2009-11-9 17:29:02

呃,感覺我這個好用些,網頁版的,支持html和Discuz!代码,顏色、關鍵字可以自己修改!
http://dukedz.blog.163.com/blog/static/3911808820091094551595/edit/

lofeng 发表于 2010-1-5 12:11:15

mark

lofeng 发表于 2010-1-7 18:11:55

很好用,但是注释中有中文会乱码
/*0001*/int main(void)
/*0002*/{
/*0003*/    u16 i;
/*0004*/
/*0005*/    recv_ptr = 0;
/*0006*/   
/*0007*/    RCC_Configuration();
/*0008*/    GPIO_Configuration();
/*0009*/    NVIC_Configuration();
/*0010*/    DMA_Configuration();
/*0011*/    USART1_Configuration();
/*0012*/   
/*0013*/    printf("\r\nSystem Start...\r\n");
/*0014*/    printf("Initialling SendBuff... \r\n");
/*0015*/    for(i=0;i<SENDBUFF_SIZE;i++)
/*0016*/    {
/*0017*/      SendBuff = i&0xff;
/*0018*/    }
/*0019*/    printf("Initial success!\r\nWaiting for transmission...\r\n");
/*0020*/    //¡¤¡é?¨ª¨¨£¤¨ºy?Y¨°??-¡Á?¡À?o?¡ê?¡ã¡ä??¡ã¡ä?¨¹?¡ä?a¨º?¡ä?¨º?
/*0021*/    while(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3));//KEY2
/*0022*/   
/*0023*/    printf("Start DMA transmission!\r\n");
/*0024*/   
/*0025*/    //?a¨¤?¨º??a¨º?DMA¡ä?¨º??¡ã¦Ì?¨°?D?¡Á?¡À?1¡è¡Á¡Â¡ê???USART1?¡ê?¨¦¨¦¨¨??3¨¦DMA¡¤?¨º?1¡è¡Á¡Â
/*0026*/    USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
/*0027*/    //?a¨º?¨°?¡ä?DMA¡ä?¨º?¡ê?
/*0028*/    DMA_Cmd(DMA1_Channel4, ENABLE);
/*0029*/   
/*0030*/    //¦Ì¨¨¡äyDMA¡ä?¨º?¨ª¨º3¨¦¡ê?¡ä?¨º¡À?¨°??¨¤¡ä¡Á?¨¢¨ª¨ªa¨°?D?¨º?¡ê?¦Ì?¦Ì?
/*0031*/    //¨º¦Ì?¨º¨®|¨®??D¡ê?¡ä?¨º?¨ºy?Y?¨²??¡ê??¨¦¨°??¡äDD¨¢¨ª¨ªa¦Ì?¨¨???
/*0032*/    #if 0
/*0033*/while(DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET)
/*0034*/    {
/*0035*/      GPIOC->ODR ^= (1<<7);      //LED1¡¤-¡Áa
/*0036*/      Delay();      //¨¤?¡¤?¨º¡À??
/*0037*/    }
/*0038*/#else
/*0039*/while(DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET)
/*0040*/{
/*0041*/    DMA_ClearITPendingBit(DMA1_IT_TC4);
/*0042*/    dma_count++;
/*0043*/}
/*0044*/    #endif
/*0045*/   
/*0046*/    //DMA¡ä?¨º??¨¢¨º?o¨®¡ê?¡Á??¡¥1?¡À?¨¢?DMA¨ª¡§¦Ì¨¤¡ê????TD¨¨¨º??¡¥1?¡À?
/*0047*/    //????¦Ì?¨®???¡À?¡Á¡é¨º¨ª
/*0048*/    //DMA_Cmd(DMA1_Channel4, DISABLE);
/*0049*/   
/*0050*/    printf("\r\nDMA transmission successful!\r\n");
/*0051*/    printf("\r\ndma_count=%d!\r\n",dma_count);
/*0052*/printf("\r\nusart_count=%d!\r\n",usart_count);
/*0053*/
/*0054*/   
/*0055*/    /* Infinite loop */
/*0056*/    while (1)
/*0057*/    {
/*0058*/    }
/*0059*/}
/*0060*/

xu.sunrise 发表于 2010-1-31 13:34:53

我也试一下
/*0001*/void    cpSetCodeColor(const string& formatName, const string& colorName, BOOL bNeedTrack)
/*0002*/{
/*0003*/    cpColorFormat_t::iterator it = cpColorFormat.find(formatName);
/*0004*/    if(it == cpColorFormat.end()){
/*0005*/      return;
/*0006*/    }
/*0007*/    cpColorMap_t::iterator it1 = cpColorMap.find(colorName);
/*0008*/    if(it1 == cpColorMap.end()){
/*0009*/      return;
/*0010*/    }
/*0011*/    COLORREF ref = RGB(
/*0012*/      GetBValue(it1->second),
/*0013*/      GetGValue(it1->second),
/*0014*/      GetRValue(it1->second)
/*0015*/      );
/*0016*/    char buf[1024] = "";
/*0017*/    sprintf(buf,it->second.set.c_str(),ref);
/*0018*/    OutputString(buf,0);
/*0019*/    if(bNeedTrack){
/*0020*/      cpCodeColor cpColor;
/*0021*/      cpColor.color = it1->second;
/*0022*/      cpColor.start = curPos;
/*0023*/      cpPosStack.push_back(cpColor);
/*0024*/    }
/*0025*/}
/*0026*/
/*0027*/
/*0028*/void StringLog(const char* str);
/*0029*//**
<font color=#646400>|*0030*|reset code color
|*0031*| */</font>
/*0032*/void    cpResetCodeColor(const string& formatName, BOOL bNeedTrack)
/*0033*/{
/*0034*/    cpColorFormat_t::iterator it = cpColorFormat.find(formatName);
/*0035*/    if(it == cpColorFormat.end()){
/*0036*/      return;
/*0037*/    }
/*0038*/    char buf[1024] = "";
/*0039*/    sprintf(buf,it->second.reset.c_str());
/*0040*/    OutputString(buf,0);
/*0041*/    if(bNeedTrack && cpPosStack.size()){
/*0042*/      vector<cpCodeColor>::iterator it1 = cpPosStack.begin();
/*0043*/      it1 += (cpPosStack.size() - 1);
/*0044*/      if(it1 != cpPosStack.end()){
/*0045*/            it1->end = curPos;
/*0046*/            if(0){
/*0047*/                char buf[1024] = "";
/*0048*/                sprintf(buf,"Color: 0x%06X, Start: %d, End: %d",
/*0049*/                  it1->color,it1->start, it1->end);
/*0050*/                StringLog(buf);
/*0051*/            }
/*0052*/            cpColorStack.push_back(*it1);
/*0053*/            cpPosStack.erase(it1);
/*0054*/      }
/*0055*/    }
/*0056*/}

eduhf_123 发表于 2010-2-27 00:24:54

MARK 代码工具

h2feo4 发表于 2010-2-27 00:33:10

不错

goink 发表于 2010-6-11 14:03:23

测试一下
    void putch(u8 c)
    {
      if(c=='\n')
      {
            USART_SendData(USART1, 0x0D);
            while(!USART_GetITStatus(USART1, USART_IT_TXE)); //将要改这里
            USART_SendData(USART1, 0x0A);
            while(!USART_GetITStatus(USART1, USART_IT_TXE));
      }
      else
      {
            USART_SendData(USART1, (unsigned char)c);
            while(!USART_GetITStatus(USART1, USART_IT_TXE));
      }
    }//end of void putch(u8 c)

<font size=5><font color="blue">测试字色和字体 </font>

fshunj 发表于 2010-6-11 21:01:30

static int srcRemain = 0;   //< remain characters for the source text
static int srcLength = 0;   //< length of the source text
static char* lpSrc = NULL;//< source buffer pointer
/* lex parser function */
int   yylex(void);
void    ParseStart(void);
CComboBox* pLog = NULL;   //< combo box pointer used for log data
stringresult;             //< result generate by the parser
BOOL    bNeedLineNumber;    //< Flag used for output line number
char*   lnFormatComment;    //< line number format for block comment
char*   lnFormatNormal;   //< line number format for normal code
char*   currentFormat;      //< current format style, e.g. "ouravr"
int   curPos;             <font color=#008000>//< current source character position

binaimei2007 发表于 2010-11-1 22:40:39

好东西啊,谢谢楼主

cctv02 发表于 2011-10-12 14:23:08

好工具 谢谢了
页: [1]
查看完整版本: [原创] 实用工具 在BBS中高亮显示C/C++中的关键字