aboutsummaryrefslogtreecommitdiff
path: root/src/libndb/ndbaux.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2005-02-11 19:41:16 +0000
committerrsc <devnull@localhost>2005-02-11 19:41:16 +0000
commitd957951b75df08a9bb0293e3e13ff87759afbb92 (patch)
tree4d7868b0d223956217cbc8819d7afb3bec532cca /src/libndb/ndbaux.c
parentad017cfbf5530cfc3ae2fafd723cdade2a4405f6 (diff)
downloadplan9port-d957951b75df08a9bb0293e3e13ff87759afbb92.tar.gz
plan9port-d957951b75df08a9bb0293e3e13ff87759afbb92.tar.bz2
plan9port-d957951b75df08a9bb0293e3e13ff87759afbb92.zip
new
Diffstat (limited to 'src/libndb/ndbaux.c')
-rw-r--r--src/libndb/ndbaux.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/libndb/ndbaux.c b/src/libndb/ndbaux.c
new file mode 100644
index 00000000..94246fc6
--- /dev/null
+++ b/src/libndb/ndbaux.c
@@ -0,0 +1,94 @@
+#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;
+ }
+ if(first)
+ last->line = first;
+ setmalloctag(first, getcallerpc(&cp));
+ return first;
+}