- 论坛徽章:
- 2
|
加入单双引号匹配.
- #include <stdio.h>
- int filter(FILE *src_fp, FILE *tar_fp)
- {
- int in_tag = 0;
- int in_str = 0;
- int cnt = 0;
- int c;
- while ((c = fgetc(src_fp)) != EOF) {
- if (in_tag) {
- if (in_str) {
- if (c == in_str) {
- in_str = 0;
- }
- }else {
- if (c == '>') {
- in_tag = 0;
- }else if (c == '"' || c == '\'') {
- in_str = c;
- }
- }
- }else {
- if (c == '<') {
- in_tag = 1;
- }else {
- cnt++;
- fputc(c, tar_fp);
- }
- }
- }
- return cnt;
- }
- int main(int argc, const char *argv[])
- {
- FILE *fp, *tar_fp;
- if (argc == 2) {
- fp = fopen(argv[1], "r");
- if (fp != NULL) {
- filter(fp, stdout);
- fclose(fp);
- return 0;
- }
- }else if (argc == 3) {
- fp = fopen(argv[1], "r");
- if (fp != NULL) {
- tar_fp = fopen(argv[2], "w");
- if (tar_fp != NULL) {
- filter(fp, tar_fp);
- fclose(tar_fp);
- fclose(fp);
- return 0;
- }
- fclose(fp);
- }
- }else {
- printf("syntax: filter <source file> [target file]\n");
- return 0;
- }
- return -1;
- }
复制代码 |
|