/* Vide https://rudhar.com/lingtics/intrlnga/cgi-grep/fria/intro-ia.htm e specificamente https://rudhar.com/lingtics/intrlnga/cgi-grep/fria/intro-ia.htm#frhtmtxt Isto es un variante, que extrahe le parolas e expression in interlingua ex le dictionario francese-interlingua. Le 16 de februario 2020, © Ruud Harmsen, ma face con illo quecunque vos vole. */ #include #include #include #include char *DT = "
"; char *DD = "
"; char *DL = "
"; char *SDL = "
"; char *CLN = " : "; char *ITAL = ""; char *SITAL = ""; char *OutFile1 = "tmp03.txt"; char *OutFile2 = "tmp04.txt"; struct se_SubString { char *sub; int len; }; typedef struct se_SubString se_SubstrSpec; static int TractaLinea (char *linea, FILE *fpo1, FILE *fpo2); static int TrovaClasseDeParola (char *line, char *ClasseDeParola, size_t sizeClasseDeParola); static int PlenaLista (char *linea, char *ClasseDeParola, FILE *fpo1); static se_SubstrSpec *TrovaCommas (char *linea); int main (int argc, char **argv) { static char line1[4096]; FILE *fpi = stdin, *fpo1, *fpo2; int inprepost = 1; long linecnt = 0, blanks = 0, dts = 0, dtss = 0, dds = 0; long prepost = 0, unrecog = 0, invalid = 0; char *s; if ((fpo1 = fopen(OutFile1, "wt")) == NULL) { fprintf(stderr, "%s: cannot open output file %s\n", argv[0], OutFile1); return 1; } if ((fpo2 = fopen(OutFile2, "wt")) == NULL) { fprintf(stderr, "%s: cannot open output file %s\n", argv[0], OutFile2); return 2; } while (fgets(line1, sizeof line1, fpi)) { linecnt++; if (s = strstr(line1, DL), s && s == line1) { inprepost = 0; prepost++; continue; } else if (s = strstr(line1, SDL), s && s == line1) { inprepost = 1; } if (inprepost) { prepost++; } else { if (line1[0] == '\n' && line1[1] == '\0') { /* Skip empty lines */ blanks++; } else { TractaLinea(line1, fpo1, fpo2); } } } if (fpo1) fclose(fpo1); if (fpo2) fclose(fpo2); return 0; } static int TractaLinea (char *linea, FILE *fpo1, FILE *fpo2) { char *s, *n, *c, *l; char ClasseDeParola[20]; c = strstr(linea, CLN); if (c) { c += strlen(CLN); l = c + strlen(c) - 1; if (*l == '\n') *l = '\0'; } if (s = strstr(linea, DT), s && s == linea) { if (c) { TrovaClasseDeParola(linea, ClasseDeParola, sizeof ClasseDeParola); PlenaLista(c, ClasseDeParola, fpo1); } } else if (s = strstr(linea, DD), s && s == linea) { n = s + strlen(DD); if ((isdigit(n[0]) && n[1] == '.') || (isdigit(n[0]) && isdigit(n[1]) && n[2] == '.')) { if (c) { TrovaClasseDeParola(linea, ClasseDeParola, sizeof ClasseDeParola); PlenaLista(c, ClasseDeParola, fpo1); } } else { if (c) { se_SubstrSpec *Places, *p; Places = TrovaCommas(c); for (p = Places; p->sub; p++) { fprintf(fpo2, "· %.*s\n", (int)p->len, p->sub); } } } } return 0; } static int TrovaClasseDeParola (char *line, char *ClasseDeParola, size_t sizeClasseDeParola) { char *b, *e; memset(ClasseDeParola, 0, sizeClasseDeParola); if ((b = strstr(line, ITAL)) != NULL && (e = strstr(b, SITAL)) != NULL) { b += strlen(ITAL); if (e - b > sizeClasseDeParola - 1) { e = b + sizeClasseDeParola - 1; } if (e > b && e[-1] == '.') e--; strncpy(ClasseDeParola, b, e - b); } return 0; } static int PlenaLista (char *linea, char *ClasseDeParola, FILE *fpo1) { se_SubstrSpec *Places, *p; Places = TrovaCommas(linea); for (p = Places; p->sub; p++) { fprintf(fpo1, "· %.*s · %s\n", (int)p->len, p->sub, ClasseDeParola); } return 0; } #define NUMPOINTERS 20 char *sep = ", "; static se_SubstrSpec *TrovaCommas (char *linea) { static se_SubstrSpec Places[NUMPOINTERS + 1]; int i = 0; char *b, *bn, *e; memset(Places, '\0', sizeof Places); for (b = linea, i = 0; i < NUMPOINTERS && *b; i++) { e = strstr(b, sep); if (e) { bn = e + strlen(sep); } else { e = b + strlen(b); bn = e + 1; } Places[i].sub = b; Places[i].len = e - b; b = bn; } Places[i].sub = NULL; return Places; }