dep/libchdr: Add read_header variants for user-provided file

This commit is contained in:
Stenzek 2023-08-12 14:00:33 +10:00
parent 06a8349162
commit 62c5e3af2f
2 changed files with 31 additions and 19 deletions

View file

@ -398,6 +398,8 @@ CHD_EXPORT const char *chd_error_string(chd_error err);
CHD_EXPORT const chd_header *chd_get_header(chd_file *chd); CHD_EXPORT const chd_header *chd_get_header(chd_file *chd);
/* read CHD header data from file into the pointed struct */ /* read CHD header data from file into the pointed struct */
CHD_EXPORT chd_error chd_read_header_core_file(core_file *file, chd_header *header);
CHD_EXPORT chd_error chd_read_header_file(FILE *file, chd_header *header);
CHD_EXPORT chd_error chd_read_header(const char *filename, chd_header *header); CHD_EXPORT chd_error chd_read_header(const char *filename, chd_header *header);

View file

@ -2009,34 +2009,44 @@ CHD_EXPORT const chd_header *chd_get_header(chd_file *chd)
chd_read_header - read CHD header data chd_read_header - read CHD header data
from file into the pointed struct from file into the pointed struct
-------------------------------------------------*/ -------------------------------------------------*/
CHD_EXPORT chd_error chd_read_header(const char *filename, chd_header *header) CHD_EXPORT chd_error chd_read_header_core_file(core_file* file, chd_header* header)
{ {
chd_error err = CHDERR_NONE;
chd_file chd; chd_file chd;
chd.file = file;
/* punt if NULL */
if (filename == NULL || header == NULL)
EARLY_EXIT(err = CHDERR_INVALID_PARAMETER);
/* open the file */
chd.file = core_stdio_fopen(filename);
if (chd.file == NULL)
EARLY_EXIT(err = CHDERR_FILE_NOT_FOUND);
/* attempt to read the header */ /* attempt to read the header */
err = header_read(&chd, header); const chd_error err = header_read(&chd, header);
if (err != CHDERR_NONE) if (err != CHDERR_NONE)
EARLY_EXIT(err); return err;
/* validate the header */ /* validate the header */
err = header_validate(header); return header_validate(header);
if (err != CHDERR_NONE) }
EARLY_EXIT(err);
cleanup: CHD_EXPORT chd_error chd_read_header_file(FILE *file, chd_header *header)
if (chd.file != NULL) {
core_fclose(chd.file); core_file stream;
stream.argp = file;
stream.fsize = core_stdio_fsize;
stream.fread = core_stdio_fread;
stream.fclose = core_stdio_fclose_nonowner;
stream.fseek = core_stdio_fseek;
return chd_read_header_core_file(&stream, header);
}
CHD_EXPORT chd_error chd_read_header(const char *filename, chd_header *header)
{
if (filename == NULL)
return CHDERR_INVALID_PARAMETER;
core_file* file = core_stdio_fopen(filename);
if (file == NULL)
return CHDERR_FILE_NOT_FOUND;
chd_error err = chd_read_header_core_file(file, header);
core_fclose(file);
return err; return err;
} }