commit 94ad0043622327173f2d441116da2304ce4ccb48
parent 635a95056374e3834ce9c596f9c525d21667f990
Author: ssnf <ssnf@ssnf.xyz>
Date: Sun, 3 Aug 2025 04:48:37 +0000
libsdb: fix parsing
Diffstat:
4 files changed, 21 insertions(+), 24 deletions(-)
diff --git a/man/man3/sdb.3 b/man/man3/sdb.3
@@ -104,7 +104,7 @@ returns the number of records currently loaded in the database.
advances to the next record in the database.
If records are already loaded, it moves to the next loaded record.
Otherwise, it reads the next record from the input.
-It returns the number of attributes in the record, or 0 if no more records.
+It returns the record number starting from 1, or 0 if no more records.
Records are parsed from the input where each record consists of
attribute=value pairs separated by whitespace.
Multi-line records are supported with continuation lines starting with whitespace.
@@ -213,8 +213,8 @@ String s;
sdb_open(&db, "access.log");
sdbr_init(&q);
-sdbr_str2r(&q, "level=error "
- "timestamp=>=2024-01-15T14:00:00");
+sdbr_str2r(&q, Str("level=error "
+ "timestamp=>=2024-01-15T14:00:00"));
while(sdb_query(&db, q)) {
s = sdbr_val(db.r[db.n], "component");
if(sdbr_match("*auth*", s.s))
@@ -236,8 +236,8 @@ String s;
sdb_open(&db, "servers.db");
sdbr_init(&q);
-sdbr_str2r(&q, "type=server !status=maintenance "
- "location=datacenter1");
+sdbr_str2r(&q, Str("type=server !status=maintenance "
+ "location=datacenter1"));
while(sdb_query(&db, q)) {
s = sdbr_val(db.r[db.n], "hostname");
print("Active server: %s\en", s.s);
diff --git a/man/man3/sdbr.3 b/man/man3/sdbr.3
@@ -6,6 +6,8 @@ sdbr_add, sdbr_arg2r, sdbr_attr, sdbr_close, sdbr_dup, sdbr_edit, sdbr_edit2, sd
.br
.B #include <libc.h>
.br
+.B #include <bio.h>
+.br
.B #include <str.h>
.br
.B #include <vec.h>
diff --git a/src/libsdb/sdb_next.c b/src/libsdb/sdb_next.c
@@ -9,11 +9,11 @@ sdb_next(Sdb *db)
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]))
+ for (;Strgets(&db->ln, db->b);) {
+ for (n = 0; n < db->ln.n
+ && isspace(db->ln.s[n]); ++n);
+ if (db->ln.s[n] != '#'
+ && !isspace(db->ln.s[n]))
break;
}
if (!db->ln.n)
@@ -24,13 +24,16 @@ sdb_next(Sdb *db)
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]))
+ if (db->ln.s[0] != '#' && !isspace(db->ln.s[0]))
break;
+ for (n = 0; n < db->ln.n; ++n)
+ if (db->ln.s[n] == '#') {
+ Strdelete(&db->ln, n, db->ln.n);
+ 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
+ return db->n + 1;
+}
diff --git a/src/libsdbr/sdbr_str2r.c b/src/libsdbr/sdbr_str2r.c
@@ -25,7 +25,7 @@ parsetuple(char *p, Sdbr *r)
return p;
}
v = ++p;
- for (q = p; *q && *q != '"' && *q != '\n'; ++q) {
+ for (q = p; *q && *q != '"'; ++q) {
if (*q == '\\')
switch (*(q + 1)) {
case '\\': ++q; break;
@@ -51,13 +51,6 @@ sdbr_str2r(Sdbr *sr, String s)
Strinit(&t);
Strdup(&t, s);
- for (p = t.s; *p; ++p) {
- if (*p != '#')
- continue;
- for (q = p; *q && *q != '\n'; ++q);
- Strdelete(&t, p - t.s, q - p);
- --p;
- }
for (p = t.s; *p; p = parsetuple(p, sr));
Strclose(&t);
}