stew

a monorepo of some sort
Log | Files | Refs

commit 001008da34d04dcdfe1d3e91e8c3d9b6576f1c0c
parent 9c76822a4a94acc7d84416e43051c4f230761b7e
Author: rpa <rpa@laika>
Date:   Sat, 15 Apr 2023 19:59:43 +0000

list: probably some code

Diffstat:
Asrc/list.c | 33+++++++++++++++++++++++++++++++++
Msrc/list/list.c | 90+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
Msrc/list/list.h | 25++++++++++++-------------
Msrc/list/mkfile | 2+-
Msrc/mkfile | 6++++++
5 files changed, 110 insertions(+), 46 deletions(-)

diff --git a/src/list.c b/src/list.c @@ -0,0 +1,33 @@ +#include <u.h> +#include <libc.h> + +#include "list/list.h" + +Node sublist[] = { + {CData, 5, "hello"}, + {CData, 5, "world"}, + {CNull, 0, nil}, +}; + +Node list[] = { + {CData, 5, "hwlst"}, + {CList, 0, sublist}, + {CNull, 0, nil}, +}; + +void +main(void) +{ + int fd = create("/tmp/blk", ORDWR, 0666); + + writelist(fd, list); + + seek(fd, 0, 0); + Node n; + readnodeheader(fd, &n); + seek(fd, n.count, 1); + + Node *node = readlist(fd); + + print("type %uhhd, count %ud\n", node[1].type, node[1].count); +} diff --git a/src/list/list.c b/src/list/list.c @@ -4,47 +4,76 @@ #include "list.h" int -readcell(int fd, Cell *cell) +readnodeheader(int fd, Node *node) { - if (read(fd, &cell->type, sizeof(u8int)) <= 0) return -1; - if (read(fd, &cell->count, sizeof(u32int)) <= 0) return -1; - if ((cell->type == CNull) || (cell->type == CList)) { - cell->count = 0; + char buf[4]; + if (read(fd, buf, sizeof(char) * 4) <= 0) return -1; + node->count = (buf[1] & 0xff) << 16; + node->count |= (buf[2] & 0xff) << 8; + node->count |= (buf[3] & 0xff); + node->type = buf[0]; + + return 0; +} + +int +writenodeheader(int fd, Node *node) +{ + char buf[4]; + buf[0] = node->type; + buf[1] = node->count >> 16; + buf[2] = node->count >> 8; + buf[3] = node->count; + if (write(fd, buf, sizeof(char) * 4) <= 0) return -1; + return 0; +} + +int +readnode(int fd, Node *node) +{ + if (readnodeheader(fd, node) < 0) return -1; + + + if (node->count == 0) { + node->data = nil; return 0; - } - char *buf = malloc(sizeof(cell->count)); - if (read(fd, buf, cell->count) < cell->count) { + }; + char *buf = malloc(sizeof(node->count)); + if (read(fd, buf, node->count) < node->count) { free(buf); return -1; } - cell->data = buf; + node->data = buf; + return 0; } int -writecell(int fd, Cell *cell) +writenode(int fd, Node *node) { - if (write(fd, &cell->type, sizeof(u8int)) <= 0) return -1; - if (write(fd, &cell->count, sizeof(u32int)) <= 0) return -1; - if (write(fd, cell->data, cell->count) < cell->count) return -1; + if (writenodeheader(fd, node) < 0) return -1; + if (node->count == 0) return 0; + if (write(fd, node->data, node->count) < node->count) return -1; return 0; } -Cell * +Node * readlist(int fd) { int i = 0, len = 0; - Cell *list = nil; + Node *list = nil; for (;; i++) { if (i >= len) { len += 256; - list = realloc(list, sizeof(Cell) * len); + list = realloc(list, sizeof(Node) * len); } - if (readcell(fd, list + i) < 0) { + if (readnode(fd, list + i) < 0) { free(list); return nil; } switch (list[i].type) { + default: + break; case CList: list[i].data = readlist(fd); if (list[i].data == nil) { @@ -54,31 +83,28 @@ readlist(int fd) break; case CNull: list[i].count = 0; + list[i].data = nil; return list; } } } int -writelist(int fd, Cell *cell) +writelist(int fd, Node *node) { - int n = 0, r; - for (;; cell++) { - if ((r = write(fd, &cell->type, sizeof(u8int))) <= 0) return -1; - n += r; - if ((r = write(fd, &cell->count, sizeof(u32int))) <= 0) return -1; - n += r; - switch (cell->type) { + Node lstart = {CList, 0, nil}; + writenodeheader(fd, &lstart); + for (;; node++) { + switch (node->type) { + default: + writenode(fd, node); + break; case CList: - writelist(fd, cell->data); + writelist(fd, node->data); break; case CNull: - goto over; - default: - if ((r = write(fd, cell->data, cell->count)) <= 0) return -1; - n += r; + writenode(fd, node); + return 0; } } - over: - return n; } diff --git a/src/list/list.h b/src/list/list.h @@ -1,21 +1,20 @@ enum { - CNull = 0, - CList, - CString, - - CMax, - - CGarbage = 0xff, + CTrsh = 0, + CNull = ')', + CList = '(', + CData = 'd', }; -typedef struct Cell Cell; -struct Cell { +typedef struct Node Node; +struct Node { u8int type; u32int count; void *data; }; -int readcell(int, Cell *); -int writecell(int, Cell *); -Cell * readlist(int); -int writelist(int, Cell *); +int readnode(int, Node *); +int readnodeheader(int, Node *); +int writenode(int, Node *); +int writenodeheader(int, Node *); +Node * readlist(int); +int writelist(int, Node *); diff --git a/src/list/mkfile b/src/list/mkfile @@ -1,7 +1,7 @@ </$objtype/mkfile LIB=liblist.a$O -OFILES=liblist.$O +OFILES=list.$O HFILES=list.h </sys/src/cmd/mklib diff --git a/src/mkfile b/src/mkfile @@ -3,3 +3,9 @@ TARG=shuf poke HFILES= OFILES= </sys/src/cmd/mkmany + +$O.list: list.$O list/liblist.a$O + +list/liblist.a$O: + cd list + mk