mirror of
https://github.com/pygos/pkg-utils.git
synced 2024-12-22 18:00:49 +01:00
pack cmd: seperate file line reader from file list parser
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
parent
53194aa2ac
commit
47e237688b
4 changed files with 73 additions and 58 deletions
|
@ -25,6 +25,7 @@ EXTRA_DIST = autogen.sh
|
|||
pkg_SOURCES += main/cmd/pack/filelist.c main/cmd/pack/filelist_read.c
|
||||
pkg_SOURCES += main/cmd/pack/write_toc.c main/cmd/pack/write_files.c
|
||||
pkg_SOURCES += main/cmd/pack/pack.h main/cmd/pack/pack.c
|
||||
pkg_SOURCES += main/cmd/pack/input_file.c
|
||||
|
||||
# dump command
|
||||
pkg_SOURCES += main/cmd/dump/dump.c main/cmd/dump/dump.h main/cmd/dump/dump_toc.c
|
||||
|
|
|
@ -11,59 +11,6 @@ static const struct {
|
|||
|
||||
#define NUM_LINE_HOOKS (sizeof(line_hooks) / sizeof(line_hooks[0]))
|
||||
|
||||
static int prefetch_line(input_file_t *f)
|
||||
{
|
||||
char *line = NULL;
|
||||
size_t n = 0;
|
||||
ssize_t ret;
|
||||
|
||||
free(f->line);
|
||||
f->line = NULL;
|
||||
|
||||
errno = 0;
|
||||
ret = getline(&line, &n, f->f);
|
||||
|
||||
if (ret < 0) {
|
||||
if (errno != 0) {
|
||||
perror(f->filename);
|
||||
free(line);
|
||||
return -1;
|
||||
}
|
||||
free(line);
|
||||
return 1;
|
||||
}
|
||||
|
||||
n = strlen(line);
|
||||
while (n >0 && isspace(line[n - 1]))
|
||||
--n;
|
||||
line[n] = '\0';
|
||||
|
||||
f->line = line;
|
||||
f->linenum += 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void cleanup_file(input_file_t *f)
|
||||
{
|
||||
fclose(f->f);
|
||||
free(f->line);
|
||||
}
|
||||
|
||||
static 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;
|
||||
}
|
||||
|
||||
image_entry_t *filelist_read(const char *filename)
|
||||
{
|
||||
image_entry_t *ent, *list = NULL;
|
||||
|
@ -82,11 +29,7 @@ image_entry_t *filelist_read(const char *filename)
|
|||
if (ret > 0)
|
||||
break;
|
||||
|
||||
for (ptr = f.line; isspace(*ptr); ++ptr)
|
||||
;
|
||||
|
||||
if (*ptr == '\0' || *ptr == '#')
|
||||
continue;
|
||||
ptr = f.line;
|
||||
|
||||
for (i = 0; i < NUM_LINE_HOOKS; ++i) {
|
||||
len = strlen(line_hooks[i].name);
|
||||
|
|
65
main/cmd/pack/input_file.c
Normal file
65
main/cmd/pack/input_file.c
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include "pack.h"
|
||||
|
||||
int prefetch_line(input_file_t *f)
|
||||
{
|
||||
char *line, *ptr;
|
||||
ssize_t ret;
|
||||
size_t n;
|
||||
retry:
|
||||
free(f->line);
|
||||
f->line = NULL;
|
||||
|
||||
errno = 0;
|
||||
line = NULL;
|
||||
n = 0;
|
||||
ret = getline(&line, &n, f->f);
|
||||
|
||||
if (ret < 0) {
|
||||
if (errno != 0) {
|
||||
perror(f->filename);
|
||||
free(line);
|
||||
return -1;
|
||||
}
|
||||
free(line);
|
||||
return 1;
|
||||
}
|
||||
|
||||
n = strlen(line);
|
||||
while (n >0 && isspace(line[n - 1]))
|
||||
--n;
|
||||
line[n] = '\0';
|
||||
|
||||
f->line = line;
|
||||
f->linenum += 1;
|
||||
|
||||
for (ptr = f->line; isspace(*ptr); ++ptr)
|
||||
;
|
||||
|
||||
if (*ptr == '\0' || *ptr == '#')
|
||||
goto retry;
|
||||
|
||||
if (ptr != f->line)
|
||||
memmove(f->line, ptr, strlen(ptr) + 1);
|
||||
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;
|
||||
}
|
|
@ -40,4 +40,10 @@ int write_toc(pkg_writer_t *wr, image_entry_t *list, compressor_t *cmp);
|
|||
|
||||
int write_files(pkg_writer_t *wr, image_entry_t *list, compressor_t *cmp);
|
||||
|
||||
int prefetch_line(input_file_t *f);
|
||||
|
||||
void cleanup_file(input_file_t *f);
|
||||
|
||||
int open_file(input_file_t *f, const char *filename);
|
||||
|
||||
#endif /* PACK_H */
|
||||
|
|
Loading…
Reference in a new issue