From af16af8af42e41f470f44fcd174babda8b52dfc7 Mon Sep 17 00:00:00 2001 From: epoch Date: Wed, 11 Aug 2021 00:02:42 +0000 Subject: added another example of something actually useful to me --- examples/wordtr.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 examples/wordtr.c (limited to 'examples/wordtr.c') diff --git a/examples/wordtr.c b/examples/wordtr.c new file mode 100644 index 0000000..464450f --- /dev/null +++ b/examples/wordtr.c @@ -0,0 +1,45 @@ +#include +#include +#include + +/* + +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; +} -- cgit v1.2.3