int createrulelink_f(char *file){ //解析文件 char path[120]={0}; char line[1024] = { 0 }; char *data[10]={0}; struct ruledata rd={0}; FILE *fp; int status=0; culine=0; getpath(path,file); cufile=path; if(!(fp=fopen(path,"r"))){ printf("指定规则文件%s不存在\n",path); exit(1); } while (1){ //遍历规则文件,以行模式 int i = 0; int j = 1; if(!fgets(line, 1023, fp)) break; culine++; if(isignore(line[0])) //判断行首字符是不是注释符 break; data[0] = line; while (line[i]){ //以空格分割规则成10部分 if (line[i] == ' '){ line[i] = '\0'; data[j] = (char *)(line + i+1); j++; } i++; if (j == 10) break; } if(islegal(data,culine)){ //验证规则合法性 exit(0); } datatostruct(data,&rd); //从规则中提取出数据,获得ruledata结构 linetorules(&rd);//使用ruledata数据结构中数据转化成规则体,挂载到rks规则链中 } return 0; }
const char* text_decode_markup(const char *p, char **textp, int maxlength) { char ch, prev_ch; int h1, h2; uint32_t i; char *tp, *lp, *s; bool prev_lb, prev_ws, url, open_url, open_tag[nmarkup]; tp = *textp; if (*p != '=' || !*p) return 0; p++; if (*p == '&' || !*p) return 0; lp = tp; ch = *p++; prev_lb = 1; prev_ws = 1; prev_ch = 0; url = 0; open_url = 0; memset(open_tag, 0, sizeof(open_tag)); do { if(ch == '+') { ch = ' '; } else if(ch == '%') { if((h1 = tohex(p[0])) >= 0 && (h2 = tohex(p[1])) >= 0) { p += 2; ch = (h1 << 4) | h2; if(ch == '&') { *tp++ = '&'; *tp++ = 'a'; *tp++ = 'm'; *tp++ = 'p'; ch = ';'; } } } if (ch == '<' || ch == '>') { *tp++ = '&'; *tp++ = (ch == '<') ? 'l' : 'g'; *tp++ = 't'; ch = ';'; } if (ch == '"') { *tp++ = '&'; *tp++ = 'q'; *tp++ = 'u'; *tp++ = 'o'; *tp++ = 't'; ch = ';'; } if (url) { if (ch == '\n' || iswhitespace(ch)) { tp += sprintf(tp, "\">"); url = 0; open_url = 1; lp = tp + 1; continue; } } if (ch == '\n') { if (prev_lb) continue; prev_lb = 1; *tp++ = '<'; *tp++ = 'b'; *tp++ = 'r'; ch = '>'; } else if (iswhitespace(ch)) { if (prev_ws) continue; prev_ws = 1; } else if (isignore(ch)) { continue; } else { prev_lb = 0; prev_ws = 0; if (ch == prev_ch && !url) { s = strchr(markup_ch, ch); if (s) { i = s - markup_ch; if (i == 2 || !open_tag[2]) { tp--; if (open_tag[i]) tp += sprintf(tp, "</%s", markup_tags[i]); else tp += sprintf(tp, "<%s", markup_tags[i]); open_tag[i] = !open_tag[i]; ch = '>'; } } else if (ch == '@') { tp--; if (!open_url) tp += sprintf(tp, "<a href="), url = 1, ch = '"'; else tp += sprintf(tp, "</a"), ch = '>'; open_url = 0; } } lp = tp + 1; } *tp++ = ch; prev_ch = ch; if (!--maxlength) { while (*p && *p++ != '&'); break; } } while(*p && (ch = *p++) != '&'); if (lp == *textp) return 0; if (url) lp += sprintf(lp, "\"></a>"); else if (open_url) lp += sprintf(lp, "</a>"); for (i = 0; i < sizeof(open_tag); i++) if (open_tag[i]) lp += sprintf(lp, "</%s>", markup_tags[i]); *lp++ = 0; *textp = lp; return p; }
const char* text_decode(const char *p, char **textp, int maxlength, int level) { char ch; int h1, h2; char *tp, *lp; bool prev_lb, prev_ws; tp = *textp; if (*p != '=' || !*p) return 0; p++; if (*p == '&' || !*p) return 0; lp = tp; ch = *p++; prev_lb = 1; prev_ws = 1; do { if(ch == '+') { ch = ' '; } else if(ch == '%') { if((h1 = tohex(p[0])) >= 0 && (h2 = tohex(p[1])) >= 0) { p += 2; ch = (h1 << 4) | h2; if(ch == '&') { *tp++ = '&'; *tp++ = 'a'; *tp++ = 'm'; *tp++ = 'p'; ch = ';'; } } } if(ch == '<' || ch == '>') { *tp++ = '&'; *tp++ = (ch == '<') ? 'l' : 'g'; *tp++ = 't'; ch = ';'; } if (ch == '"') { *tp++ = '&'; *tp++ = 'q'; *tp++ = 'u'; *tp++ = 'o'; *tp++ = 't'; ch = ';'; } if (ch == '\n') { if (level & 2) return 0; if ((level & 1) && prev_lb) continue; prev_lb = 1; *tp++ = '<'; *tp++ = 'b'; *tp++ = 'r'; *tp++ = '>'; continue; } else if (iswhitespace(ch)) { if (level) { if (!(level & 1)) return 0; if (prev_ws) continue; prev_ws = 1; } } else if (isignore(ch)) { continue; } else { prev_lb = 0; prev_ws = 0; lp = tp + 1; } *tp++ = ch; if (!--maxlength) { if (!level) return 0; while (*p && *p++ != '&'); break; } } while(*p && (ch = *p++) != '&'); if (lp == *textp) return 0; *lp++ = 0; *textp = lp; return p; }