commit 635a95056374e3834ce9c596f9c525d21667f990
parent 2ad37309cdb65969b8c5512ce34c2c14c3f98f07
Author: ssnf <ssnf@ssnf.xyz>
Date: Sun, 3 Aug 2025 02:41:53 +0000
libsdbr: add sdbr_r2str, update documentation
Diffstat:
6 files changed, 47 insertions(+), 6 deletions(-)
diff --git a/include/sdbr.h b/include/sdbr.h
@@ -18,6 +18,7 @@ char sdbr_match(char*, char*);
unsigned long sdbr_n(Sdbr);
void sdbr_print(Sdbr);
int sdbr_query(Sdbr, Sdbr);
+void sdbr_r2str(String*, Sdbr);
void sdbr_str2r(Sdbr*, String);
String sdbr_val(Sdbr, char*);
void sdbr_zero(Sdbr*);
diff --git a/man/man3/INDEX b/man/man3/INDEX
@@ -1184,6 +1184,7 @@ sdbr_match sdbr.3
sdbr_n sdbr.3
sdbr_print sdbr.3
sdbr_query sdbr.3
+sdbr_r2str sdbr.3
sdbr_str2r sdbr.3
sdbr_val sdbr.3
sdbr_zero sdbr.3
@@ -1293,6 +1294,7 @@ nsec time.3
time time.3
udpread udpread.3
udpwrite udpread.3
+Vec vec.3
Vecadd vec.3
Vecaddv vec.3
Vecclose vec.3
diff --git a/man/man3/sdbr.3 b/man/man3/sdbr.3
@@ -1,6 +1,6 @@
.TH SDBR 3
.SH NAME
-sdbr_add, sdbr_arg2r, sdbr_attr, sdbr_close, sdbr_dup, sdbr_edit, sdbr_edit2, sdbr_escape, sdbr_init, sdbr_match, sdbr_n, sdbr_print, sdbr_query, sdbr_str2r, sdbr_val, sdbr_zero, vsdbr_join \- simple database records
+sdbr_add, sdbr_arg2r, sdbr_attr, sdbr_close, sdbr_dup, sdbr_edit, sdbr_edit2, sdbr_escape, sdbr_init, sdbr_match, sdbr_n, sdbr_print, sdbr_query, sdbr_r2str, sdbr_str2r, sdbr_val, sdbr_zero, vsdbr_join \- simple database records
.SH SYNOPSIS
.B #include <u.h>
.br
@@ -62,6 +62,9 @@ void sdbr_print(Sdbr r)
int sdbr_query(Sdbr r, Sdbr q)
.PP
.B
+void sdbr_r2str(String *s, Sdbr r)
+.PP
+.B
void sdbr_str2r(Sdbr *sr, String s)
.PP
.B
@@ -114,6 +117,16 @@ copies all attribute-value pairs from record
to record
.IR dst .
.PP
+.I Sdbr_r2str
+formats record
+.I r
+as a string and stores it in string
+.IR s .
+Each attribute-value pair is formatted as attribute=value,
+with values quoted and escaped as needed using
+.IR sdbr_escape .
+Pairs are separated by tabs and terminated with a newline.
+.PP
.I Sdbr_str2r
parses a string
.I s
@@ -121,7 +134,7 @@ containing space-separated attribute=value pairs and adds them to record
.IR r .
The input string is not modified.
Values containing spaces can be quoted with double quotes.
-Escape sequences \e0, \ef, \en, \er, \et, \ev, \e", and \e\e are supported within quoted values.
+When values are quoted, the following characters are escaped: \e0, \ef, \en, \er, \et, \ev, \e", and \e\e.
.PP
.I Sdbr_arg2r
parses command-line arguments
@@ -341,5 +354,12 @@ sdbr_close(&q);
Functions call
.I sysfatal
if memory allocation fails.
+.I Sdbr_print ,
+.I sdbr_r2str ,
+and
+.I vsdbr_join
+call
+.I sysfatal
+if passed an empty record.
.SH BUGS
None.
diff --git a/src/libsdbr/mkfile b/src/libsdbr/mkfile
@@ -13,6 +13,7 @@ OFILES=\
sdbr_match.$O\
sdbr_print.$O\
sdbr_query.$O\
+ sdbr_r2str.$O\
sdbr_str2r.$O\
sdbr_val.$O\
sdbr_zero.$O\
diff --git a/src/libsdbr/sdbr_print.c b/src/libsdbr/sdbr_print.c
@@ -9,10 +9,8 @@ sdbr_print(Sdbr r)
if (!Vecsiz(r.attr))
sysfatal("sdbr_print: empty record");
Strinit(&s);
- print("%s=%s", r.attr[0].s, sdbr_escape(&s, r.val[0]));
- for (i = 1; i < Vecsiz(r.attr); ++i)
- print(" %s=%s", r.attr[i].s, sdbr_escape(&s, r.val[i]));
+ sdbr_r2str(&s, r);
+ write(1, s.s, s.n);
Strclose(&s);
- write(1, "\n", 1);
}
diff --git a/src/libsdbr/sdbr_r2str.c b/src/libsdbr/sdbr_r2str.c
@@ -0,0 +1,19 @@
+#include "std.h"
+
+void
+sdbr_r2str(String *s, Sdbr r)
+{
+ String t;
+ ulong i;
+
+ if (!Vecsiz(r.attr))
+ sysfatal("sdbr_r2str: empty record");
+ Strinit(&t);
+ Strzero(s);
+ Strprint(s, "%s=%s", r.attr[0].s, sdbr_escape(&t, r.val[0]));
+ for (i = 1; i < Vecsiz(r.attr); ++i)
+ Strprint(s, "\t%s=%s", r.attr[i].s
+ , sdbr_escape(&t, r.val[i]));
+ Straddc(s, '\n');
+ Strclose(&t);
+}