Added an 'expected_type' argument
This commit is contained in:
parent
f0e591d595
commit
8933be944c
|
@ -36,6 +36,9 @@ typedef enum {
|
|||
/* could not seek in stream */
|
||||
g1m_error_noseek,
|
||||
|
||||
/* wrong type */
|
||||
g1m_error_wrong_type,
|
||||
|
||||
/* magic or control problem */
|
||||
g1m_error_magic,
|
||||
/* unexpected EOF */
|
||||
|
@ -222,8 +225,10 @@ typedef struct {
|
|||
/* Main functions */
|
||||
/* ************************************************************************** */
|
||||
/* open handles */
|
||||
int g1m_open(g1m_t **handle, const char *path);
|
||||
int g1m_fopen(g1m_t **handle, const char *path, FILE *stream);
|
||||
int g1m_open(g1m_t **handle, const char *path,
|
||||
g1m_type_t expected_type);
|
||||
int g1m_fopen(g1m_t **handle, const char *path, FILE *stream,
|
||||
g1m_type_t expected_type);
|
||||
|
||||
/* open MCS handle (this one is mainly for `libp7`) */
|
||||
int g1m_parse_mcsfile_content(g1m_mcsfile_t **handle, FILE *stream,
|
||||
|
|
|
@ -123,7 +123,8 @@ G1M_PROTOTYPE_PIX(16bits)
|
|||
/* Utilities */
|
||||
/* ************************************************************************** */
|
||||
/* Parsing */
|
||||
int g1m_parse(g1m_t *handle, const char *path, FILE *stream);
|
||||
int g1m_parse(g1m_t *handle, const char *path, FILE *stream,
|
||||
g1m_type_t expected_type);
|
||||
|
||||
/* Free-ing */
|
||||
void g1m_free_content(g1m_t *handle);
|
||||
|
|
|
@ -29,6 +29,9 @@ const char *g1m_error_strings[] = {
|
|||
[g1m_error_noseek] =
|
||||
"given stream was not seekable",
|
||||
|
||||
[g1m_error_wrong_type] =
|
||||
"was not of the expected type",
|
||||
|
||||
[g1m_error_magic] =
|
||||
"is probably not a file libg1m can parse, or is corrupted",
|
||||
[g1m_error_eof] =
|
||||
|
|
|
@ -129,13 +129,15 @@ static int find_parse_function(g1m_t *handle, const char *path,
|
|||
* Read the standard header, correct endianness, check magic numbers,
|
||||
* then read subparts according to the G1M type.
|
||||
*
|
||||
* @arg handle the handle.
|
||||
* @arg path the file path.
|
||||
* @arg stream the stream to parse from.
|
||||
* @return the error code (0 if ok).
|
||||
* @arg handle the handle.
|
||||
* @arg path the file path.
|
||||
* @arg stream the stream to parse from.
|
||||
* @arg expected_type the expected type.
|
||||
* @return the error code (0 if ok).
|
||||
*/
|
||||
|
||||
int g1m_parse(g1m_t *handle, const char *path, FILE *stream)
|
||||
int g1m_parse(g1m_t *handle, const char *path, FILE *stream,
|
||||
g1m_type_t expected_type)
|
||||
{
|
||||
/* initialize the handle */
|
||||
bzero(handle, sizeof(g1m_t));
|
||||
|
@ -160,6 +162,10 @@ int g1m_parse(g1m_t *handle, const char *path, FILE *stream)
|
|||
if ((err = find_parse_function(handle, path, &hd, &read_func)))
|
||||
return (err);
|
||||
|
||||
/* check if the type is alright */
|
||||
if (expected_type && handle->type != expected_type)
|
||||
return (g1m_error_wrong_type);
|
||||
|
||||
/* log some data */
|
||||
log_info("Standard Header filesize is %" PRIu32 "o.", hd.filesize);
|
||||
log_info("Standard Header num is %" PRIu16 ".", hd.number);
|
||||
|
|
|
@ -17,16 +17,18 @@
|
|||
*
|
||||
* @arg handle the handle to create.
|
||||
* @arg path the path of the file to open.
|
||||
* @arg expected the expected type - 0 if none
|
||||
* @return the error code (0 if ok).
|
||||
*/
|
||||
|
||||
int g1m_open(g1m_t **handle, const char *path)
|
||||
int g1m_open(g1m_t **handle, const char *path,
|
||||
g1m_type_t expected)
|
||||
{
|
||||
/* open stream */
|
||||
FILE *f = fopen(path, "r");
|
||||
|
||||
/* open using `g1m_fopen` */
|
||||
int err = g1m_fopen(handle, path, f);
|
||||
int err = g1m_fopen(handle, path, f, expected);
|
||||
|
||||
/* close opened stream and return error code (or 0 if ok) */
|
||||
if (f) fclose(f);
|
||||
|
@ -43,10 +45,12 @@ int g1m_open(g1m_t **handle, const char *path)
|
|||
* @arg handle the handle to create.
|
||||
* @arg path the file path.
|
||||
* @arg stream the stream.
|
||||
* @arg expected the expected type.
|
||||
* @return the error code (0 if ok)
|
||||
*/
|
||||
|
||||
int g1m_fopen(g1m_t **handle, const char *path, FILE *stream)
|
||||
int g1m_fopen(g1m_t **handle, const char *path, FILE *stream,
|
||||
g1m_type_t expected)
|
||||
{
|
||||
int err;
|
||||
|
||||
|
@ -59,7 +63,7 @@ int g1m_fopen(g1m_t **handle, const char *path, FILE *stream)
|
|||
if (!*handle) return (g1m_error_alloc);
|
||||
|
||||
/* fill it by parsing opened file */
|
||||
if ((err = g1m_parse(*handle, path, stream))) {
|
||||
if ((err = g1m_parse(*handle, path, stream, expected))) {
|
||||
free(*handle);
|
||||
*handle = NULL;
|
||||
return (err);
|
||||
|
|
Reference in New Issue