Chinaunix首页 | 论坛 | 博客
  • 博客访问: 465894
  • 博文数量: 107
  • 博客积分: 6073
  • 博客等级: 准将
  • 技术积分: 790
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-14 15:34
文章分类

全部博文(107)

文章存档

2010年(1)

2009年(106)

分类: C/C++

2009-10-06 23:37:43

作者:wangxinus,
来源:
http://wangxinus.cublog.cn
声明:原创文章欢迎转载,交流请Email给作者

以前的文章, 重新贴在这里。因为我要把以前的博客删掉了。 wangxinus 2009.10


/***************************************************************
* Name:      comdel.cpp
* Purpose:   delete the comments in C or C++ souce files
* Author:    wang (wangsin@qq.com)
* Created:   2009-07-06
* Modify:    2009-07-08, strings with chars <* />
* Copyright: wang (wangsin)
* License:   GPL
* Version:   0.2
**************************************************************/

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

typedef list StringList;

/* NO USED */
typedef enum LANG
{
    LANG_C = 0,
    LANG_CPP,
    LANG_END
}LANG;

int keep_line = 0; // "-kl option"

/***************************************************************
** process file , delete comment
** file: filename
***************************************************************/
int process(const string& file);

/***************************************************************
** main
***************************************************************/
int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        cout<<"-no input! you can type 'comdel -h' or 'comdel --help' for more information"<         return -1;
    }
    if(strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)
    {
        cout<<"Usage: comdel [options] [file [args]]"<         cout<<"Available options are:"<         cout<<" -h(--help): read help infomation."<         cout<<" -kl:        keep line as before."<         cout<<"Example: "<         cout<<" comdel -kl test0.h test1.c test2.cpp"<         return 0;
    }
   
    StringList options;
    for(int i=1; i     {
        string str = argv[i];
        if(str == "-kl")
        {
            keep_line = 1;
        }
        else
        {
            options.push_back(str);
        }
    }
    for_each(options.begin(), options.end(), process);
   
    return 0;
}

int process(const string& file)
{
    /* check input file */
    ifstream in(file.c_str());
    if(!in)
    {
        cout<<"-cannot open "<         return -1;
    }
    /* create output file */
    string file_out = file + "_";
    ofstream out(file_out.c_str());
    if(!out)
    {
        cout<<"-can't save file:"<         return -2;
    }
   
    /* read a line from in-file */
    char str[512];
    char text[512];
    string line;
    int status = 0; // 0:normal   1: //... 2: /*... 3: "..." 4: '...'(case '"':)
    while(!in.eof())
    {
        memset(text, 0, sizeof(text));
        int i_text = 0;   
        in.getline(str, sizeof(str));
        if(status == 1)
        {
            status = 0;
        }
        for(int i=0; i         {
            switch (str[i])
            {
            case '\"': // string like this : puts("it is not comment // here!");
            {
                if(status == 0)
                    status = 3;
                else if(status == 3)
                    status = 0;
               
                if(status != 1 && status != 2)
                    text[i_text++] = str[i];               
            }
            break;
            case '\\': //string like this : puts("the string is not end \\\" here \" ");
            {
                if(status == 3 || status == 4) // in a string
                {
                    if(str[i+1] == '\\' || str[i+1] == '\"' || str[i+1] == '\'')
                    {
                        // still in a string, the string not end, status would not be changed!
                        text[i_text++] = str[i]; // char <\>
                        text[i_text++] = str[i+1]; // char <\> or <">
                        ++i; // skip next char
                    }
                    else
                    {
                        text[i_text++] = str[i];
                    }
                   
                }
                else if (status != 1 && status != 2)
                {
                    text[i_text++] = str[i];
                }
            }
            break;
            case '\'': // string like this : case '"';
            {
                if(status == 0)
                    status = 4;
                else if(status == 4)
                    status = 0;
               
                if(status != 1 && status != 2)
                    text[i_text++] = str[i];
            }
            break;
            case '/':
            {
                if(str[i+1] == '/' && status == 0)
                {
                    status = 1;
                    ++i;// skip next char
                }
                else if(str[i+1] == '*' && status == 0)
                {
                    status = 2;
                    ++i;
                }
                else if(status != 1 && status != 2)
                {
                    text[i_text++] = str[i];
                }
            }
            break;
            case '*':
            {
                if(str[i+1] == '/' && status == 2)
                {
                    status = 0;
                    ++i;
                }
                else if(status != 1 && status != 2)
                {
                    text[i_text++] = str[i];
                }
            }
            break;
            default:
                if(status != 1 && status != 2)
                    text[i_text++] = str[i];
                break;
            }
        }
        if(keep_line == 0)
        {
            int k=0;
            for (k=0; k             {
                if(isgraph(text[k]))
                {
                    break;
                }
            }
            if(k < strlen(text))
                out<         }
        else
            out<     }
    cout<<"+new file: "< "<     return 0;
}

//--------------------------------------------------------------------------------------------------------------------
Makefile

comdel:comdel.cpp
    g++ comdel.cpp -o comdel -O2
clean:
    rm comdel
install:comdel
    cp comdel /usr/local/bin/

//----------------------------------------------------------------------------------------------------------------------
test

wang@ubuntu:~/桌面$ cat test.c
#include
// test comdel.
int main(void)
{
        /* this is single-line comment */
        /*
           this is muilt-line comment
        */
        /*
        //    comment */ int i =10;
        printf("hello world!!//nihao /*gooddss*/\n");/* comment */
        /* comment */puts("the string is not end\\\\\" here \"");
}
wang@ubuntu:~/桌面$ comdel test.c
+new file: test.c ---> test.c_
wang@ubuntu:~/桌面$ cat comdel test.c_
cat: comdel: 没有该文件或目录
#include
int main(void)
{
int i =10;
        printf("hello world!!//nihao /*gooddss*/\n");
        puts("the string is not end\\\\\" here \"");
}
阅读(1715) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~