commit 2845b2e003692489420870511c073cff0e2571a3
parent 9432c6b96ce90ae895c4b99921a64aa238701df4
Author: glenda <glenda@9front.local>
Date: Tue, 29 Sep 2020 12:47:10 +0000
stacks are awesome
Diffstat:
M | cells.c | | | 125 | +++++++++++++++++++++++++++++++++++-------------------------------------------- |
M | cells.h | | | 23 | +++++++++++------------ |
M | sss.c | | | 31 | ++++++++++++------------------- |
A | stack.c | | | 28 | ++++++++++++++++++++++++++++ |
A | stack.h | | | 10 | ++++++++++ |
5 files changed, 117 insertions(+), 100 deletions(-)
diff --git a/cells.c b/cells.c
@@ -1,6 +1,7 @@
#include <u.h>
#include <libc.h>
+#include "stack.h"
#include "cells.h"
/*** ADDRESS ***/
@@ -46,92 +47,78 @@ free_value(Value v)
/*** TABLE ***/
void
-free_table(Table T)
+free_table(Stack *T)
{
- while (*T != nil) {
- free_value(**T);
- free(*T);
- T++;
+ Value *v;
+ while(T->size > 0){
+ v = pop(T);
+ free_value(*v);
+ free(v);
}
}
-Table
+Stack*
empty_table(void)
{
- Table T;
- T = malloc(sizeof(Value*));
- T[0] = nil;
+ Stack *T;
+ T = malloc(sizeof(Stack));
+ T->data = nil;
+ T->size = 0;
return T;
}
-Table
-copy_table(Table T)
+Stack*
+copy_table(Stack *T)
{
- Table N;
- long i, n;
+ Stack *N;
+ Value *nv, *v;
+ long i;
N = empty_table();
- for (n = 0; T[n] != nil; n++);
- N = realloc(N, sizeof(Value*) * (n + 1));
- for (i = 0; i < n; i++) {
- N[i] = malloc(sizeof(Value));
- N[i]->addr = T[i]->addr;
- N[i]->data = strdup(T[i]->data);
+ for (i = 0; i < T->size; i++) {
+ v = T->data[i];
+ nv = malloc(sizeof(Value));
+ nv->addr = v->addr;
+ nv->data = strdup(v->data);
+ push(N, nv);
}
- N[n] = nil;
- return N;
-}
-
-Table
-eval_table(Table T)
-{
- // TODO
- Table N;
- N = copy_table(T);
return N;
}
-Table
-table_put(Table T, Value v)
+Stack*
+table_put(Stack *T, Value v)
{
- Table N;
- long n;
+ Stack *N;
+ Value *nv;
N = copy_table(T);
- for (n = 0; N[n] != nil; n++) {
- if (addrcmp(N[n]->addr, v.addr) == 0) {
- free(N[n]->data);
- N[n]->addr = v.addr;
- N[n]->data = v.data;
- return N;
- }
- }
- N = realloc(N, sizeof(Value*) * (n + 2));
- N[n] = malloc(sizeof(Value));
- N[n]->addr = v.addr;
- N[n]->data = v.data;
- N[n+1] = nil;
+ nv = malloc(sizeof(Value));
+ nv->addr = v.addr;
+ nv->data = strdup(v.data);
+ push(N, nv);
return N;
}
Value
-table_get(Table T, Addr addr)
+table_get(Stack *T, Addr addr)
{
- Value **V, v;
- v.addr = addr;
- v.data = strdup("");
- for (V = T; *V != nil; V++) {
- if (addrcmp((*V)->addr, addr) == 0) {
- free(v.data);
- v.data = strdup((*V)->data);
- break;
+ Value *v, r;
+ long i;
+ r.addr = addr;
+ r.data = strdup("");
+ for (i = 0; i < T->size; i++) {
+ v = T->data[i];
+ if (addrcmp(v->addr, addr) == 0) {
+ free(r.data);
+ r.data = strdup(v->data);
}
}
- return v;
+ return r;
}
-Table
+Stack*
table_read(char *path)
{
- Table T, N;
+ Stack *T, *N;
+ Value *v;
char b, *buf;
long bufsize, x, y;
int fd;
@@ -145,19 +132,17 @@ table_read(char *path)
while (read(fd, &b, 1) > 0) {
switch (b){
case '\t':
- N = table_put(T, mk_value(xy2addr(x, y), buf));
- free_table(T);
- free(T);
- T = N;
+ v = malloc(sizeof(Value));
+ *v = mk_value(xy2addr(x, y), buf);
+ push(T, v);
bufsize = 0;
buf = realloc(buf, 0);
x++;
break;
case '\n':
- N = table_put(T, mk_value(xy2addr(x, y), buf));
- free_table(T);
- free(T);
- T = N;
+ v = malloc(sizeof(Value));
+ *v = mk_value(xy2addr(x, y), buf);
+ push(T, v);
bufsize = 0;
buf = realloc(buf, 0);
y++;
@@ -183,11 +168,13 @@ table_read(char *path)
void
-print_table(Table T)
+print_table(Stack *T)
{
long i;
+ Value *v;
print("----\n");
- for (i = 0; T[i] != nil; i++) {
- print("%ld:%ld %s\n", T[i]->addr.x, T[i]->addr.y, T[i]->data);
+ for (i = 0; i < T->size; i++) {
+ v = T->data[i];
+ print("%ld:%ld %s\n", v->addr.x, v->addr.y, v->data);
}
}
diff --git a/cells.h b/cells.h
@@ -22,18 +22,17 @@ Value mk_value(Addr, char *data);
void free_value(Value);
/*** TABLE ***/
-typedef Value** Table;
-void free_table(Table);
-Table empty_table(void);
-Table copy_table(Table);
-Table eval_table(Table);
-Table table_put(Table, Value);
-Value table_get(Table, Addr);
-Table table_read(char*);
+void free_table(Stack*);
+Stack* empty_table(void);
+Stack* copy_table(Stack*);
+Stack* table_put(Stack*, Value);
+Value table_get(Stack*, Addr);
+Stack* table_read(char*);
-void print_table(Table);
+void print_table(Stack*);
// TODO:
-Table table_join(Table, Table);
-Table table_transform(Table, void (*func)(void**), void**);
-\ No newline at end of file
+Stack* eval_table(Stack*);
+Stack* table_join(Stack*, Stack*);
+Stack* table_transform(Stack*, void (*func)(void**), void**);
+\ No newline at end of file
diff --git a/sss.c b/sss.c
@@ -6,16 +6,11 @@
#include <cursor.h>
#include <keyboard.h>
+#include "stack.h"
#include "cells.h"
-typedef struct Hist Hist;
-
-struct Hist {
- Table T;
- Hist *prev;
-};
-
-Hist *hp;
+Stack hp, hr;
+Stack *T;
int defcwidth, defcheight;
@@ -71,10 +66,11 @@ threadmain(int argc, char **argv)
defcheight = font->height + 4;
defcwidth = stringwidth(font, "01234567") + 4;
- hp = malloc(sizeof(Hist));
- if (argc == 1) hp->T = table_read(argv[0]);
- else hp->T = empty_table();
- if (hp->T == nil) exits("failed to create initial table");
+ if (argc == 1) T = table_read(argv[0]);
+ else T = empty_table();
+ push(&hp, T);
+ hp.size = 0;
+ hp.data = nil;
clear();
draw_cells();
draw_columns();
@@ -232,7 +228,7 @@ draw_cells(void)
pt = addpt(pt, view);
while (pt.y < screen->r.max.y){
r = rectaddpt(Rect(0,0,defcwidth, defcheight), pt);
- v = table_get(hp->T, xy2addr(x, y));
+ v = table_get(T, xy2addr(x, y));
text = v.data;
draw_cell(screen, r, Ibg, Iborder, Ifg, text, -1);
pt.x += defcwidth;
@@ -261,9 +257,8 @@ edit_cell(Addr addr, Mousectl *mc, Keyboardctl *kc)
{
Value v;
char prompt[256], buf[1024];
- struct Hist *hn;
snprint(prompt, 256, "%ld,%ld:", addr.x, addr.y);
- v = table_get(hp->T, addr);
+ v = table_get(T, addr);
*buf = 0;
if (v.data != 0) strncat(buf, v.data, 1024);
if (enter(prompt, buf, 1024, mc, kc, 0) < 0) {
@@ -274,8 +269,6 @@ edit_cell(Addr addr, Mousectl *mc, Keyboardctl *kc)
free_value(v);
return;
}
- hn = malloc(sizeof(struct Hist));
- hn->prev = hp;
- hn->T = table_put(hp->T, mk_value(addr, buf));
- hp = hn;
+ T = table_put(T, mk_value(addr, buf));
+ push(&hp, T);
}
diff --git a/stack.c b/stack.c
@@ -0,0 +1,28 @@
+#include <u.h>
+#include <libc.h>
+#include "stack.h"
+
+int
+push(Stack *S, void *v)
+{
+ S->data = realloc(S->data, (S->size + 1) * sizeof(void*));
+ S->data[S->size] = v;
+ S->size++;
+ return 1;
+}
+
+void*
+pop(Stack *S)
+{
+ void *v;
+ S->size--;
+ v = S->data[S->size];
+ S->data = realloc(S->data, S->size * sizeof(void*));
+ return v;
+}
+
+//void
+//purge(Stack *s)
+//{
+// for(;S->size > 0; S->size--)
+//}
diff --git a/stack.h b/stack.h
@@ -0,0 +1,10 @@
+typedef struct Stack Stack;
+struct Stack {
+ void **data;
+ long size;
+};
+
+int push(Stack*, void*);
+void* pop(Stack*);
+
+// void purge(Stack*);