commit 4601dd1827700297b525884f95f444c5ff252d0b
parent fcf6087676b59412bf56b460d3f396a839566e18
Author: rpa <rpa@laika>
Date: Sat, 22 Jul 2023 21:42:59 +0000
src/tablist: encoding is probably done
Diffstat:
3 files changed, 38 insertions(+), 36 deletions(-)
diff --git a/src/tablist/example.tl b/src/tablist/example.tl
@@ -1,14 +1,10 @@
-a
-aa
- b
- c
- d
- cc dd
-name
- \Shinju no Nectar
- \Nectar of Dharani
- \Nectar of Divine Curse
- \神呪のネクタール
-status \ch 31
-tags \setting:fantasy isekai ecchi smut
-type \manga
+Uno
+Duo uno
+Tres
+ uno
+ duo 1
+ tres
+\A a
+ \a a
+ \b a
+ \g a
diff --git a/src/tablist/tablist.c b/src/tablist/tablist.c
@@ -136,7 +136,6 @@ TLencstate _encnode, _encname, _encvalue, _encnext,
#define ENCTOS(X) ( *(TLnode **)slicegetp(X->ns[X->sp], X->cs[X->sp]) )
-// #define ENCTOS(X) ( *(TLnode **)slicegetp(X->ns[0], 0) )
TLencoder *
initTLencoder(Slice *nodes)
@@ -150,10 +149,8 @@ initTLencoder(Slice *nodes)
void *
_encnode(TLencoder *w, char *c)
{
- print("node [%d %d]\n", w->sp, w->cs[w->sp]);
TLnode *n = ENCTOS(w);
assert(n != nil);
- _printnode(n);
w->i = 0;
if (n->name != nil) return _encname(w, c);
if (n->value != nil) return _encvalue(w, c);
@@ -163,7 +160,6 @@ _encnode(TLencoder *w, char *c)
void *
_encname(TLencoder *w, char *c)
{
- print("name\n");
TLnode *n = ENCTOS(w);
if (w->i >= n->name->len) return _encnext(w, c);
char *s = slicegetp(n->name, w->i);
@@ -175,10 +171,14 @@ _encname(TLencoder *w, char *c)
void *
_encvalue(TLencoder *w, char *c)
{
- print("value\n");
+ if (w->i == 0) {
+ *c = '\\';
+ w->i++;
+ return _encvalue;
+ }
TLnode *n = ENCTOS(w);
- if (w->i >= n->value->len) return _encnext(w, c);
- char *s = slicegetp(n->value, w->i);
+ if (w->i >= n->value->len + 1) return _encnext(w, c);
+ char *s = slicegetp(n->value, w->i - 1);
*c = *s;
w->i++;
return _encvalue;
@@ -187,7 +187,6 @@ _encvalue(TLencoder *w, char *c)
void *
_encnext(TLencoder *w, char *c)
{
- print("next\n");
TLnode *n = ENCTOS(w);
if (n->children->len == 0) {
w->cs[w->sp]++;
@@ -198,25 +197,29 @@ _encnext(TLencoder *w, char *c)
w->cs[w->sp] = 0;
return _encsp(w, c);
} else {
- return _encend(w, c);
+ w->sp++;
+ w->ns[w->sp] = n->children;
+ w->cs[w->sp] = 0;
+ return _encnl(w, c);
}
}
void *
_encnl(TLencoder *w, char *c)
{
- print("nl\n");
*c = '\n';
- if (w->ns[w->sp]->len == w->cs[w->sp]) return _encend;
+ while (w->cs[w->sp] == w->ns[w->sp]->len) {
+ w->sp--;
+ if (w->sp < 0) return _encend;
+ w->cs[w->sp]++;
+ }
+ w->i = 0;
return _encind;
-
-
}
void *
_encsp(TLencoder *, char *c)
{
- print("sp\n");
*c = ' ';
return _encnode;
}
@@ -224,14 +227,17 @@ _encsp(TLencoder *, char *c)
void *
_encind(TLencoder *w, char *c)
{
- print("ind\n");
+ if (w->i < w->sp) {
+ *c = '\t';
+ w->i++;
+ return _encind;
+ }
return _encnode(w, c);
}
void *
_encend(TLencoder *, char *c)
{
- print("end\n");
*c = '\0';
return _encend;
}
diff --git a/src/tablist/test.c b/src/tablist/test.c
@@ -26,11 +26,12 @@ printnodes(Slice *nodes, int indent)
void
test(char *path)
{
+ enum { BufSize = 256 };
fprint(2, "%s...\n", path);
- char *buf = malloc(4096);
+ char *buf = malloc(BufSize);
int fd = open(path, OREAD);
if (fd < 0) sysfatal("%r");
- usize n = read(fd, buf, 4096);
+ usize n = read(fd, buf, BufSize);
print("READ %d bytes\n", n);
write(1, buf, n);
TLdecoder *tld = initTLdecoder(path);
@@ -38,12 +39,11 @@ test(char *path)
print("DEBUGPRINT\n");
printnodes(tld->nodes, 0);
-
+ print("ENCODE\n");
TLencoder *tle = initTLencoder(tld->nodes);
- print("WRITE\n");
- n = TLencode(tle, buf, 4096);
+ n = TLencode(tle, buf, BufSize);
+ print("WRITE %d bytes\n", n);
write(1, buf, n);
- print("%d bytes\n", n);
}
void