#include #include #include #include struct hash_entry { struct hash_entry * next; int points; int found; char name[1]; }; int hashfn (char * name) { unsigned char h = 0; while (*name) h += *name++; return h; } struct hash_entry * hashtab[256]; int optwarn = 0; void hashadd (char * name, int points) { struct hash_entry ** pp = &hashtab[hashfn(name)]; while (*pp) { if (strcmp ((*pp)->name, name) == 0) break; pp = &(*pp)->next; } if (*pp) { if (optwarn && ((*pp)->points != points)) fprintf (stderr, "WARNUNG: %s bekommt mehrmals Punkte\n", name); return; } (*pp) = calloc (1, sizeof (struct hash_entry) + strlen (name)); strcpy ((*pp)->name, name); (*pp)->next = NULL; (*pp)->points = points; (*pp)->found = 0; } struct hash_entry * hashlookup (char * name) { struct hash_entry * p = hashtab[hashfn(name)]; while (p) { if (strcmp (p->name, name) == 0) return p; p = p->next; } return p; } char * progname; void usage () { fprintf (stderr, "usage: %s [-w] [-o outfile] template points\n", progname); exit (1); } int main (int argc, char * argv[]) { FILE * outfile = NULL; FILE * template = NULL; FILE * points = NULL; int c, i; char buf[100]; progname = argv[0]; while ((c = getopt (argc, argv, "o:wh")) != EOF) { switch (c) { case 'o': if (outfile) usage (); outfile = fopen (optarg, "w"); if (!outfile) usage (); break; case 'w': if (optwarn) usage (); optwarn = 1; break; case 'h': default: usage (); } } if (optind != (argc - 2)) usage (); template = fopen (argv[optind], "r"); if (!template) usage (); points = fopen (argv[optind+1], "r"); if (!points) usage (); if (!outfile) outfile = stdout; while (1) { int pts; fgets (buf, 100, points); if (feof (points)) break; buf[strlen(buf)-1] = 0; i = 0; while (buf[i] && (buf[i] != ':')) i++; if ((i==0) || (buf[i] != ':') || (sscanf (&(buf[i+1]), "%d", &pts) != 1)) { if (optwarn) fprintf (stderr, "WARNUNG: Zeile %s ignoriert\n", buf); continue; } buf[i] = 0; hashadd (buf, pts); } fclose (points); while (1) { struct hash_entry * p; fgets (buf, 100, template); if (feof (template)) break; buf[strlen(buf)-1] = 0; p = hashlookup (buf); if (!p) { fprintf (outfile, "%s\n", buf); } else { p->found = 1; fprintf (outfile, "%s:%d\n", buf, p->points); } } if (!optwarn) return 0; for (i=0; i<256; ++i) { struct hash_entry * p; for (p = hashtab[i]; p; p = p->next) if (!p->found) fprintf (stderr, "WARNUNG: %s ist kein Teilnehmer der Vorlesung bekommt aber Punkte\n", p->name); } return 0; }