commit bba3d9b8e6cb1ebeb35857d1daf4c397e9c00b7b
parent 85babaa28cb492e6900e86c030c30a8abf4aaa1a
Author: ssnf <ssnf@ssnf.xyz>
Date: Mon, 9 Aug 2021 20:54:53 +0000
converted file name to String structure
Diffstat:
4 files changed, 12 insertions(+), 11 deletions(-)
diff --git a/file.c b/file.c
@@ -14,9 +14,9 @@ void
file_init(File* f)
{
f->s = emalloc(sizeof(String));
+ f->name = emalloc(sizeof(String));
str_init(f->s);
- if (f->name)
- file_load(f);
+ str_init(f->name);
f->dot.p0 = f->dot.p1 = 0;
}
@@ -25,7 +25,7 @@ file_load(File* f)
{
FILE* disk;
- if (!(disk = fopen(f->name, "r")))
+ if (!(disk = fopen(f->name->s, "r")))
return;
fseek(disk, 0, SEEK_END);
f->s->n = ftell(disk);
@@ -35,13 +35,12 @@ file_load(File* f)
fclose(disk);
}
-
void
file_save(File* f)
{
FILE* disk;
- disk = fopen(f->name, "w");
+ disk = fopen(f->name->s, "w");
fwrite(f->s->s, f->s->n, 1, disk);
fclose(disk);
}
diff --git a/sim.c b/sim.c
@@ -23,7 +23,7 @@ die(char* fmt, ...)
va_list ap;
win_end();
- f.name = "sim-crash.swap";
+ str_adds(f.name, ".swap", 5);
file_save(&f);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
@@ -92,12 +92,13 @@ main(int argc, char* argv[])
uchar c;
win_init();
- if (argv[1])
- f.name = argv[1];
file_init(&f);
+ if (argv[1]) {
+ str_adds(f.name, argv[1], strlen(argv[1]));
+ file_load(&f);
+ }
for (;;) {
win_query(&w);
-
c = fgetc(stdin);
for (i = 0; i < LENGTH(keys); ++i) {
if (keys[i].key == c) {
diff --git a/sim.h b/sim.h
@@ -26,7 +26,7 @@ struct Address {
struct File {
String* s;
- char* name;
+ String* name;
Address dot;
};
@@ -57,6 +57,7 @@ void file_close(File* f);
void str_init(String* s);
void str_insure(String* s, ulong n);
void str_close(String* s);
+void str_adds(String* p, char* s, ulong n);
void win_init();
void win_end();
void win_query(Window* w);
diff --git a/string.c b/string.c
@@ -53,7 +53,7 @@ str_addc(String* p, int c)
}
void
-strnadds(String* p, char* s, uint n)
+str_adds(String* p, char* s, ulong n)
{
str_insure(p, p->n + n);
memmove(p->s + p->n, s, n);