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
- C 1.pass_list_check 2.某个文件中是否存在特定的字符串 3.replace
- jquery判断字符串中是否存在某个的字符串
- jquery判断字符串中是否存在某个的字符串
- C#判断txt文件中某个字符串是否存在的函数
- 判断字符串中是否存在某个字符
- 截取某个字符串中特定的字符串
- [码海拾贝 之Perl]在字符串数组中查找特定的字符串是否存在
- 用jQuery判断字符串中是否存在某个的字符串的实现代码
- 关于判断字符串中是否含有某个特定字符的问题
- EXCEL判断字符串中是否包含某个特定子串的公式
- jquery获取class的值加入到数组,并判断数组中是否存在特定字符串
- javascript中检测某个字符串在数组中是否存在
- oracle regexp_like实现字符串中判断某个值是否存在
- PHP判断字符串str中是否存在某个值
- JS 判断某个字符串是否存在与数组中
- js 判断字符 是否存在于某个字符串中
- java判断String字符串数组中是否存在某个值
- 通过JS,Jquery判断某个字符在一个字符串中是否存在的方法
- Java线程Thread类
- 面向过程和面向对象的区别
- Android 沉浸式通知栏
- Android应用如何退到首页/登录页
- 博客搬家了!!!
- C 1.pass_list_check 2.某个文件中是否存在特定的字符串 3.replace
- 51nod最长公共子序列问题
- 最短路
- ajax调用controller后页面无法跳转的问题
- dialog居于底部并使得宽度占满整个屏幕宽度
- c++继承上
- Linux环境下MySQL数据库大小写区分问题
- 机房收费系统之“基本数据设定的含义“
- java中线程的同步:(互斥,协作)