mirror of
https://github.com/pygos/pkg-utils.git
synced 2024-11-22 04:49:46 +01:00
Add utility function to iterate over lines in a file
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
parent
af7c6f09df
commit
f7e4aec2ea
3 changed files with 67 additions and 0 deletions
|
@ -11,4 +11,9 @@ ssize_t read_retry(int fd, void *buffer, size_t size);
|
||||||
|
|
||||||
int mkdir_p(const char *path);
|
int mkdir_p(const char *path);
|
||||||
|
|
||||||
|
typedef int (*linecb_t)(void *usr, const char *filename,
|
||||||
|
size_t linenum, char *line);
|
||||||
|
|
||||||
|
int foreach_line_in_file(const char *filename, void *usr, linecb_t fun);
|
||||||
|
|
||||||
#endif /* UTIL_H */
|
#endif /* UTIL_H */
|
||||||
|
|
|
@ -3,6 +3,7 @@ libutil_a_SOURCES += lib/util/write_retry.c lib/util/read_retry.c
|
||||||
libutil_a_SOURCES += lib/util/canonicalize_name.c
|
libutil_a_SOURCES += lib/util/canonicalize_name.c
|
||||||
libutil_a_SOURCES += include/util/util.h include/util/input_file.h
|
libutil_a_SOURCES += include/util/util.h include/util/input_file.h
|
||||||
libutil_a_SOURCES += include/util/hashtable.h lib/util/hashtable.c
|
libutil_a_SOURCES += include/util/hashtable.h lib/util/hashtable.c
|
||||||
|
libutil_a_SOURCES += lib/util/fileproc.c
|
||||||
|
|
||||||
libfilelist_a_SOURCES = lib/filelist/dump_toc.c lib/filelist/image_entry.c
|
libfilelist_a_SOURCES = lib/filelist/dump_toc.c lib/filelist/image_entry.c
|
||||||
libfilelist_a_SOURCES += lib/filelist/image_entry_sort.c
|
libfilelist_a_SOURCES += lib/filelist/image_entry_sort.c
|
||||||
|
|
61
lib/util/fileproc.c
Normal file
61
lib/util/fileproc.c
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "util/util.h"
|
||||||
|
|
||||||
|
int foreach_line_in_file(const char *filename, void *usr, linecb_t fun)
|
||||||
|
{
|
||||||
|
FILE *f = fopen(filename, "r");
|
||||||
|
char *line = NULL, *ptr;
|
||||||
|
size_t n, lineno = 1;
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
|
if (f == NULL) {
|
||||||
|
perror(filename);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
free(line);
|
||||||
|
|
||||||
|
line = NULL;
|
||||||
|
errno = 0;
|
||||||
|
n = 0;
|
||||||
|
ret = getline(&line, &n, f);
|
||||||
|
|
||||||
|
if (ret < 0) {
|
||||||
|
if (errno == 0) {
|
||||||
|
ret = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
perror(filename);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ptr = line; isspace(*ptr); ++ptr)
|
||||||
|
;
|
||||||
|
|
||||||
|
if (ptr != line)
|
||||||
|
memmove(line, ptr, strlen(ptr) + 1);
|
||||||
|
|
||||||
|
ptr = line + strlen(line);
|
||||||
|
while (ptr > line && isspace(ptr[-1]))
|
||||||
|
--ptr;
|
||||||
|
*ptr = '\0';
|
||||||
|
|
||||||
|
if (*line == '\0')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (fun(usr, filename, lineno++, line)) {
|
||||||
|
ret = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(line);
|
||||||
|
fclose(f);
|
||||||
|
return ret;
|
||||||
|
}
|
Loading…
Reference in a new issue