Chinaunix首页 | 论坛 | 博客
  • 博客访问: 755418
  • 博文数量: 119
  • 博客积分: 137
  • 博客等级: 少校
  • 技术积分: 1582
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-28 16:39
文章分类

全部博文(119)

文章存档

2017年(3)

2016年(7)

2014年(1)

2013年(8)

2012年(20)

2011年(27)

2010年(53)

分类: C/C++

2012-06-14 15:13:19

两个功能
1. 注释 #define USE_FILE
   执行的是字符串替换功能
2. 不注释 #define USE_FILE
   执行的是文件中的字符串解析(分割)功能
其中字符串替换 使用 void strReplace(char* source, char* sub, char* rep);

该文档留作备用

点击(此处)折叠或打开

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include <string.h>

  4. //#define USE_FILE

  5. #ifdef USE_FILE
  6. #define CHILLI_CONFIG "./test.txt"
  7. #endif
  8. void strReplace(char* source, char* sub, char* rep)
  9. {
  10.     char* src = source;
  11.     char* temp= strstr(source,sub);
  12.     char end_result[256]="";
  13.     char *result = NULL;
  14.     
  15.     temp = strstr(source,sub);
  16.     if(temp)
  17.     {
  18.         result = malloc( sizeof(char *) * (strlen(source) + strlen(rep)) );
  19.         strcpy(end_result,rep);
  20.         strcpy(result,temp+strlen(sub));
  21.         strReplace(result,sub,rep);
  22.         strcat(end_result,result);
  23.         source[strlen(source) - strlen(temp)] = '\0';
  24.         strcpy(result,source);
  25.         strcat(result,end_result);
  26.         strcpy(source,result);    
  27.         free(result);    
  28.     }
  29. }



  30. int strOperate(char *str)
  31. {
  32. #ifdef USE_FILE    
  33.     FILE *fd;
  34.     char seps[] = ";\n";
  35. #else
  36.     char seps[] = ",\n";
  37. #endif
  38.     char buffer[256] = {0};
  39.     int IsFind=0;
  40.     
  41.     char name[32] = {0};
  42.     char value[256] = {0};
  43.     
  44.     char ddd[]="*.brasiltelecom.com.br,*.fon.com,ssl.google-analytics.com,fon-en.custhelp.com,
  45. *.telemar.com.br,maps.gstatic.com,maps.google.com,!wifi.fon.com,a4.g.akamai.net,access.skype.com,
  46. *.paypal.com,*.paypalobjects.com,*.clickandbuy.com,mpsnare.iesnare.com,*.verisign.com,crl.comodoca.com,
  47. *.oi.net.br,*.ezconet.com.br,*.zendesk.com,gg.google.com,mt.l.google.com,mt0.google.com,mt1.google.com,
  48. ocsp.comodoca.com,ocsp.usertrust.com,*.oi.com.br,*.ig.com.br,*.btfon.com,*.nintendowifi.net,a248.e.akamai.net,*.wii.com,*.ss.*.com";

  49.     char cmd[256] = "uamallowed ";
  50.     
  51.     

  52.     char *token ;
  53.     int i = 0;
  54. #ifdef USE_FILE    
  55.     if((fd=fopen(CHILLI_CONFIG,"r"))==NULL)
  56.     {
  57.         return 0;
  58.     }    

  59.     while(fgets(buffer, 256, fd))
  60.     {
  61.          if( strlen(buffer) > 1 )
  62.          {
  63.             token = strtok( buffer, seps );
  64. #else
  65.             token = strtok( ddd, seps );
  66. #endif
  67.             //printf("!^wifi.fon.com$\n");
  68.             while( token != NULL )
  69.             {

  70. #ifdef USE_FILE    
  71.                 sprintf(cmd,"uamallowed %s",token);
  72.                 //strcat(cmd,token);
  73.                 //strcat(cmd,"\n");
  74. #else
  75.                 char *tempString = NULL;
  76.                 tempString = malloc(strlen(token) * sizeof(char *) );
  77.              strcpy(tempString,token);
  78.                 strReplace( tempString , "*." , "(.*\\.)?" );
  79.                 if( tempString[0] == '!')
  80.                 {
  81.                     sprintf(cmd,"!^%s$",tempString);
  82.                 }
  83.                 else
  84.                 {
  85.                     sprintf(cmd,"^%s$",tempString);
  86.                 }                
  87.                 free(tempString);                
  88.                 i++;                
  89. #endif            
  90.                 printf("%s\n",cmd);
  91.                 token = strtok( NULL, seps );                
  92.             }
  93. #ifdef USE_FILE
  94.          }
  95.     }
  96.    
  97.     fclose(fd);
  98.  
  99.     printf("\n\n\n\n",CHILLI_CONFIG);
  100. #endif        
  101.     return IsFind;
  102. }

  103. int main(void)
  104. {
  105.     char *str1="START_CHILLI=1";
  106.     int t1=0,t2=0;
  107.     strOperate(str1);   
  108.     return 0;
  109. }

test.txt

点击(此处)折叠或打开

  1. 213.134.44.0/23,www.paypal.com,www.paypalobjects.com;paypal.112.2o7.net;202.232.239.16/28,203.180.85.64/26,173.203.47.186;www.delta.fon.com,shop.delta.fon.com,maps.delta.fon.com;www.sandbox.paypal.com,sandbox.paypal.com,developer.paypal.com


阅读(1372) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~