mirror of
https://git.planet-casio.com/Lephenixnoir/fxsdk.git
synced 2024-12-28 04:23:37 +01:00
fxgxa: use standard fopen() for reading files [Windows]
This commit is contained in:
parent
50997a8b75
commit
b83796a382
1 changed files with 15 additions and 15 deletions
30
fxgxa/file.c
30
fxgxa/file.c
|
@ -19,7 +19,7 @@ static void invert_header(void *gxa)
|
|||
|
||||
#define fail(msg, ...) { \
|
||||
fprintf(stderr, "error: " msg ": %m\n", ##__VA_ARGS__); \
|
||||
close(fd); \
|
||||
fclose(fp); \
|
||||
free(data); \
|
||||
return NULL; \
|
||||
}
|
||||
|
@ -28,31 +28,31 @@ static void invert_header(void *gxa)
|
|||
Allocates a buffer with @prepend leading bytes initialized to zero. */
|
||||
static void *load(const char *filename, size_t *size, int header, int footer)
|
||||
{
|
||||
int fd;
|
||||
struct stat statbuf;
|
||||
FILE *fp;
|
||||
void *data = NULL;
|
||||
size_t filesize;
|
||||
long filesize;
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if(fd < 0) fail("cannot open %s", filename);
|
||||
fp = fopen(filename, "rb");
|
||||
if(!fp) fail("cannot open %s", filename);
|
||||
|
||||
int x = fstat(fd, &statbuf);
|
||||
if(x > 0) fail("cannot stat %s", filename);
|
||||
|
||||
filesize = statbuf.st_size;
|
||||
fseek(fp, 0, SEEK_END);
|
||||
filesize = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
if(ftell(fp) != 0) fail("cannod rewind %s", filename);
|
||||
data = malloc(header + filesize + footer);
|
||||
if(!data) fail("cannot load %s", filename);
|
||||
|
||||
size_t remaining = filesize;
|
||||
long remaining = filesize;
|
||||
while(remaining > 0)
|
||||
{
|
||||
size_t offset = header + filesize - remaining;
|
||||
ssize_t y = read(fd, data + offset, remaining);
|
||||
long offset = header + filesize - remaining;
|
||||
long y = fread(data + offset, 1, remaining, fp);
|
||||
|
||||
if(y < 0) fail("cannot read from %s", filename);
|
||||
if(y <= 0) fail("cannot read from %s at offset %ld",
|
||||
filename, offset);
|
||||
remaining -= y;
|
||||
}
|
||||
close(fd);
|
||||
fclose(fp);
|
||||
|
||||
memset(data, 0, header);
|
||||
memset(data + header + filesize, 0, footer);
|
||||
|
|
Loading…
Reference in a new issue