commit d5b15d279a1225471a8886a0eb9c4fe88d32210b
parent 7bfc3db39946bae25edb0765683e33ad946d0c68
Author: glenda <glenda@9front.local>
Date: Mon, 21 Sep 2020 13:16:24 +0000
fixes for table_read, table_put and so on down the stack
Diffstat:
5 files changed, 65 insertions(+), 19 deletions(-)
diff --git a/README b/README
@@ -1 +1 @@
-"spreadsheets for plan9"
+spreadsheets for plan9
diff --git a/cells.c b/cells.c
@@ -23,6 +23,12 @@ addrcmp(Addr addr1, Addr addr2)
/*** VALUES ***/
Value
+copy_value(Value v)
+{
+ return mk_value(v.addr, v.data);
+}
+
+Value
mk_value(Addr addr, char *data)
{
Value v;
@@ -62,15 +68,16 @@ Table
copy_table(Table T)
{
Table N;
- long i, len;
+ long i, n;
N = empty_table();
- for (len = 0; T[len] != nil; len++);
- N = realloc(N, sizeof(Value*) * (len + 1));
- for (i = 0; i < len; i++) {
+ 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] = mk_value(T[i]->addr, T[i]->data);
+ N[i]->addr = T[i]->addr;
+ N[i]->data = strdup(T[i]->data);
}
- N[len] = nil;
+ N[n] = nil;
return N;
}
@@ -91,14 +98,16 @@ table_put(Table T, Value v)
N = copy_table(T);
for (n = 0; N[n] != nil; n++) {
if (addrcmp(N[n]->addr, v.addr) == 0) {
- free_value(*N[n]);
- *N[n] = v;
+ free(N[n]->data);
+ N[n]->addr = v.addr;
+ N[n]->data = v.data;
return N;
}
}
- N = realloc(N, sizeof(Value*) * (n + 1));
+ N = realloc(N, sizeof(Value*) * (n + 2));
N[n] = malloc(sizeof(Value));
- *N[n] = v;
+ N[n]->addr = v.addr;
+ N[n]->data = v.data;
N[n+1] = nil;
return N;
}
@@ -137,6 +146,7 @@ table_read(char *path)
case '\t':
N = table_put(T, mk_value(xy2addr(row, col), buf));
free_table(T);
+ free(T);
T = N;
bufsize = 0;
buf = realloc(buf, 0);
@@ -145,6 +155,7 @@ table_read(char *path)
case '\n':
N = table_put(T, mk_value(xy2addr(row, col), buf));
free_table(T);
+ free(T);
T = N;
bufsize = 0;
buf = realloc(buf, 0);
@@ -158,9 +169,24 @@ table_read(char *path)
bufsize++;
}
}
- N = table_put(T, mk_value(xy2addr(row, col), buf));
- free_table(T);
- T = N;
+ if (buf != nil) {
+ N = table_put(T, mk_value(xy2addr(row, col), buf));
+ free_table(T);
+ free(T);
+ }
realloc(buf, 0);
- return T;
+ close(fd);
+
+ return N;
+}
+
+
+void
+print_table(Table T)
+{
+ long i;
+ 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);
+ }
}
\ No newline at end of file
diff --git a/cells.h b/cells.h
@@ -17,6 +17,7 @@ struct Value {
char *data;
};
+Value copy_value(Value v);
Value mk_value(Addr, char *data);
void free_value(Value);
@@ -31,6 +32,8 @@ Table table_put(Table, Value);
Value table_get(Table, Addr);
Table table_read(char*);
+void print_table(Table);
+
// TODO:
Table table_join(Table, Table);
Table table_transform(Table, void (*func)(void**), void**);
\ No newline at end of file
diff --git a/mkfile b/mkfile
@@ -8,3 +8,7 @@ OFILES=\
BIN=/$objtype/bin
</sys/src/cmd/mkone
+
+test_cells: cells.c test_cells.c
+ $CC cells.c test_cells.c
+ $LD -o test_cells cells.$O test_cells.$O
diff --git a/sss.c b/sss.c
@@ -28,6 +28,7 @@ void draw_cells(void);
void draw_cell(Image*, Rectangle, Image*, Image*, Image*, char*, int);
void draw_rows(void);
void colors(void);
+void runcmd(Mousectl *mc, Keyboardctl *kc);
void
threadmain(int argc, char **argv)
@@ -62,10 +63,12 @@ threadmain(int argc, char **argv)
defcwidth = stringwidth(font, "01234567") + 4;
hp = malloc(sizeof(Hist));
- if (argc == 1) hp->T = table_read(argv[0]);
+ if (argc == 1) {
+ fprint(2, "loading %s\n...", argv[0]);
+ hp->T = table_read(argv[0]);
+ }
else hp->T = empty_table();
if (hp->T == nil) exits("failed to create initial table");
-
clear();
draw_columns();
draw_rows();
@@ -75,6 +78,7 @@ threadmain(int argc, char **argv)
switch (alt(alts)) {
case 0: /* keyboard */
if (kv == 0x7f) threadexitsall(nil);
+ runcmd(mc, kc);
break;
case 1: /* mouse */
break;
@@ -200,4 +204,14 @@ draw_cells(void)
col = 0;
}
}
-}
-\ No newline at end of file
+}
+
+
+void
+runcmd(Mousectl *mc, Keyboardctl *kc)
+{
+ char buf[1024];
+ *buf = 0;
+ enter("cmd: ", buf, 1024, mc, kc, 0);
+
+}