0
0
Fork 0
mirror of https://git.sr.ht/~rabbits/uxn synced 2024-11-23 14:25:10 +00:00

Renamed prepare for init

This commit is contained in:
Devine Lu Linvega 2021-11-07 10:30:56 -05:00
parent db0efaeff1
commit dee04ff44a
4 changed files with 44 additions and 62 deletions

View file

@ -48,13 +48,6 @@ reset(void)
state = IDLE; state = IDLE;
} }
void
file_prepare(void *filename)
{
reset();
current_filename = (char *)filename;
}
static Uint16 static Uint16
get_entry(char *p, Uint16 len, const char *pathname, const char *basename, int fail_nonzero) get_entry(char *p, Uint16 len, const char *pathname, const char *basename, int fail_nonzero)
{ {
@ -76,6 +69,35 @@ get_entry(char *p, Uint16 len, const char *pathname, const char *basename, int f
return strlen(p); return strlen(p);
} }
static Uint16
file_read_dir(void *dest, Uint16 len)
{
static char pathname[PATH_MAX];
char *p = dest;
if(de == NULL) de = readdir(d);
for(; de != NULL; de = readdir(d)) {
Uint16 n;
if(de->d_name[0] == '.' && de->d_name[1] == '\0')
continue;
strncpy(pathname, current_filename, sizeof(pathname) - 1);
strncat(pathname, "/", sizeof(pathname) - 1);
strncat(pathname, de->d_name, sizeof(pathname) - 1);
n = get_entry(p, len, pathname, de->d_name, 1);
if(!n) break;
p += n;
len -= n;
}
return p - (char *)dest;
}
Uint16
file_init(void *filename)
{
reset();
current_filename = (char *)filename;
return 0;
}
Uint16 Uint16
file_read(void *dest, Uint16 len) file_read(void *dest, Uint16 len)
{ {
@ -88,24 +110,8 @@ file_read(void *dest, Uint16 len)
} }
if(state == FILE_READ) if(state == FILE_READ)
return fread(dest, 1, len, f); return fread(dest, 1, len, f);
if(state == DIR_READ) { if(state == DIR_READ)
static char pathname[PATH_MAX]; return file_read_dir(dest, len);
char *p = dest;
if(de == NULL) de = readdir(d);
for(; de != NULL; de = readdir(d)) {
Uint16 n;
if(de->d_name[0] == '.' && de->d_name[1] == '\0')
continue;
strncpy(pathname, current_filename, sizeof(pathname) - 1);
strncat(pathname, "/", sizeof(pathname) - 1);
strncat(pathname, de->d_name, sizeof(pathname) - 1);
n = get_entry(p, len, pathname, de->d_name, 1);
if(!n) break;
p += n;
len -= n;
}
return p - (char *)dest;
}
return 0; return 0;
} }

View file

@ -10,7 +10,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE. WITH REGARD TO THIS SOFTWARE.
*/ */
void file_prepare(void *filename); Uint16 file_init(void *filename);
Uint16 file_read(void *dest, Uint16 len); Uint16 file_read(void *dest, Uint16 len);
Uint16 file_write(void *src, Uint16 len, Uint8 flags); Uint16 file_write(void *src, Uint16 len, Uint8 flags);
Uint16 file_stat(void *dest, Uint16 len); Uint16 file_stat(void *dest, Uint16 len);

View file

@ -80,24 +80,12 @@ static void
file_deo(Device *d, Uint8 port) file_deo(Device *d, Uint8 port)
{ {
switch(port) { switch(port) {
case 0x1: case 0x1: d->vector = peek16(d->dat, 0x0); break;
d->vector = peek16(d->dat, 0x0); case 0x9: poke16(d->dat, 0x2, file_init(&d->mem[peek16(d->dat, 0x8)])); break;
break; case 0xd: poke16(d->dat, 0x2, file_read(&d->mem[peek16(d->dat, 0xc)], peek16(d->dat, 0xa))); break;
case 0x9: case 0xf: poke16(d->dat, 0x2, file_write(&d->mem[peek16(d->dat, 0xe)], peek16(d->dat, 0xa), d->dat[0x7])); break;
file_prepare(&d->mem[peek16(d->dat, 0x8)]); case 0x5: poke16(d->dat, 0x2, file_stat(&d->mem[peek16(d->dat, 0x4)], peek16(d->dat, 0xa))); break;
break; case 0x6: poke16(d->dat, 0x2, file_delete()); break;
case 0xd:
poke16(d->dat, 0x2, file_read(&d->mem[peek16(d->dat, 0xc)], peek16(d->dat, 0xa)));
break;
case 0xf:
poke16(d->dat, 0x2, file_write(&d->mem[peek16(d->dat, 0xe)], peek16(d->dat, 0xa), d->dat[0x7]));
break;
case 0x5:
poke16(d->dat, 0x2, file_stat(&d->mem[peek16(d->dat, 0x4)], peek16(d->dat, 0xa)));
break;
case 0x6:
poke16(d->dat, 0x2, file_delete());
break;
} }
} }

View file

@ -372,24 +372,12 @@ static void
file_deo(Device *d, Uint8 port) file_deo(Device *d, Uint8 port)
{ {
switch(port) { switch(port) {
case 0x1: case 0x1: d->vector = peek16(d->dat, 0x0); break;
d->vector = peek16(d->dat, 0x0); case 0x9: poke16(d->dat, 0x2, file_init(&d->mem[peek16(d->dat, 0x8)])); break;
break; case 0xd: poke16(d->dat, 0x2, file_read(&d->mem[peek16(d->dat, 0xc)], peek16(d->dat, 0xa))); break;
case 0x9: case 0xf: poke16(d->dat, 0x2, file_write(&d->mem[peek16(d->dat, 0xe)], peek16(d->dat, 0xa), d->dat[0x7])); break;
file_prepare(&d->mem[peek16(d->dat, 0x8)]); case 0x5: poke16(d->dat, 0x2, file_stat(&d->mem[peek16(d->dat, 0x4)], peek16(d->dat, 0xa))); break;
break; case 0x6: poke16(d->dat, 0x2, file_delete()); break;
case 0xd:
poke16(d->dat, 0x2, file_read(&d->mem[peek16(d->dat, 0xc)], peek16(d->dat, 0xa)));
break;
case 0xf:
poke16(d->dat, 0x2, file_write(&d->mem[peek16(d->dat, 0xe)], peek16(d->dat, 0xa), d->dat[0x7]));
break;
case 0x5:
poke16(d->dat, 0x2, file_stat(&d->mem[peek16(d->dat, 0x4)], peek16(d->dat, 0xa)));
break;
case 0x6:
poke16(d->dat, 0x2, file_delete());
break;
} }
} }