Skip to content

Commit

Permalink
Use vfile instead of stdio for world file IO. (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceLR committed Jan 10, 2021
1 parent 22275e5 commit ef777e7
Show file tree
Hide file tree
Showing 16 changed files with 606 additions and 615 deletions.
49 changes: 23 additions & 26 deletions src/editor/board.c
Expand Up @@ -56,22 +56,21 @@ static int board_magic(const char magic_string[4])
}

void save_board_file(struct world *mzx_world, struct board *cur_board,
char *name)
const char *name)
{
FILE *fp = fopen_unsafe(name, "wb");
vfile *vf = vfopen_unsafe(name, "wb");
struct zip_archive *zp;
boolean success = false;

if(fp)
if(vf)
{
fputc(0xFF, fp);
vfputc(0xFF, vf);

fputc('M', fp);
fputc((MZX_VERSION >> 8) & 0xFF, fp);
fputc(MZX_VERSION & 0xFF, fp);

zp = zip_open_fp_write(fp);
vfputc('M', vf);
vfputc((MZX_VERSION >> 8) & 0xFF, vf);
vfputc(MZX_VERSION & 0xFF, vf);

zp = zip_open_vf_write(vf);
if(zp)
{
if(!save_board(mzx_world, cur_board, zp, 0, MZX_VERSION, 0))
Expand All @@ -80,29 +79,27 @@ void save_board_file(struct world *mzx_world, struct board *cur_board,
zip_close(zp, NULL);
}
else
fclose(fp);
vfclose(vf);
}

if(!success)
error_message(E_WORLD_IO_SAVING, 0, NULL);
}

static struct board *legacy_load_board_allocate_direct(struct world *mzx_world,
FILE *fp, int version)
vfile *vf, int version)
{
struct board *cur_board = cmalloc(sizeof(struct board));
int board_start, board_end;

board_start = ftell(fp);
fseek(fp, 0, SEEK_END);
board_end = ftell(fp);
fseek(fp, board_start, SEEK_SET);
board_start = vftell(vf);
board_end = vfilelength(vf, false);

cur_board->world_version = version;
legacy_load_board_direct(mzx_world, cur_board, fp, (board_end - board_start), 0,
legacy_load_board_direct(mzx_world, cur_board, vf, (board_end - board_start), 0,
version);

if(!fread(cur_board->board_name, 25, 1, fp))
if(!vfread(cur_board->board_name, 25, 1, vf))
cur_board->board_name[0] = 0;

return cur_board;
Expand All @@ -129,24 +126,24 @@ static int find_first_board(struct zip_archive *zp)
return -1;
}

void replace_current_board(struct world *mzx_world, char *name)
void replace_current_board(struct world *mzx_world, const char *name)
{
int current_board_id = mzx_world->current_board_id;
struct board *src_board = mzx_world->current_board;

FILE *fp = fopen_unsafe(name, "rb");
vfile *vf = vfopen_unsafe(name, "rb");
struct zip_archive *zp;

char version_string[4];
int file_version;
int success = 0;

if(fp)
if(vf)
{
if(!fread(version_string, 4, 1, fp))
if(!vfread(version_string, 4, 1, vf))
{
error_message(E_IO_READ, 0, NULL);
fclose(fp);
vfclose(vf);
return;
}

Expand All @@ -158,10 +155,10 @@ void replace_current_board(struct world *mzx_world, char *name)
clear_board(src_board);

src_board =
legacy_load_board_allocate_direct(mzx_world, fp, file_version);
legacy_load_board_allocate_direct(mzx_world, vf, file_version);

success = 1;
fclose(fp);
vfclose(vf);
}
else

Expand All @@ -170,7 +167,7 @@ void replace_current_board(struct world *mzx_world, char *name)
int board_id;

// Regular board or maybe not a board at all.
zp = zip_open_fp_read(fp);
zp = zip_open_vf_read(vf);

// Make sure it's an actual zip.
if(zp)
Expand Down Expand Up @@ -198,7 +195,7 @@ void replace_current_board(struct world *mzx_world, char *name)
else
{
error_message(E_BOARD_FILE_FUTURE_VERSION, file_version, NULL);
fclose(fp);
vfclose(vf);
}

if(success)
Expand Down
4 changes: 2 additions & 2 deletions src/editor/board.h
Expand Up @@ -28,10 +28,10 @@ __M_BEGIN_DECLS

#include "configure.h"

void replace_current_board(struct world *mzx_world, char *name);
void replace_current_board(struct world *mzx_world, const char *name);
struct board *create_blank_board(struct editor_config_info *conf);
struct board *create_buffer_board(int width, int height);
void save_board_file(struct world *mzx_world, struct board *cur_board, char *name);
void save_board_file(struct world *mzx_world, struct board *cur_board, const char *name);
void change_board_size(struct board *src_board, int new_width, int new_height);

__M_END_DECLS
Expand Down
38 changes: 19 additions & 19 deletions src/editor/world.c
Expand Up @@ -194,7 +194,7 @@ static void append_world_refactor_board(struct board *cur_board,
}


static boolean append_world_legacy(struct world *mzx_world, FILE *fp,
static boolean append_world_legacy(struct world *mzx_world, vfile *vf,
int file_version)
{
int i, j;
Expand All @@ -204,28 +204,28 @@ static boolean append_world_legacy(struct world *mzx_world, FILE *fp,
unsigned int *board_offsets;
unsigned int *board_sizes;

fseek(fp, 4234, SEEK_SET);
vfseek(vf, 4234, SEEK_SET);

num_boards = fgetc(fp);
num_boards = vfgetc(vf);

if(num_boards == 0)
{
int sfx_size;
// Sfx skip word size
fseek(fp, 2, SEEK_CUR);
vfseek(vf, 2, SEEK_CUR);

// Skip sfx
for(i = 0; i < NUM_SFX; i++)
{
sfx_size = fgetc(fp);
fseek(fp, sfx_size, SEEK_CUR);
sfx_size = vfgetc(vf);
vfseek(vf, sfx_size, SEEK_CUR);
}
num_boards = fgetc(fp);
num_boards = vfgetc(vf);
}

// Skip the names for now
board_names_pos = ftell(fp);
fseek(fp, num_boards * BOARD_NAME_SIZE, SEEK_CUR);
board_names_pos = vftell(vf);
vfseek(vf, num_boards * BOARD_NAME_SIZE, SEEK_CUR);

if(num_boards + old_num_boards >= MAX_BOARDS)
num_boards = MAX_BOARDS - old_num_boards;
Expand All @@ -241,15 +241,15 @@ static boolean append_world_legacy(struct world *mzx_world, FILE *fp,
board_sizes = cmalloc(sizeof(int) * num_boards);
for(i = 0; i < num_boards; i++)
{
board_sizes[i] = fgetd(fp);
board_offsets[i] = fgetd(fp);
board_sizes[i] = vfgetd(vf);
board_offsets[i] = vfgetd(vf);
}

// Append boards
for(i = 0, j = old_num_boards; j < old_num_boards + num_boards; i++, j++)
{
cur_board =
legacy_load_board_allocate(mzx_world, fp, board_offsets[i], board_sizes[i],
legacy_load_board_allocate(mzx_world, vf, board_offsets[i], board_sizes[i],
false, file_version);

mzx_world->board_list[j] = cur_board;
Expand All @@ -274,7 +274,7 @@ static boolean append_world_legacy(struct world *mzx_world, FILE *fp,
free(board_sizes);

// Go back to where the names are
fseek(fp, board_names_pos, SEEK_SET);
vfseek(vf, board_names_pos, SEEK_SET);
for(i = old_num_boards; i < old_num_boards + num_boards; i++)
{
char ignore[BOARD_NAME_SIZE];
Expand All @@ -284,11 +284,11 @@ static boolean append_world_legacy(struct world *mzx_world, FILE *fp,
if(cur_board)
dest = cur_board->board_name;

if(!fread(dest, BOARD_NAME_SIZE, 1, fp))
if(!vfread(dest, BOARD_NAME_SIZE, 1, vf))
dest[0] = 0;
}

fclose(fp);
vfclose(vf);
return true;
}

Expand Down Expand Up @@ -392,19 +392,19 @@ boolean append_world(struct world *mzx_world, const char *file)
boolean ret = false;

struct zip_archive *zp;
FILE *fp;
vfile *vf;

try_load_world(mzx_world, &zp, &fp, file, false, &file_version, ignore);
try_load_world(mzx_world, &zp, &vf, file, false, &file_version, ignore);

if(zp)
{
ret = append_world_zip(mzx_world, zp, file_version);
}
else

if(fp)
if(vf)
{
ret = append_world_legacy(mzx_world, fp, file_version);
ret = append_world_legacy(mzx_world, vf, file_version);
}

// Remove any null boards
Expand Down
24 changes: 12 additions & 12 deletions src/io/zip.c
Expand Up @@ -2390,28 +2390,28 @@ static struct zip_archive *zip_new_archive(void)
* pointer if this archive is ready for file reading; otherwise, returns
* NULL. On failure, this will also close the file pointer.
*/
struct zip_archive *zip_open_fp_read(FILE *fp)
struct zip_archive *zip_open_vf_read(vfile *vf)
{
if(fp)
if(vf)
{
struct zip_archive *zp = zip_new_archive();
long int file_len;

zp->vf = vfile_init_fp(fp, "rb");
zp->vf = vf;
file_len = vfilelength(zp->vf, false);

if(file_len < 0)
{
zip_error("zip_open_fp_read", ZIP_STAT_ERROR);
fclose(fp);
vfclose(vf);
free(zp);
return NULL;
}

if(file_len > INT_MAX)
{
zip_error("zip_open_fp_read", ZIP_UNSUPPORTED_ZIP64);
fclose(fp);
vfclose(vf);
free(zp);
return NULL;
}
Expand All @@ -2433,23 +2433,23 @@ struct zip_archive *zip_open_fp_read(FILE *fp)

struct zip_archive *zip_open_file_read(const char *file_name)
{
FILE *fp = fopen_unsafe(file_name, "rb");
vfile *vf = vfopen_unsafe(file_name, "rb");

return zip_open_fp_read(fp);
return zip_open_vf_read(vf);
}

/**
* Open a zip archive located in a file for writing. The archive will be in
* raw write mode, for use with zip_write(), until zip_write_file() is called.
* Afterward, the archive will be in file write mode.
*/
struct zip_archive *zip_open_fp_write(FILE *fp)
struct zip_archive *zip_open_vf_write(vfile *vf)
{
if(fp)
if(vf)
{
struct zip_archive *zp = zip_new_archive();

zp->vf = vfile_init_fp(fp, "wb");
zp->vf = vf;

zip_init_for_write(zp, ZIP_DEFAULT_NUM_FILES);

Expand All @@ -2462,9 +2462,9 @@ struct zip_archive *zip_open_fp_write(FILE *fp)

struct zip_archive *zip_open_file_write(const char *file_name)
{
FILE *fp = fopen_unsafe(file_name, "wb");
vfile *vf = vfopen_unsafe(file_name, "wb");

return zip_open_fp_write(fp);
return zip_open_vf_write(vf);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/io/zip.h
Expand Up @@ -292,8 +292,8 @@ UTILS_LIBSPEC enum zip_error zip_write_file(struct zip_archive *zp,
UTILS_LIBSPEC enum zip_error zip_close(struct zip_archive *zp,
size_t *final_length);

UTILS_LIBSPEC struct zip_archive *zip_open_fp_read(FILE *fp);
UTILS_LIBSPEC struct zip_archive *zip_open_fp_write(FILE *fp);
UTILS_LIBSPEC struct zip_archive *zip_open_vf_read(vfile *vf);
UTILS_LIBSPEC struct zip_archive *zip_open_vf_write(vfile *vf);
UTILS_LIBSPEC struct zip_archive *zip_open_file_read(const char *file_name);
UTILS_LIBSPEC struct zip_archive *zip_open_file_write(const char *file_name);
UTILS_LIBSPEC struct zip_archive *zip_open_mem_read(const void *src, size_t len);
Expand Down

0 comments on commit ef777e7

Please sign in to comment.