读书人

求可以直接使用的C去重复代码解决思路

发布时间: 2012-04-05 12:42:40 作者: rapoo

求可以直接使用的C去重复代码
我写了一个把wxwidget里面的wxT("")转成_("") 并提取里面字符串生成po格式的代码
但是去重复部分的代码不知道怎样写比较好,求实际可行的代码
附上我的提取字符串代码,求修改

C/C++ code
#include <stdio.h>#include <stdlib.h>int main(int   argc,   char   *argv[]){FILE *input,*output,*langfile;long len;char *buf,*point;int i,j,iswxt=0,is_=0;langfile=fopen("lang.po","wb+");for(i=1;i<argc;i++){input = fopen(argv[i],"rb");fseek( input,0,SEEK_END);len=ftell(input);if(len<1) continue;buf=(char*)malloc(len*sizeof(char));fseek( input,0,SEEK_SET);fread( buf,sizeof(char),len, input);fclose(input);output=fopen(argv[i],"wb");point=buf;for(j=0;j<len;){if(!iswxt&&!is_&&j<len-5){//.................  wxT("if(!iswxt&&*point=='w'&&*(point+1)=='x'   &&*(point+2)=='T'   &&*(point+3)=='('&&*(point+4)=='"' )   {fwrite((void *)"_(\"",sizeof(char),3,output);point+=5;j+=5;iswxt=1;fwrite((void *)"msgid \"",sizeof(char),7,langfile);   }else if(!is_&&*point=='_'   &&*(point+1)=='('&&*(point+2)=='"' )   {fwrite((void *)"_(\"",sizeof(char),3,output);point+=3;j+=3;is_=1;fwrite((void *)"msgid \"",sizeof(char),7,langfile);   }else{fputc(*point,output);point++;j++;}//...................          }else if( (iswxt||is_)&&j<len-1){//...............  ")if(*point=='"'&&*(point+1)==')'&&*(point-1)!='\\')   {iswxt=0;is_=0;fwrite((void *)"\")",sizeof(char),2,output);point+=2;j+=2;fwrite((void *)"\"\r\nmsgstr \"\"\r\n\r\n",       sizeof(char),16,langfile);   }else{    fputc(*point,output);fputc(*point,langfile);    point++;j++;    }//..............      }else{fputc(*point,output);point++;j++;}}fclose(output);//printf("%s\n",argv[i]);//printf("%s\n",buf);}fclose(langfile);    return 0;}


[解决办法]
去重复部分 , 去什么东西重复
[解决办法]
给你个思路,看懂了自己完全能写你要求的mo生成工具
C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct _T_STR{    char* pstr;    struct _T_STR* next;} TStr, *PTStr;int chk_dup(PTStr phdr, const char* pstr){    PTStr pNode = phdr->next;    while(pNode)    {        if(0 == strcmp(pNode->pstr + strlen("_(\""), pstr))            break;        pNode = pNode->next;    }    return ((PTStr)0 != pNode);}void prn_link(PTStr phdr){    PTStr pNode = phdr->next;    while(pNode)    {        printf("%s\n", pNode->pstr);        pNode = pNode->next;    }}void free_link(PTStr phdr){    PTStr pNode = phdr->next;    while(pNode)    {        phdr->next = pNode->next;        free(pNode->pstr);        free(pNode);        pNode = phdr->next;    }}int main(){    const char* arrStr[] = { "wxT(\"123\")", "wxT(\"1234\")", "wxT(\"abc\")", "wxT(\"1234\")", "wxT(\"abcd\")" };        TStr hdr = { 0 };    PTStr pLast = &hdr;    size_t count = sizeof(arrStr) / sizeof(const char*);    size_t i = 0;    for(; i < count; i ++)    {        const char* p = strstr(arrStr[i], "wxT(\"");        if(p)        {            p += strlen("wxT(\"");            if(! chk_dup(&hdr, p))            {                pLast->next = (PTStr)malloc(sizeof(TStr));                pLast->next->pstr = (char*)malloc(strlen(p) + strlen("_(\"") + 1);                strcpy(pLast->next->pstr, "_(\"");                strcat(pLast->next->pstr, p);                pLast->next->next = 0;                pLast = pLast->next;            }        }    }    prn_link(&hdr);    free_link(&hdr);    fflush(stdin);    getchar();    return 0;}
[解决办法]
btw:我想现在机器的内存应该够处理一般大小的po文件了,除非你的po文件比较大,比如说 N gb,那就要考虑磁盘交换了,内存装不下整个链表

读书人网 >C语言

热点推荐