aboutsummaryrefslogtreecommitdiffstats
path: root/examples/wordtr.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/wordtr.c')
-rw-r--r--examples/wordtr.c45
1 files changed, 45 insertions, 0 deletions
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 <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;
+}