diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libndb/ndbaux.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libndb/ndbaux.c')
-rwxr-xr-x | sys/src/libndb/ndbaux.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/sys/src/libndb/ndbaux.c b/sys/src/libndb/ndbaux.c new file mode 100755 index 000000000..5c2d23d9c --- /dev/null +++ b/sys/src/libndb/ndbaux.c @@ -0,0 +1,95 @@ +#include <u.h> +#include <libc.h> +#include <bio.h> +#include <ctype.h> +#include <ndb.h> +#include "ndbhf.h" + + +/* + * parse a single tuple + */ +char* +_ndbparsetuple(char *cp, Ndbtuple **tp) +{ + char *p; + int len; + Ndbtuple *t; + + /* a '#' starts a comment lasting till new line */ + EATWHITE(cp); + if(*cp == '#' || *cp == '\n') + return 0; + + t = ndbnew(nil, nil); + setmalloctag(t, getcallerpc(&cp)); + *tp = t; + + /* parse attribute */ + p = cp; + while(*cp != '=' && !ISWHITE(*cp) && *cp != '\n') + cp++; + len = cp - p; + if(len >= Ndbalen) + len = Ndbalen-1; + strncpy(t->attr, p, len); + + /* parse value */ + EATWHITE(cp); + if(*cp == '='){ + cp++; + if(*cp == '"'){ + p = ++cp; + while(*cp != '\n' && *cp != '"') + cp++; + len = cp - p; + if(*cp == '"') + cp++; + } else if(*cp == '#'){ + len = 0; + } else { + p = cp; + while(!ISWHITE(*cp) && *cp != '\n') + cp++; + len = cp - p; + } + ndbsetval(t, p, len); + } + + return cp; +} + +/* + * parse all tuples in a line. we assume that the + * line ends in a '\n'. + * + * the tuples are linked as a list using ->entry and + * as a ring using ->line. + */ +Ndbtuple* +_ndbparseline(char *cp) +{ + Ndbtuple *t; + Ndbtuple *first, *last; + + first = last = 0; + while(*cp != '#' && *cp != '\n'){ + t = 0; + cp = _ndbparsetuple(cp, &t); + if(cp == 0) + break; + if(first){ + last->line = t; + last->entry = t; + } else + first = t; + last = t; + t->line = 0; + t->entry = 0; + setmalloctag(t, getcallerpc(&cp)); + } + if(first) + last->line = first; + ndbsetmalloctag(first, getcallerpc(&cp)); + return first; +} |