commit bb591e0b48c7aecae71d474faae6e69a764e112d
parent 943d145cb581bcc2a5945e779fac2c2ae9bf7155
Author: ssnf <ssnf@ssnf.xyz>
Date: Tue, 28 Jan 2025 18:59:58 +0000
improve file handling
Diffstat:
M | config.def.h | | | 4 | ++-- |
M | sim.c | | | 207 | ++++++++++++++++++++++++++++++++++++++----------------------------------------- |
2 files changed, 102 insertions(+), 109 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -1,8 +1,8 @@
#define STATUS "%lu,%u 0x%02x %s%c -%lu- %u" \
, f->dot.p1 \
, curpos() \
- , f->s->s[f->dot.p1] \
- , f->name->n ? f->name->s : "-unnamed-" \
+ , f->s.s[f->dot.p1] \
+ , f->name.n ? f->name.s : "-unnamed-" \
, ISDIRTY(*f) \
, f - file \
, counter
diff --git a/sim.c b/sim.c
@@ -60,11 +60,11 @@ typedef struct {
} Buffer;
typedef struct {
- String* s;
- String* name;
+ String s;
+ String name;
Address dot;
- short i; /*buffer index*/
- short di; /*disk buffer index*/
+ short i; /*buffer index*/
+ short di; /*disk buffer index*/
} File;
typedef struct {
@@ -104,7 +104,7 @@ static int isword(uchar c);
static void file_close(int arg);
static void file_init(File* f);
static void file_load(File* f);
-static void file_open(int c);
+static void file_open(int arg);
static void file_save(int arg);
static void fr_add(Frame* fr, Address a);
static void fr_calc(void);
@@ -158,11 +158,11 @@ die(char* fmt, ...)
else
fputc('\n', stderr);
for (i = 0; i < FILECOUNT; ++i) {
- if (!file[i].s->n)
+ if (!file[i].s.n)
continue;
- str_adds(file[i].name, ".swap", 5);
+ str_adds(&file[i].name, ".swap", 5);
file_save(i);
- fprintf(stderr, "file saved to %s\n", file[i].name->s);
+ fprintf(stderr, "file saved to %s\n", file[i].name.s);
}
abort();
}
@@ -196,22 +196,22 @@ blind_reader(Frame* fr, Posn p0)
a.p0 = a.p1 = p0;
do {
- for (wx = 0; wx < w.wx && a.p1 < f->s->n;) {
- if (f->s->s[a.p1] == '\n')
+ for (wx = 0; wx < w.wx && a.p1 < f->s.n;) {
+ if (f->s.s[a.p1] == '\n')
break;
- wx += charsiz(f->s->s[a.p1], wx);
+ wx += charsiz(f->s.s[a.p1], wx);
if (wx < w.wx)
++a.p1;
if (wx >= w.wx) {
- if (f->s->s[a.p1] == '\t')
+ if (f->s.s[a.p1] == '\t')
++a.p1;
- if (f->s->s[a.p1 + 1] == '\n')
+ if (f->s.s[a.p1 + 1] == '\n')
++a.p1;
}
}
fr_add(fr, a);
a.p0 = ++a.p1;
- } while (a.p1 <= f->s->n && f->s->s[a.p1 - 1] != '\n');
+ } while (a.p1 <= f->s.n && f->s.s[a.p1 - 1] != '\n');
}
static void
@@ -227,7 +227,7 @@ blind_writer(ushort line, ushort offset, ushort top, ushort bot)
else if (fr->n)
o = fr->n - 1;
printf(CSI "%uH", offset > line ? 0 : line - offset);
- fwrite(&f->s->s[fr->a[i].p0], fr->a[o].p1 - fr->a[i].p0
+ fwrite(&f->s.s[fr->a[i].p0], fr->a[o].p1 - fr->a[i].p0
, 1, stdout
);
}
@@ -273,8 +273,8 @@ change(int arg)
++f->dot.p1;
str_init(&s);
if (f->dot.p0 != f->dot.p1) {
- str_adds(&s, f->s->s + f->dot.p0, f->dot.p1 - f->dot.p0);
- str_delete(f->s, f->dot.p0, f->dot.p1);
+ str_adds(&s, f->s.s + f->dot.p0, f->dot.p1 - f->dot.p0);
+ str_delete(&f->s, f->dot.p0, f->dot.p1);
}
f->dot.p0 = f->dot.p1 = f->dot.p0;
fr_zero(fr);
@@ -313,7 +313,7 @@ curpos(void)
wx = 0;
for (i = fr->a[fr->cur].p0; i < f->dot.p1; ++i)
- wx += charsiz(f->s->s[i], wx);
+ wx += charsiz(f->s.s[i], wx);
return wx;
}
@@ -323,7 +323,7 @@ delete(int arg)
String s;
uint count;
- if (!f->s->n)
+ if (!f->s.n)
return;
if (!arg) {
switch (arg = fgetc(stdin)) {
@@ -342,12 +342,12 @@ delete(int arg)
return;
str_init(&s);
str_zero(&istr);
- str_adds(&s, f->s->s + f->dot.p0, f->dot.p1 + 1 - f->dot.p0);
- str_adds(&istr, f->s->s + f->dot.p0, f->dot.p1 + 1 - f->dot.p0);
+ str_adds(&s, f->s.s + f->dot.p0, f->dot.p1 + 1 - f->dot.p0);
+ str_adds(&istr, f->s.s + f->dot.p0, f->dot.p1 + 1 - f->dot.p0);
buf_add(buf, NULL, &s, Delete, arg, count, f->dot.p0);
- str_delete(f->s, f->dot.p0, f->dot.p1 + 1);
+ str_delete(&f->s, f->dot.p0, f->dot.p1 + 1);
str_close(&s);
- if (f->dot.p0 == f->s->n && f->dot.p0)
+ if (f->dot.p0 == f->s.n && f->dot.p0)
--f->dot.p0;
f->dot.p1 = f->dot.p0;
fr_update();
@@ -368,7 +368,7 @@ dot(int arg)
move(EndLine);
else
move(arg);
- str_insert(f->s, &istr, f->dot.p0);
+ str_insert(&f->s, &istr, f->dot.p0);
buf_add(buf, &istr, NULL, Insert, arg, counter, f->dot.p0);
break;
case Delete:
@@ -381,12 +381,12 @@ dot(int arg)
if (arg == Word || arg == Letter || arg > 0x7f)
++f->dot.p1;
str_adds(
- &ds, f->s->s + f->dot.p0, f->dot.p1 - f->dot.p0
+ &ds, f->s.s + f->dot.p0, f->dot.p1 - f->dot.p0
);
- str_delete(f->s, f->dot.p0, f->dot.p1);
+ str_delete(&f->s, f->dot.p0, f->dot.p1);
}
if (buf->is[f->i].n)
- str_insert(f->s, &buf->is[f->i], f->dot.p0);
+ str_insert(&f->s, &buf->is[f->i], f->dot.p0);
buf_add(
buf, &buf->is[f->i], &ds, Change, arg,
buf->count[f->i], f->dot.p0
@@ -405,10 +405,10 @@ redo(int arg)
return;
if (buf->ds[++f->i].n)
str_delete(
- f->s, buf->p0[f->i], buf->p0[f->i] + buf->ds[f->i].n
+ &f->s, buf->p0[f->i], buf->p0[f->i] + buf->ds[f->i].n
);
if (buf->is[f->i].n)
- str_insert(f->s, &buf->is[f->i], buf->p0[f->i]);
+ str_insert(&f->s, &buf->is[f->i], buf->p0[f->i]);
f->dot.p0 = f->dot.p1 = buf->p0[f->i];
fr_zero(fr);
fr_update();
@@ -471,8 +471,8 @@ insert(int arg)
{
String s, c;
- if (f->s->s[f->s->n - 1] != '\n')
- str_addc(f->s, '\n');
+ if (f->s.s[f->s.n - 1] != '\n')
+ str_addc(&f->s, '\n');
str_init(&s), str_init(&c);
str_addc(&c, '\0');
switch (arg) {
@@ -484,7 +484,7 @@ insert(int arg)
case Down:
move(EndLine);
str_addc(&s, '\n');
- str_insert(f->s, &s, f->dot.p0);
+ str_insert(&f->s, &s, f->dot.p0);
++f->dot.p1;
break;
}
@@ -497,13 +497,13 @@ insert(int arg)
case Del:
if (f->dot.p1 != f->dot.p0) {
str_delc(&s);
- str_delete(f->s, f->dot.p1 - 1, f->dot.p1);
+ str_delete(&f->s, f->dot.p1 - 1, f->dot.p1);
--f->dot.p1;
}
break;
default:
str_addc(&s, c.s[0]);
- str_insert(f->s, &c, f->dot.p1);
+ str_insert(&f->s, &c, f->dot.p1);
}
f->dot.p1 = f->dot.p0 + s.n;
}
@@ -512,12 +512,12 @@ insert(int arg)
str_insert(&istr, &s, 0);
for (counter ? --counter : 0; counter; --counter) {
str_insert(&istr, &s, istr.n);
- str_insert(f->s, &s, f->dot.p0);
+ str_insert(&f->s, &s, f->dot.p0);
}
buf_add(buf, &istr, NULL, Insert, arg, counter, f->dot.p0);
str_close(&s), str_close(&c);
f->dot.p0 = f->dot.p1;
- if (f->dot.p1 >= f->s->n)
+ if (f->dot.p1 >= f->s.n)
move(Left);
}
@@ -541,30 +541,30 @@ init(void)
static void
file_close(int arg)
{
- if (arg == -1)
- arg = f - file;
- if (ISDIRTY(file[arg])) {
+ if (arg != -1)
+ f = &file[arg];
+ if (ISDIRTY(*f)) {
printf(
CSI "%uH" EL CSI "31mSave %s?" CSI "0m [y/n]", w.wy/2,
- file[arg].name->n ? file[arg].name->s : "-unnamed-"
+ f->name.n ? f->name.s : "-unnamed-"
);
if (fgetc(stdin) == 'y')
file_save(arg);
}
- str_zero(f->s);
- str_zero(f->name);
- f->dot.p0 = f->dot.p1 = 0;
+ str_close(&f->s);
+ str_close(&f->name);
+ file_init(f);
}
static void
file_init(File* f)
{
- f->s = emalloc(sizeof(String));
- f->name = emalloc(sizeof(String));
- str_init(f->s);
- str_init(f->name);
- f->dot.p0 = f->dot.p1 = 0;
- f->i = f->di = -1;
+ str_init(&f->s);
+ str_init(&f->name);
+ f->dot.p0 = 0;
+ f->dot.p1 = 0;
+ f->i = -1;
+ f->di = -1;
}
static void
@@ -572,13 +572,13 @@ file_load(File* f)
{
FILE* disk;
- if (!(disk = fopen(f->name->s, "r")))
+ if (!(disk = fopen(f->name.s, "r")))
return;
fseek(disk, 0, SEEK_END);
- if ((f->s->n = ftell(disk))) {
- str_insure(f->s, f->s->n);
+ if ((f->s.n = ftell(disk))) {
+ str_insure(&f->s, f->s.n);
rewind(disk);
- fread(f->s->s, f->s->n, 1, disk);
+ fread(f->s.s, f->s.n, 1, disk);
}
fclose(disk);
f->dot.p0 = f->dot.p1 = 0;
@@ -587,17 +587,10 @@ file_load(File* f)
}
static void
-file_open(int c)
+file_open(int arg)
{
- if (ISDIRTY(*f)) {
- printf(
- CSI "%uH" EL CSI "31mSave %s?" CSI "0m [y/n]", w.wy/2,
- f->name->s
- );
- if (fgetc(stdin) == 'y')
- file_save(f - file);
- }
- input(f->name, w.wy/2, CSI "32m$ " CSI "m");
+ file_close(-1);
+ input(&f->name, w.wy/2, CSI "32m$ " CSI "m");
file_load(f);
}
@@ -608,13 +601,13 @@ file_save(int arg)
if (arg == -1)
arg = f - file;
- if (!file[arg].name->n) {
- input(file[arg].name, w.wy/2, "File name: ");
- if (!file[arg].name->n)
+ if (!file[arg].name.n) {
+ input(&file[arg].name, w.wy/2, "File name: ");
+ if (!file[arg].name.n)
return;
}
- disk = fopen(file[arg].name->s, "w");
- fwrite(file[arg].s->s, file[arg].s->n, 1, disk);
+ disk = fopen(file[arg].name.s, "w");
+ fwrite(file[arg].s.s, file[arg].s.n, 1, disk);
fclose(disk);
file[arg].di = file[arg].i;
}
@@ -642,13 +635,13 @@ fr_calc(void)
|| f->dot.p1 > fr->a[fr->n - 1].p1
|| (fr->cur < w.wy && fr->a[0].p0)
|| (fr->cur + w.wy > fr->n
- && fr->a[fr->n - 1].p1 + 1 < f->s->n
+ && fr->a[fr->n - 1].p1 + 1 < f->s.n
)
) {
/*dot + bottom addresses*/
fr_zero(fr);
- for (p0 = f->dot.p1; p0 && f->s->s[p0 - 1] != '\n'; --p0);
- for (;p0 < f->s->n && fr->n < w.wy * 2;) {
+ for (p0 = f->dot.p1; p0 && f->s.s[p0 - 1] != '\n'; --p0);
+ for (;p0 < f->s.n && fr->n < w.wy * 2;) {
blind_reader(fr, p0);
p0 = fr->a[fr->n - 1].p1 + 1;
}
@@ -658,7 +651,7 @@ fr_calc(void)
; fr->cur += fr0.n
) {
for (p0 = fr->a[0].p0 - 1
- ; p0 && f->s->s[p0 - 1] != '\n'
+ ; p0 && f->s.s[p0 - 1] != '\n'
; --p0
);
blind_reader(&fr0, p0);
@@ -712,7 +705,7 @@ fr_update(void)
uint half;
half = w.wy >> 1;
- if (!f->s->n) {
+ if (!f->s.n) {
fr->a[0].p0 = fr->a[0].p1 = 0;
printf(ED);
goto status;
@@ -724,7 +717,7 @@ fr_update(void)
printf(CSI "%uH", w.wy);
printf(STATUS);
for (p0 = fr->a[fr->cur].p0, p1 = 1; p0 < f->dot.p1; ++p0) {
- if (f->s->s[p0] == '\t' && p1 % w.t)
+ if (f->s.s[p0] == '\t' && p1 % w.t)
p1 += (w.t + 1) - (p1 % w.t);
else
++p1;
@@ -749,7 +742,7 @@ move(int arg)
--f->dot.p1;
break;
case Right:
- if (f->dot.p1 + 1 < f->s->n)
+ if (f->dot.p1 + 1 < f->s.n)
++f->dot.p1;
break;
case Up:
@@ -773,13 +766,13 @@ move(int arg)
f->dot.p1 = fr->a[fr->cur - w.wy/2].p0;
break;
case HalfDown:
- if (!f->s->n)
+ if (!f->s.n)
break;
if (fr->n - fr->cur <= w.wy/2) {
- if (fr->a[fr->n - 1].p1 <= f->s->n)
+ if (fr->a[fr->n - 1].p1 <= f->s.n)
f->dot.p1 = fr->a[fr->n - 1].p0;
else
- f->dot.p1 = f->s->n;
+ f->dot.p1 = f->s.n;
} else
f->dot.p1 = fr->a[fr->cur + w.wy/2].p0;
break;
@@ -787,46 +780,46 @@ move(int arg)
f->dot.p1 = 0;
break;
case Bottom:
- if (f->s->n)
- f->dot.p1 = f->s->n - 1;
+ if (f->s.n)
+ f->dot.p1 = f->s.n - 1;
break;
case StartLine:
if (fr->cur)
for (;fr->cur > 1
- && f->s->s[fr->a[fr->cur].p0 - 1] != '\n'
+ && f->s.s[fr->a[fr->cur].p0 - 1] != '\n'
; --fr->cur
);
f->dot.p1 = fr->a[fr->cur].p0;
break;
case EndLine:
- for (;f->dot.p1 + 1 < f->s->n && f->s->s[f->dot.p1] != '\n'
+ for (;f->dot.p1 + 1 < f->s.n && f->s.s[f->dot.p1] != '\n'
; ++f->dot.p1
);
break;
case Word:
- for (;f->dot.p1 + 1 < f->s->n && isword(f->s->s[f->dot.p1])
+ for (;f->dot.p1 + 1 < f->s.n && isword(f->s.s[f->dot.p1])
; ++f->dot.p1
);
- for (;f->dot.p1 + 1 < f->s->n && !isword(f->s->s[f->dot.p1])
+ for (;f->dot.p1 + 1 < f->s.n && !isword(f->s.s[f->dot.p1])
; ++f->dot.p1
);
break;
case EndWord:
move(Right);
- for (;f->dot.p1 < f->s->n && !isword(f->s->s[f->dot.p1])
+ for (;f->dot.p1 < f->s.n && !isword(f->s.s[f->dot.p1])
; ++f->dot.p1
);
- for (;f->dot.p1 < f->s->n && isword(f->s->s[f->dot.p1])
+ for (;f->dot.p1 < f->s.n && isword(f->s.s[f->dot.p1])
; ++f->dot.p1
);
move(Left);
break;
case PrevWord:
move(Left);
- for (;f->dot.p1 > 0 && !isword(f->s->s[f->dot.p1])
+ for (;f->dot.p1 > 0 && !isword(f->s.s[f->dot.p1])
; --f->dot.p1
);
- for (;f->dot.p1 > 0 && isword(f->s->s[f->dot.p1])
+ for (;f->dot.p1 > 0 && isword(f->s.s[f->dot.p1])
; --f->dot.p1
);
if (f->dot.p1)
@@ -866,7 +859,7 @@ gmove(int arg)
static void
paste(int arg)
{
- str_insert(f->s, &istr, f->dot.p0);
+ str_insert(&f->s, &istr, f->dot.p0);
buf_add(buf, &istr, NULL, Insert, 0, 1, f->dot.p0);
}
@@ -876,10 +869,10 @@ pline(int arg)
ulong i, j, c, l;
for (l = f->dot.p1 = 0; f->dot.p1 < f->dot.p0; ++l) {
- for (;f->dot.p1 + 1 < f->s->n && f->s->s[f->dot.p1] != '\n';
+ for (;f->dot.p1 + 1 < f->s.n && f->s.s[f->dot.p1] != '\n';
++f->dot.p1
);
- if (f->dot.p1 + 1 < f->s->n)
+ if (f->dot.p1 + 1 < f->s.n)
++f->dot.p1;
}
++l;
@@ -925,7 +918,7 @@ search(int arg)
Posn pos;
char* p;
- f->s->s[f->s->n] = 0;
+ f->s.s[f->s.n] = 0;
if (arg == '/' || arg == '?') {
str_zero(&srch);
input(&srch, w.wy, "/");
@@ -935,7 +928,7 @@ search(int arg)
}
if (arg == '/' || arg == 'n') {
move(Right);
- p = strstr(f->s->s + f->dot.p0, srch.s);
+ p = strstr(f->s.s + f->dot.p0, srch.s);
if (p == NULL) {
move(Left);
return;
@@ -946,19 +939,19 @@ search(int arg)
move(Left);
for (;;) {
for (;move(Left), f->dot.p1
- && f->s->s[f->dot.p1] != srch.s[0]
+ && f->s.s[f->dot.p1] != srch.s[0]
;
);
- if (!strncmp(f->s->s + f->dot.p1, srch.s, srch.n))
+ if (!strncmp(f->s.s + f->dot.p1, srch.s, srch.n))
break;
if (!f->dot.p1) {
f->dot.p0 = f->dot.p1 = pos;
return;
}
}
- p = f->s->s + f->dot.p1;
+ p = f->s.s + f->dot.p1;
}
- f->dot.p0 = f->dot.p1 = p - f->s->s;
+ f->dot.p0 = f->dot.p1 = p - f->s.s;
if (srch.s[0] == '\n' && srch.s[1] != '\n')
move(Right);
fr_update();
@@ -989,7 +982,7 @@ selection(int arg)
move(EndLine);
move(Right);
}
- if (f->dot.p1 + 1 < f->s->n)
+ if (f->dot.p1 + 1 < f->s.n)
move(Left);
break;
case Bottom:
@@ -1013,13 +1006,13 @@ selection(int arg)
return 0;
till:
p0 = f->dot.p0;
- for (;counter && f->dot.p1 + 1 < f->s->n; --counter)
+ for (;counter && f->dot.p1 + 1 < f->s.n; --counter)
for (++f->dot.p1
- ; f->dot.p1 + 1 < f->s->n
- && f->s->s[f->dot.p1 + 1] != arg
+ ; f->dot.p1 + 1 < f->s.n
+ && f->s.s[f->dot.p1 + 1] != arg
; ++f->dot.p1
);
- if (f->s->s[f->dot.p1 + 1] != arg) {
+ if (f->s.s[f->dot.p1 + 1] != arg) {
f->dot.p1 = f->dot.p0;
return -1;
}
@@ -1115,10 +1108,10 @@ undo(int arg)
return;
if (buf->is[f->i].n)
str_delete(
- f->s, buf->p0[f->i], buf->p0[f->i] + buf->is[f->i].n
+ &f->s, buf->p0[f->i], buf->p0[f->i] + buf->is[f->i].n
);
if (buf->ds[f->i].n)
- str_insert(f->s, &buf->ds[f->i], buf->p0[f->i]);
+ str_insert(&f->s, &buf->ds[f->i], buf->p0[f->i]);
f->dot.p0 = f->dot.p1 = buf->p0[f->i--];
fr_zero(fr);
fr_update();
@@ -1127,7 +1120,7 @@ undo(int arg)
static void
yank(int arg)
{
- if (!f->s->n)
+ if (!f->s.n)
return;
if (!arg) {
switch (arg = fgetc(stdin)) {
@@ -1143,7 +1136,7 @@ yank(int arg)
if ((arg = selection(arg)) < 0)
return;
str_zero(&istr);
- str_adds(&istr, f->s->s + f->dot.p0, f->dot.p1 + 1 - f->dot.p0);
+ str_adds(&istr, f->s.s + f->dot.p0, f->dot.p1 + 1 - f->dot.p0);
}
int
@@ -1154,7 +1147,7 @@ main(int argc, char* argv[])
init();
if (argv[1]) {
- str_adds(f->name, argv[1], strlen(argv[1]));
+ str_adds(&f->name, argv[1], strlen(argv[1]));
file_load(f);
}
for (;;) {