commit 1196031726c313b332d2b17e004343ea4ad776cc
parent fc9b5b0b104b04b8f8203eac0bfcc49857adda0d
Author: Renev Pavel <an2qzavok@gmail.com>
Date: Sat, 22 Apr 2023 19:02:29 +0000
tablist: basic parsing almost done
Diffstat:
2 files changed, 53 insertions(+), 12 deletions(-)
diff --git a/src/tablist/tablist.c b/src/tablist/tablist.c
@@ -5,39 +5,78 @@
struct TLrow {
int indent;
+ int n;
TLNode *nodes;
};
-int
-_tlparseline(char *s, char *e)
+struct TLrow
+_tlparseline(char *s)
{
- struct TLrow row;
- row.ident = 0;
- row.nodes = nil;
+ char *p;
+ struct TLrow row = {0, 0, nil};
while (*s == '\t') {
- row.ident++;
+ row.indent++;
s++;
};
- return 0;
+
+ fprint(2, "indent %d\n", row.indent);
+
+ if (*s == '\0') return 0;
+
+ for (;;) {
+ if (*s == '\\') {
+ s++;
+ row.nodes = realloc(row.nodes, (row.n + 1) * sizeof(TLNode));
+ memset(&row.nodes[row.n], 0, sizeof(TLNode));
+ char *v = strdup(s);
+ row.nodes[row.n].value = v;
+ row.n++;
+ break;
+ }
+ p = strchr(s, ' ');
+ if (p == s) {
+ sysfatal("tlparseline: unexpected space");
+ } else if (p == nil) {
+ row.nodes = realloc(row.nodes, (row.n + 1) * sizeof(TLNode));
+ memset(&row.nodes[row.n], 0, sizeof(TLNode));
+ char *n = strdup(s);
+ row.nodes[row.n].name = n;
+ row.n++;
+ break;
+ } else {
+ *p = '\0';
+ row.nodes = realloc(row.nodes, (row.n + 1) * sizeof(TLNode));
+ memset(&row.nodes[row.n], 0, sizeof(TLNode));
+ char *n = strdup(s);
+ row.nodes[row.n].name = n;
+ row.n++;
+ s = p + 1;
+ }
+ }
+ return row;
}
int
_tlparse(char *data, usize count)
{
int rown = 0;
- struct TLrow *rows = nil;
+ struct TLrow row;
char *e, *p = data;
- while (p < data + p) {
+ while (p < (data + count)) {
+
+ fprint(2, "<%d>\n", rown);
+
e = memchr(p, '\n', count - (p - data));
if (e == nil) {
werrstr("newline not found");
return -1;
}
if (p != e) {
- _tlparseline(p, e);
+ *e = '\0';
+ row = _tlparseline(p);
}
p = e + 1;
rown++;
}
+ return 0;
}
-
diff --git a/src/tablist/tablist.h b/src/tablist/tablist.h
@@ -1,7 +1,9 @@
-#pragma lib "../tablist/tablist.h"
+#pragma lib "../tablist/libtablist.a$O"
typedef struct TLNode TLNode;
struct TLNode {
char *name;
char *value;
TLNode *children;
};
+
+int _tlparse(char *, usize);