Blob
1 #include <u.h>2 #include <libc.h>3 #include <bio.h>4 #include <ctype.h>5 #include <ndb.h>6 #include "ndbhf.h"8 /*9 * free a parsed entry10 */11 void12 ndbfree(Ndbtuple *t)13 {14 Ndbtuple *tn;16 for(; t; t = tn){17 tn = t->entry;18 if(t->val != t->valbuf){19 free(t->val);20 }21 free(t);22 }23 }25 /*26 * set a value in a tuple27 */28 void29 ndbsetval(Ndbtuple *t, char *val, int n)30 {31 if(n < Ndbvlen){32 if(t->val != t->valbuf){33 free(t->val);34 t->val = t->valbuf;35 }36 } else {37 if(t->val != t->valbuf)38 t->val = realloc(t->val, n+1);39 else40 t->val = malloc(n+1);41 if(t->val == nil)42 sysfatal("ndbsetval %r");43 }44 strncpy(t->val, val, n);45 t->val[n] = 0;46 }48 /*49 * allocate a tuple50 */51 Ndbtuple*52 ndbnew(char *attr, char *val)53 {54 Ndbtuple *t;56 t = mallocz(sizeof(*t), 1);57 if(t == nil)58 sysfatal("ndbnew %r");59 if(attr != nil)60 strncpy(t->attr, attr, sizeof(t->attr)-1);61 t->val = t->valbuf;62 if(val != nil)63 ndbsetval(t, val, strlen(val));64 return t;65 }