Improved implementation of file_import

This commit is contained in:
Devine Lu Linvega 2024-05-10 08:42:37 -07:00
parent fec9cffcd6
commit 0de5bb473b
1 changed files with 9 additions and 8 deletions

View File

@ -99,23 +99,24 @@ device_read(void)
if(dst_ - origin == 0) dst_--;
}
static void
file_import(char *path)
static char *
file_import(char *path, char *ptr)
{
FILE *f;
int pr = 0;
char c;
if((f = fopen(path, "r"))) {
char c;
while(fread(&c, 1, 1, f)) {
c = c <= 0x20 ? 0x20 : c;
if(c == '(') pr++;
if(c == ')') pr--;
*dst_++ = c;
*ptr++ = c;
}
fclose(f);
if(pr) fprintf(stderr, "Modal program imbalanced.\n");
} else
dst_ = copy("NAF", dst_, 3);
return ptr;
}
return copy("NAF", ptr, 3);
}
static void
@ -127,7 +128,7 @@ write_reg(char r, char *reg)
case '_': {
char filepath[0x80];
copy(reg, filepath, walk(reg) - reg);
file_import(filepath);
dst_ = file_import(filepath, dst_);
return;
}
case '^': { /* op: join */
@ -322,7 +323,7 @@ main(int argc, char **argv)
case 'n': /* infinite */ cycles = 0xffffffff; break;
}
}
dst_ = bank_a, file_import(argv[i]), dst_ = bank_b;
file_import(argv[i], src_);
while(rewrite() && ++rw)
if(!cycles--) return !fprintf(stderr, "Modal rewrites exceeded.\n");
if(!quiet) {