ref: 27925e85e49baf91a5d7db61072913e781b85844
parent: 2c00193c59135aa88feda4fa3642662aa951f9a1
author: sirjofri <sirjofri@sirjofri.de>
date: Sun Dec 21 10:51:30 EST 2025
fix Q generation with nested elements.
This now allows for /multi/{path}/{one}/{two} nested elements.
--- a/code.c
+++ b/code.c
@@ -6,6 +6,7 @@
typedef struct Vqid Vqid;
struct Vqid {char *name;
+ char *part;
VFile *vfile;
Vqid *children;
Vqid *next;
@@ -215,7 +216,7 @@
Vqid *v;
for (v = parent->children; v; v = v->next) {- if (strcmp(v->name, child) == 0)
+ if (strcmp(v->part, child) == 0)
return v;
}
return nil;
@@ -233,6 +234,7 @@
/* root element */
vqids = mallocz(sizeof(Vqid), 1);
vqids->name = "root";
+ vqids->part = vqids->name;
vqids->vfile = getfile("/");vqids->vfile->isdir = 1;
}
@@ -252,6 +254,7 @@
nv = mallocz(sizeof(Vqid), 1);
pathtostring(buf, sizeof(buf), f->path+1);
nv->name = strdup(buf);
+ nv->part = strdup(s);
nv->vfile = f;
nv->next = v->children;
v->children = nv;
--- a/files.c
+++ b/files.c
@@ -95,7 +95,6 @@
if (!verifyfiles(new->parts))
return nil;
-
return new;
}
@@ -118,6 +117,50 @@
for (vf = files; vf; vf = vf->next)
f(vf, aux);
+}
+
+static int
+getnumparts(VFile *f)
+{+ char **a;
+ int numparts = 0;
+
+ for (a = f->parts; *a; a++)
+ numparts++;
+ return numparts;
+}
+
+void
+sortfiles()
+{+ VFile **cached;
+ VFile *v;
+ int n;
+ int level = 0;
+ int i;
+ int numfiles = getnfiles();
+
+ /* sort files by level, from root to highest nesting level */
+
+ cached = mallocz((numfiles+1) * sizeof(VFile*), 1);
+
+ for (i = 0; i < numfiles;) {+ for (v = files; v; v = v->next) {+ n = getnumparts(v) - 1;
+ n = n < 0 ? 0 : n;
+ if (n == level) {+ cached[i++] = v;
+ }
+ }
+ level++;
+ }
+
+ for (i = 0; i < numfiles; i++) {+ cached[i]->next = cached[i+1];
+ }
+
+ files = *cached;
+ free(cached);
}
int
--- a/fns.h
+++ b/fns.h
@@ -2,6 +2,7 @@
VFile *addfile(char *path);
VFile *getfile(char *path);
int getnfiles(void);
+void sortfiles(void);
void foreachfile(void (*f)(VFile*,void*), void*);
void genqids(VFile*,void*);
--- a/main.c
+++ b/main.c
@@ -235,6 +235,12 @@
free(s);
}
+static void
+pfile(VFile *f, void*)
+{+ fprint(2, "file: %s\n", f->path);
+}
+
void
main(int argc, char **argv)
{@@ -264,6 +270,7 @@
process(bin);
Bterm(bin);
+ sortfiles();
foreachfile(genqids, nil);
printqids();
--
⑨