stew

a monorepo of some sort
git clone git://git.nsmpr.xyz/stew.git
Log | Files | Refs

wdb2fltp.c (1258B)


      1 /*
      2  * convert wdb databse from tablist to fltp format
      3  * hardcodes galore
      4  */
      5 
      6 
      7 #include <u.h>
      8 #include <libc.h>
      9 
     10 #include <util/util.h>
     11 #include <tablist/tablist.h>
     12 
     13 TLdecoder *tld;
     14 
     15 void printslice(Slice *);
     16 
     17 void
     18 printself(TLnode *node){
     19 	int i;
     20 	char *buf;
     21 	if(node->name != nil){
     22 		buf = malloc(node->name->len + 1);
     23 		for (i = 0; i < node->name->len; i++) {
     24 			char *s = slicegetp(node->name, i);
     25 			buf[i] = *s;
     26 		}
     27 		buf[node->name->len] = '\0';
     28 	}
     29 	else{
     30 		buf = malloc(node->value->len + 1 + 3);
     31 		buf[0] = '.';
     32 		buf[1] = 'n';
     33 		buf[2] = ' ';
     34 		for (i = 0; i < node->value->len; i++) {
     35 			char *s = slicegetp(node->value, i);
     36 			buf[i + 3] = *s;
     37 		}
     38 		buf[node->value->len + 3] = '\0';
     39 	}
     40 	print("%s\n", buf);
     41 	free(buf);
     42 }
     43 
     44 void
     45 printnode(TLnode *node){
     46 	if (node->children->len > 0){
     47 		print("{\n");
     48 		printself(node);
     49 		printslice(node->children);
     50 		print("}\n");
     51 	} else printself(node);
     52 }
     53 
     54 void
     55 printslice(Slice *s){
     56 	int i;
     57 	void **v;
     58 	TLnode *node;
     59 	for(i = 0; i < s->len; i++){
     60 		v = slicegetp(s, i);
     61 		node = *v;
     62 		printnode(node);
     63 	}
     64 }
     65 
     66 void
     67 main(void){
     68 	char *buf;
     69 	long n;
     70 	tld = initTLdecoder(nil);
     71 	buf = malloc(8912);
     72 	while((n = read(0, buf, 8912)) > 0) TLdecode(tld, buf, n);
     73 	if(n < 0) sysfatal("%r");
     74 	printslice(tld->nodes);
     75 }