mirror of
https://github.com/holub/mame
synced 2025-06-22 20:38:50 +03:00
Added new function core_fload() to load a file into an allocated buffer.
Updated src2html, regrep, and chdman tools to use this function where appropriate. In chdman, changed -addmeta to -addmetatext or -addmetabin to explicitly specify the type of data (previous auto-detect was too dangerous).
This commit is contained in:
parent
695bdfd04d
commit
df24d3be59
@ -687,6 +687,47 @@ const void *core_fbuffer(core_file *file)
|
||||
}
|
||||
|
||||
|
||||
/*-------------------------------------------------
|
||||
core_fload - open a file with the specified
|
||||
filename, read it into memory, and return a
|
||||
pointer
|
||||
-------------------------------------------------*/
|
||||
|
||||
file_error core_fload(const char *filename, void **data, UINT32 *length)
|
||||
{
|
||||
core_file *file = NULL;
|
||||
file_error err;
|
||||
UINT64 size;
|
||||
|
||||
/* attempt to open the file */
|
||||
err = core_fopen(filename, OPEN_FLAG_READ, &file);
|
||||
if (err != FILERR_NONE)
|
||||
return err;
|
||||
|
||||
/* get the size */
|
||||
size = core_fsize(file);
|
||||
if ((UINT32)size != size)
|
||||
{
|
||||
core_fclose(file);
|
||||
return FILERR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
/* allocate memory */
|
||||
*data = malloc(size);
|
||||
if (length != NULL)
|
||||
*length = (UINT32)size;
|
||||
|
||||
/* read the data */
|
||||
if (core_fread(file, *data, size) != size)
|
||||
{
|
||||
core_fclose(file);
|
||||
free(*data);
|
||||
return FILERR_FAILURE;
|
||||
}
|
||||
return FILERR_NONE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
FILE WRITE
|
||||
|
@ -99,6 +99,9 @@ char *core_fgets(char *s, int n, core_file *file);
|
||||
/* this function may cause the full file data to be read */
|
||||
const void *core_fbuffer(core_file *file);
|
||||
|
||||
/* open a file with the specified filename, read it into memory, and return a pointer */
|
||||
file_error core_fload(const char *filename, void **data, UINT32 *length);
|
||||
|
||||
|
||||
|
||||
/* ----- file write ----- */
|
||||
|
@ -167,7 +167,8 @@ static int usage(void)
|
||||
printf(" or: chdman -diff parent.chd compare.chd diff.chd\n");
|
||||
printf(" or: chdman -setchs inout.chd cylinders heads sectors\n");
|
||||
printf(" or: chdman -fixavdata inout.chd\n");
|
||||
printf(" or: chdman -addmeta inout.chd tag [index] sourcefile\n");
|
||||
printf(" or: chdman -addmetabin inout.chd tag [index] sourcefile\n");
|
||||
printf(" or: chdman -addmetatext inout.chd tag [index] sourcefile\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -316,30 +317,14 @@ static int do_createhd(int argc, char *argv[], int param)
|
||||
break;
|
||||
if (*scan != 0)
|
||||
{
|
||||
file_error filerr;
|
||||
core_file *ident;
|
||||
|
||||
/* attempt to open the file */
|
||||
filerr = core_fopen(argv[4], OPEN_FLAG_READ, &ident);
|
||||
/* attempt to load the file */
|
||||
file_error filerr = core_fload(argv[4], (void **)&identdata, &identdatasize);
|
||||
if (filerr != FILERR_NONE)
|
||||
{
|
||||
fprintf(stderr, "Error opening ident file '%s'\n", argv[4]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* allocate a buffer to hold the data */
|
||||
identdatasize = core_fsize(ident);
|
||||
identdata = malloc(identdatasize);
|
||||
if (identdata == NULL)
|
||||
{
|
||||
fprintf(stderr, "Out of memory adding ident metadata\n");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* read the data, close the file */
|
||||
core_fread(ident, identdata, identdatasize);
|
||||
core_fclose(ident);
|
||||
|
||||
/* shift the remaining arguments down */
|
||||
if (argc > 5)
|
||||
memmove(&argv[4], &argv[5], (argc - 5) * sizeof(argv[0]));
|
||||
@ -2686,7 +2671,6 @@ static int do_addmeta(int argc, char *argv[], int param)
|
||||
{
|
||||
const char *inoutfile, *srcfile, *tagstring;
|
||||
UINT8 was_readonly = FALSE;
|
||||
core_file *sourcefile = NULL;
|
||||
UINT8 *metadata = NULL;
|
||||
chd_file *chd = NULL;
|
||||
chd_header header;
|
||||
@ -2694,9 +2678,7 @@ static int do_addmeta(int argc, char *argv[], int param)
|
||||
UINT32 metalength;
|
||||
UINT32 metaindex;
|
||||
UINT32 metatag;
|
||||
UINT32 chindex;
|
||||
chd_error err;
|
||||
int istext;
|
||||
|
||||
/* require 5 or 6 args total */
|
||||
if (argc != 5 && argc != 6)
|
||||
@ -2768,7 +2750,7 @@ static int do_addmeta(int argc, char *argv[], int param)
|
||||
}
|
||||
|
||||
/* attempt to open the source file */
|
||||
filerr = core_fopen(srcfile, OPEN_FLAG_READ, &sourcefile);
|
||||
filerr = core_fload(srcfile, (void **)&metadata, &metalength);
|
||||
if (filerr != FILERR_NONE)
|
||||
{
|
||||
fprintf(stderr, "Error opening source file '%s'\n", srcfile);
|
||||
@ -2776,43 +2758,17 @@ static int do_addmeta(int argc, char *argv[], int param)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* allocate memory */
|
||||
metalength = core_fsize(sourcefile);
|
||||
if (metalength == 0 || metalength >= 16 * 1024 * 1024)
|
||||
{
|
||||
fprintf(stderr, "Source file '%s' is either 0-length or too large (must be under 16MB)\n", srcfile);
|
||||
err = CHDERR_INVALID_PARAMETER;
|
||||
goto cleanup;
|
||||
}
|
||||
metadata = malloc(metalength + 1);
|
||||
if (metadata == NULL)
|
||||
{
|
||||
fprintf(stderr, "Out of memory allocating source file buffer\n");
|
||||
err = CHDERR_OUT_OF_MEMORY;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* read in the data */
|
||||
if (core_fread(sourcefile, metadata, metalength) != metalength)
|
||||
{
|
||||
fprintf(stderr, "Error reading source file\n");
|
||||
err = CHDERR_READ_ERROR;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* analyze the data */
|
||||
istext = FALSE;
|
||||
for (chindex = 0; chindex < metalength; chindex++)
|
||||
if (metadata[chindex] != 0x0d && metadata[chindex] != 0x0a && metadata[chindex] != 0x09 &&
|
||||
(metadata[chindex] < ' ' || metadata[chindex] >= 0x7f))
|
||||
break;
|
||||
|
||||
/* if it's text, strip any trailing Ctrl-Z and CR/LF and add a trailing NULL */
|
||||
if (chindex == metalength || (chindex == metalength - 1 && metadata[chindex] == 0x1a))
|
||||
if (param)
|
||||
{
|
||||
istext = TRUE;
|
||||
metalength = chindex + 1;
|
||||
metadata[metalength - 1] = 0;
|
||||
metadata = realloc(metadata, metalength + 1);
|
||||
if (metadata == NULL)
|
||||
{
|
||||
fprintf(stderr, "Out of memory preparing metadata\n");
|
||||
err = CHDERR_OUT_OF_MEMORY;
|
||||
goto cleanup;
|
||||
}
|
||||
metadata[metalength++] = 0;
|
||||
while (metalength > 0 && (metadata[metalength - 2] == 0x0a || metadata[metalength - 2] == 0x0d || metadata[metalength - 2] == 0x1a))
|
||||
metadata[--metalength] = 0;
|
||||
}
|
||||
@ -2839,13 +2795,11 @@ static int do_addmeta(int argc, char *argv[], int param)
|
||||
fprintf(stderr, "Error writing new header to CHD file: %s\n", chd_error_string(err));
|
||||
}
|
||||
if (err == CHDERR_NONE)
|
||||
printf("Metadata added successfully as %s\n", istext ? "text" : "binary");
|
||||
printf("Metadata added successfully as %s\n", param ? "text" : "binary");
|
||||
|
||||
cleanup:
|
||||
if (metadata != NULL)
|
||||
free(metadata);
|
||||
if (sourcefile != NULL)
|
||||
core_fclose(sourcefile);
|
||||
if (chd != NULL)
|
||||
chd_close(chd);
|
||||
if (err != CHDERR_NONE && was_readonly)
|
||||
@ -3208,7 +3162,8 @@ int CLIB_DECL main(int argc, char **argv)
|
||||
{ "-diff", do_diff, 0 },
|
||||
{ "-setchs", do_setchs, 0 },
|
||||
{ "-fixavdata", do_fixavdata, 0 },
|
||||
{ "-addmeta", do_addmeta, 0 },
|
||||
{ "-addmetatext", do_addmeta, TRUE },
|
||||
{ "-addmetabin", do_addmeta, FALSE },
|
||||
};
|
||||
extern char build_version[];
|
||||
int i;
|
||||
|
@ -222,7 +222,8 @@ INLINE int get_unique_index(const summary_file *curfile, int index)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
astring *dirname = NULL, *tempfilename = NULL, *tempheader = NULL, *tempfooter = NULL;
|
||||
core_file *tempfile;
|
||||
UINT32 bufsize;
|
||||
void *buffer;
|
||||
int listnum;
|
||||
int result;
|
||||
|
||||
@ -237,17 +238,10 @@ int main(int argc, char *argv[])
|
||||
list_count = argc - 3;
|
||||
|
||||
/* read the template file into an astring */
|
||||
if (core_fopen(astring_c(tempfilename), OPEN_FLAG_READ, &tempfile) == FILERR_NONE)
|
||||
if (core_fload(astring_c(tempfilename), &buffer, &bufsize) == FILERR_NONE)
|
||||
{
|
||||
UINT64 filesize = core_fsize(tempfile);
|
||||
void *buffer = malloc(filesize);
|
||||
if (buffer != NULL)
|
||||
{
|
||||
core_fread(tempfile, buffer, filesize);
|
||||
tempheader = astring_dupch(buffer, filesize);
|
||||
free(buffer);
|
||||
}
|
||||
core_fclose(tempfile);
|
||||
tempheader = astring_dupch(buffer, bufsize);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
/* verify the template */
|
||||
|
@ -202,8 +202,9 @@ static astring *find_include_file(int srcrootlen, int dstrootlen, const astring
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
astring *srcdir = NULL, *dstdir = NULL, *tempfilename = NULL, *tempheader = NULL, *tempfooter = NULL;
|
||||
core_file *tempfile;
|
||||
int unadorned = 0;
|
||||
UINT32 bufsize;
|
||||
void *buffer;
|
||||
int result;
|
||||
int argnum;
|
||||
|
||||
@ -249,17 +250,10 @@ int main(int argc, char *argv[])
|
||||
goto usage;
|
||||
|
||||
/* read the template file into an astring */
|
||||
if (core_fopen(astring_c(tempfilename), OPEN_FLAG_READ, &tempfile) == FILERR_NONE)
|
||||
if (core_fload(astring_c(tempfilename), &buffer, &bufsize) == FILERR_NONE)
|
||||
{
|
||||
UINT64 filesize = core_fsize(tempfile);
|
||||
void *buffer = malloc(filesize);
|
||||
if (buffer != NULL)
|
||||
{
|
||||
core_fread(tempfile, buffer, filesize);
|
||||
tempheader = astring_dupch(buffer, filesize);
|
||||
free(buffer);
|
||||
}
|
||||
core_fclose(tempfile);
|
||||
tempheader = astring_dupch(buffer, bufsize);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
/* verify the template */
|
||||
|
Loading…
Reference in New Issue
Block a user