commit 451499435c91584a1f9f75910aaf29914b7034f4
parent 59105ffb7403a05caaf0a4ee83e2c7d4049d40e5
Author: ssnf <ssnf@ssnf.xyz>
Date: Thu, 10 Jul 2025 01:42:21 -0500
port libsdb
Diffstat:
10 files changed, 203 insertions(+), 0 deletions(-)
diff --git a/include/sdb.h b/include/sdb.h
@@ -0,0 +1,21 @@
+AUTOLIB(sdb)
+
+typedef struct {
+ String ln; /*current line*/
+ Sdbr *r;
+ Biobuf *b;
+ ulong n; /*current Sdbr*/
+} Sdb;
+
+void sdb_add(Sdb*, Sdbr);
+void sdb_close(Sdb*);
+void sdb_edit(Sdb*, Sdbr, Sdbr);
+void sdb_flush(Sdb*);
+void sdb_Bopen(Sdb*, Biobuf*);
+ulong sdb_n(Sdb);
+int sdb_next(Sdb*);
+void sdb_open(Sdb*, char*);
+void sdb_openf(Sdb*, char*, ...);
+int sdb_query(Sdb*, Sdbr);
+void sdb_remove(Sdb*);
+void sdb_rewind(Sdb*);
diff --git a/src/libsdb/mkfile b/src/libsdb/mkfile
@@ -0,0 +1,16 @@
+<$PLAN9/src/mkhdr
+
+LIB=libsdb.a
+
+OFILES=\
+ sdb_add.$O\
+ sdb_edit.$O\
+ sdb_flush.$O\
+ sdb_next.$O\
+ sdb_open.$O\
+ sdb_query.$O\
+ sdb_remove.$O\
+
+HFILES= $PLAN9/include/sdb.h
+
+<$PLAN9/src/mksyslib
diff --git a/src/libsdb/sdb_add.c b/src/libsdb/sdb_add.c
@@ -0,0 +1,8 @@
+#include "std.h"
+
+void
+sdb_add(Sdb *db, Sdbr r)
+{
+ sdbr_dup(Vecadd(&db->r), r);
+ db->n = Vecsiz(db->r) - 1;
+}
diff --git a/src/libsdb/sdb_edit.c b/src/libsdb/sdb_edit.c
@@ -0,0 +1,17 @@
+#include "std.h"
+
+void
+sdb_edit(Sdb *db, Sdbr q, Sdbr r)
+{
+ ulong n;
+
+ n = db->n;
+ if (!sdb_query(db, q))
+ sdb_add(db, q);
+ for (;sdb_query(db, q);) {
+ sdbr_edit(db->r + db->n, r);
+ if (!sdb_next(db))
+ break;
+ }
+ db->n = n;
+}
diff --git a/src/libsdb/sdb_flush.c b/src/libsdb/sdb_flush.c
@@ -0,0 +1,11 @@
+#include "std.h"
+
+void
+sdb_flush(Sdb *db)
+{
+ ulong i;
+
+ for (;sdb_next(db););
+ for (i = 0; i < Vecsiz(db->r); ++i)
+ sdbr_print(db->r[i]);
+}
diff --git a/src/libsdb/sdb_next.c b/src/libsdb/sdb_next.c
@@ -0,0 +1,36 @@
+#include "std.h"
+
+int
+sdb_next(Sdb *db)
+{
+ String s;
+ ulong n;
+
+ if (db->n + 1 < Vecsiz(db->r))
+ return ++db->n;
+ if (!db->ln.n) {
+ for (;Strgets(&db->ln, db->b);) { /*skip comments and whitespace*/
+ if (db->ln.s[0] == '#')
+ continue;
+ for (n = 0; n < db->ln.n && isspace(db->ln.s[n]); ++n);
+ if (!isspace(db->ln.s[n]))
+ break;
+ }
+ if (!db->ln.n)
+ return 0;
+ }
+ if (Vecsiz(db->r) && Vecsiz(db->r[db->n].attr))
+ ++db->n;
+ Strinit(&s);
+ Stradds(&s, db->ln);
+ for (;Strgets(&db->ln, db->b);) {
+ if (db->ln.s[0] == '#')
+ continue;
+ else if (!isspace(db->ln.s[0]))
+ break;
+ Stradds(&s, db->ln);
+ }
+ sdbr_str2r(Vecadd(&db->r), s);
+ Strclose(&s);
+ return Vecsiz(db->r[db->n].attr);
+}
+\ No newline at end of file
diff --git a/src/libsdb/sdb_open.c b/src/libsdb/sdb_open.c
@@ -0,0 +1,57 @@
+#include "std.h"
+
+void
+sdb_Bopen(Sdb *db, Biobuf *b)
+{
+ Strinit(&db->ln);
+ Vecinitf(&db->r, sdbr_init, sdbr_close);
+ db->b = b;
+ db->n = 0;
+}
+
+void
+sdb_close(Sdb *db)
+{
+ Strclose(&db->ln);
+ Vecclose(&db->r);
+ Bterm(db->b);
+}
+
+void
+sdb_open(Sdb *db, char *file)
+{
+ Biobuf *b;
+
+ if (!file || file[0] == '\0')
+ b = Bfdopen(0, OREAD);
+ else
+ b = Bopen(file, OREAD);
+ if (!b)
+ sysfatal("Bopen(%s): %r", file);
+ sdb_Bopen(db, b);
+}
+
+void
+sdb_openf(Sdb *db, char *fmt, ...)
+{
+ va_list ap;
+ char *p;
+
+ va_start(ap, fmt);
+ p = vsmprint(fmt, ap);
+ va_end(ap);
+ sdb_open(db, p);
+ free(p);
+}
+
+ulong
+sdb_n(Sdb db)
+{
+ return Vecsiz(db.r);
+}
+
+void
+sdb_rewind(Sdb *db)
+{
+ db->n = 0;
+}
diff --git a/src/libsdb/sdb_query.c b/src/libsdb/sdb_query.c
@@ -0,0 +1,18 @@
+#include "std.h"
+
+int
+sdb_query(Sdb *db, Sdbr q)
+{
+ ulong n;
+
+ /*TODO: hashquery()? Necessary?*/
+ if (!Vecsiz(db->r) && !sdb_next(db))
+ return 0;
+ n = db->n;
+ do {
+ if (sdbr_query(db->r[db->n], q))
+ return 1;
+ } while (sdb_next(db));
+ db->n = n;
+ return 0;
+}
diff --git a/src/libsdb/sdb_remove.c b/src/libsdb/sdb_remove.c
@@ -0,0 +1,10 @@
+#include "std.h"
+
+void
+sdb_remove(Sdb *db)
+{
+ if (!db->n)
+ return;
+ Vecdel(&db->r, db->n--);
+}
+
diff --git a/src/libsdb/std.h b/src/libsdb/std.h
@@ -0,0 +1,7 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <vec.h>
+#include <str.h>
+#include <sdbr.h>
+#include <sdb.h>
+\ No newline at end of file