|
|
1.1.1.2 ! root 1: /* ! 2: * killdups.c ! 3: * Remove duplicate quoted strings in a file ! 4: * After each outputted quoted string, adds the PLACEHOLDER ! 5: */ ! 6: ! 7: #include <stdio.h> ! 8: #include <string.h> ! 9: #include <stdlib.h> ! 10: ! 11: #if 0 ! 12: #define PLACEHOLDER "No translation\n" ! 13: #else ! 14: #define PLACEHOLDER "" ! 15: #endif ! 16: ! 17: struct strbuf { ! 18: char *s; ! 19: struct strbuf *l, *r; ! 20: } *top; ! 21: ! 22: ! 23: main () ! 24: { ! 25: char *str, *getstr(); ! 26: ! 27: while (str = getstr()) { ! 28: if (!inlistadd (str)) { ! 29: putstr (str); ! 30: } ! 31: } ! 32: } ! 33: ! 34: ! 35: char * ! 36: getstr () ! 37: { ! 38: static char buf[4096]; ! 39: char *bp; ! 40: int c; ! 41: int bslash; ! 42: char *sbuf; ! 43: ! 44: bp = buf; ! 45: while ((c = getchar()) != EOF && c != '"') ! 46: ; ! 47: if (c == EOF) ! 48: return NULL; ! 49: bslash = 0; ! 50: while ((c = getchar()) != EOF) { ! 51: if (bslash) { ! 52: *bp++ = c; ! 53: bslash = 0; ! 54: } else { ! 55: if (c == '"') ! 56: break; ! 57: if (c == '\\') ! 58: bslash = 1; ! 59: *bp++ = c; ! 60: } ! 61: if (bp - buf >= sizeof(buf)) { ! 62: fprintf (stderr, "String too long\n"); ! 63: exit (2); ! 64: } ! 65: } ! 66: *bp++ = '\0'; ! 67: sbuf = malloc (bp - buf); ! 68: if (!sbuf) { ! 69: fprintf (stderr, "Out of memory\n"); ! 70: exit (1); ! 71: } ! 72: strcpy (sbuf, buf); ! 73: return sbuf; ! 74: } ! 75: ! 76: int ! 77: putstr (s) ! 78: char *s; ! 79: { ! 80: printf ("\"%s\"\n%s\n", s, PLACEHOLDER); ! 81: } ! 82: ! 83: int ! 84: inlistadd (s) ! 85: char *s; ! 86: { ! 87: int delta; ! 88: struct strbuf *list; ! 89: ! 90: if (top == NULL) { ! 91: newentry (&top, s); ! 92: return 0; ! 93: } ! 94: list = top; ! 95: for ( ; ; ) { ! 96: delta = strcmp (s, list->s); ! 97: if (delta == 0) ! 98: return 1; /* Already in list */ ! 99: if (delta > 0) { ! 100: if (list->r == NULL) { ! 101: newentry (&list->r, s); ! 102: return 0; ! 103: } else ! 104: list = list->r; ! 105: } else { ! 106: if (list->l == NULL) { ! 107: newentry (&list->l, s); ! 108: return 0; ! 109: } else ! 110: list = list->l; ! 111: } ! 112: } ! 113: } ! 114: ! 115: int ! 116: newentry (alist, s) ! 117: struct strbuf **alist; ! 118: char *s; ! 119: { ! 120: *alist = (struct strbuf *)malloc (sizeof(struct strbuf)); ! 121: if (*alist == NULL) { ! 122: fprintf (stderr, "Out of memory\n"); ! 123: exit (3); ! 124: } ! 125: (*alist)->s = s; ! 126: (*alist)->l = (*alist)->r = NULL; ! 127: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.