aboutsummaryrefslogtreecommitdiffstats
path: root/examples/wordtr.c
blob: 464450f25dfd17ade6f53fdfc3062c2cfcec0a8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <hashtable.h>
#include <string.h>
#include <stdio.h>

/*

Checking the hashtable before loading the arguments is just in case all of the stdin
is stuff that can be translated without needing to load all the arguments into the hashtable.
Which should make the program go faster, right?

If it isn't in the hashtable, it loads the translations from arguments until it finds the one
it needs for the current line is has read from stdin, prints the replacement, and reads another
line from stdin.

If all of that fails, it will have loaded all of the arguments, gives up by just printing the
line it got from stdin.

*/

int main(int argc,char *argv[]) {
  char line[1024];
  char *p;
  struct hashtable ht;
  inittable(&ht,512);
  argc--;
  argv++;
super_continue:
  while(fgets(line,sizeof(line),stdin)) {
    if((p=strchr(line,'\n'))) *p=0;
    if((p=ht_getvalue(&ht,line))) {
      printf("%s\n",p);
      continue;
    }
    for(;argc > 1;argv+=2,argc-=2) {
      ht_setkey(&ht,argv[0],argv[1]);
//      printf("%s %s\n",argv[0],argv[1]); //to see how stuff is loading ofc.
      if(!strcmp(argv[0],line)) {
        printf("%s\n",argv[1]);
        goto super_continue;
      }
    }
    printf("%s\n",line);
  }
  return 0;
}