]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libndb/ndbfree.c
games/nes: workaround for truncated chr
[plan9front.git] / sys / src / libndb / ndbfree.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include <ndb.h>
6 #include "ndbhf.h"
7
8 /*
9  *  free a parsed entry
10  */
11 void
12 ndbfree(Ndbtuple *t)
13 {
14         Ndbtuple *tn;
15
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 }
24
25 /*
26  *  set a value in a tuple
27  */
28 void
29 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                 else
40                         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 }
47
48 /*
49  *  allocate a tuple
50  */
51 Ndbtuple*
52 ndbnew(char *attr, char *val)
53 {
54         Ndbtuple *t;
55
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         ndbsetmalloctag(t, getcallerpc(&attr));
65         return t;       
66 }
67
68 /*
69  *  set owner of a tuple
70  */
71 void
72 ndbsetmalloctag(Ndbtuple *t, uintptr tag)
73 {
74         for(; t != nil; t=t->entry)
75                 setmalloctag(t, tag);
76 }