cleanup: move input file open/close into process_file function

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2019-03-08 15:22:35 +01:00
parent f7e4aec2ea
commit b688a39149
4 changed files with 29 additions and 51 deletions

View File

@ -15,11 +15,7 @@ typedef struct {
int (*handle)(input_file_t *f, void *obj);
} keyword_handler_t;
void cleanup_file(input_file_t *f);
int open_file(input_file_t *f, const char *filename);
int process_file(input_file_t *f, const keyword_handler_t *handlers,
int process_file(const char *filename, const keyword_handler_t *handlers,
size_t count, void *obj);
void input_file_complain(const input_file_t *f, const char *msg);

View File

@ -57,47 +57,37 @@ retry:
return 0;
}
void cleanup_file(input_file_t *f)
{
fclose(f->f);
free(f->line);
}
int open_file(input_file_t *f, const char *filename)
{
memset(f, 0, sizeof(*f));
f->filename = filename;
f->f = fopen(filename, "r");
if (f->f == NULL) {
perror(f->filename);
return -1;
}
return 0;
}
void input_file_complain(const input_file_t *f, const char *msg)
{
fprintf(stderr, "%s: %zu: %s\n", f->filename, f->linenum, msg);
}
int process_file(input_file_t *f, const keyword_handler_t *handlers,
int process_file(const char *filename, const keyword_handler_t *handlers,
size_t count, void *obj)
{
input_file_t f;
size_t i, len;
char *ptr;
int ret;
memset(&f, 0, sizeof(f));
f.filename = filename;
f.f = fopen(filename, "r");
if (f.f == NULL) {
perror(f.filename);
return -1;
}
for (;;) {
ret = prefetch_line(f);
ret = prefetch_line(&f);
if (ret < 0)
return -1;
goto fail;
if (ret > 0)
break;
ptr = f->line;
ptr = f.line;
for (i = 0; i < count; ++i) {
len = strlen(handlers[i].name);
@ -108,19 +98,25 @@ int process_file(input_file_t *f, const keyword_handler_t *handlers,
continue;
for (ptr += len; isspace(*ptr); ++ptr)
;
memmove(f->line, ptr, strlen(ptr) + 1);
memmove(f.line, ptr, strlen(ptr) + 1);
break;
}
if (i == count) {
fprintf(stderr, "%s: %zu: unknown keyword\n",
f->filename, f->linenum);
return -1;
f.filename, f.linenum);
goto fail;
}
if (handlers[i].handle(f, obj))
return -1;
if (handlers[i].handle(&f, obj))
goto fail;
}
fclose(f.f);
free(f.line);
return 0;
fail:
fclose(f.f);
free(f.line);
return -1;
}

View File

@ -97,21 +97,13 @@ static compressor_t *get_default_compressor(void)
int desc_read(const char *path, pkg_desc_t *desc)
{
input_file_t f;
char *ptr;
memset(desc, 0, sizeof(*desc));
if (open_file(&f, path))
if (process_file(path, line_hooks, NUM_LINE_HOOKS, desc))
return -1;
if (process_file(&f, line_hooks, NUM_LINE_HOOKS, desc)) {
cleanup_file(&f);
return -1;
}
cleanup_file(&f);
if (desc->datacmp == NULL)
desc->datacmp = get_default_compressor();

View File

@ -281,12 +281,8 @@ static int alloc_file_ids(image_entry_t *list)
int filelist_read(const char *filename, image_entry_t **out)
{
image_entry_t *list = NULL;
input_file_t f;
if (open_file(&f, filename))
return -1;
if (process_file(&f, line_hooks, NUM_LINE_HOOKS, &list))
if (process_file(filename, line_hooks, NUM_LINE_HOOKS, &list))
goto fail;
if (list != NULL) {
@ -296,11 +292,9 @@ int filelist_read(const char *filename, image_entry_t **out)
goto fail;
}
cleanup_file(&f);
*out = list;
return 0;
fail:
cleanup_file(&f);
image_entry_free_list(list);
return -1;
}