C 1.pass_list_check 2.某个文件中是否存在特定的字符串 3.replace

来源:互联网 发布:mac怎么设置用户名 编辑:程序博客网 时间:2024/05/29 16:28

检查某列中是否存在数据
- pass_list check

char *pass_list[] = {    "/images/",    "/cgi/cgi_wan_multiple.js",    "/chglang.cgi",    "/global.js"#if 0       "/loginzz.htm",    "/wireless_calibration.htm",    "/bottom_pane.htm"#endif}; // 1 pass_list check ok  ,  0 errorint bypass_check(const char *url) {    int i;    for (i=0; pass_list[i] != NULL; i++) {        if (strncasecmp(url, pass_list[i], strlen(pass_list[i])) == 0) {            return 1;        }    }    return 0;}

  • cprintf
#define cprintf(fmt, args...) do { \    FILE *fp = fopen("/dev/console", "w"); \    if (fp) { \        fprintf(fp, fmt, ## args); \        fclose(fp); \    } \} while (0)cprintf("[%d]: url - %s, type - %d, error msg - %s\n", r->index, r->url, type, err_string);

查找

int special_Dameon_is_Existed(char *cmd){       int ret = 0;       FILE *fp;       char temp[256] = "";       system("ps > /tmp/special_dameon_find");       fp = fopen("/tmp/special_dameon_find", "r");       if (fp == NULL)       {               ret = 0;       }       while(fgets(temp,sizeof(temp),fp)!=NULL)       {               if(strstr(temp, cmd) != NULL)               {                       ret = 1;                       break;               }       }       fclose(fp);       system("rm -f /tmp/special_dameon_find");       return ret;}

#include <stdio.h>#include <stdlib.h>#include <string.h>/** * 统计key在data中出现的次数 * @param data 待查找的字符串 * @param key  要查找的字符串 * @return key在data中出现的次数 */int replace_count_string(char *data, char *key){    int count = 0;    int klen = strlen(key);    char *pos_start = data, *pos_end;    while (NULL != (pos_end = strstr(pos_start, key))) {        pos_start = pos_end + klen;        count++;    }    return count;}/** * 将data中的rep字符串替换成to字符串,以动态分配内存方式返回新字符串 * 这个函数不需要保证data能保证容量。 * @param data 待替换某些字符串的数据 * @param rep  待替换的字符串 * @param to   替换成的字符串 * @param free_data 不为0时要释放data的内存 * @return 返回新分配内存的替换完成的字符串,注意释放。 */char *malloc_replace(char *data, char *rep, char *to, int free_data){    int rep_len = strlen(rep);    int to_len  = strlen(to);    int counts  = replace_count_string(data, rep);    //printf("counts = %d\n", counts);    int m = strlen(data) + counts * (to_len - rep_len);    char *new_buf = (char *) malloc(m + 1);    if (NULL == new_buf) {        free(data);        return NULL;    }    memset(new_buf, 0, m + 1);    char *pos_start = data, *pos_end, *pbuf = new_buf;    int copy_len;    while (NULL != (pos_end = strstr(pos_start, rep))) {        copy_len = pos_end - pos_start;        strncpy(pbuf, pos_start, copy_len);        pbuf += copy_len;        strcpy(pbuf, to);        pbuf += to_len;        pos_start  = pos_end + rep_len;    }    strcpy(pbuf, pos_start);    if (free_data)        free(data);    return new_buf;}/** * 将data中的rep字符串替换成to字符串 * 注意保证data空间足够替换完成后的字符串长度 * @param data 待替换某些字符串的数据 * @param rep  待替换的字符串 * @param to   替换成的字符串 * @return 无 */void normal_replace(char *data, char *rep, char *to){    char *new_buf = malloc_replace(data, rep, to, 0);    if (NULL != new_buf) {        strcpy(data, new_buf);        free(new_buf);    }}int main(int argc, char **argv){    char p_source[4096];    char *p_seach = "{subject}";    char *p_repstr = "leo";    strcpy(p_source, "Subject: {subject}, body:Subject2:{subject}; end");    printf("p_source:%s\n", p_source);    printf("p_seach:%s\n",  p_seach);    printf("p_repstr:%s\n", p_repstr);    //替换字符串函数,第一个参数p_source为母字符串,第二个参数p_seach为要替换的子字符串,第三个参数p_repstr为替换成的子字符串。p_source是函数返回处理好的字符串    normal_replace(p_source,  p_seach, p_repstr);     printf("p_source:%s\n", p_source);    return 0;}

结果

p_source:Subject: {subject}, body:Subject2:{subject}; endp_seach:{subject}p_repstr:leop_source:Subject: leo, body:Subject2:leo; end
0 0
原创粉丝点击