2019-01-31 17:56:18 +01:00
|
|
|
#include <string.h>
|
2019-03-08 17:10:47 +01:00
|
|
|
#include <stdio.h>
|
2019-01-31 17:56:18 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2019-02-04 15:21:19 +01:00
|
|
|
#include "util/input_file.h"
|
2019-03-08 17:10:47 +01:00
|
|
|
#include "util/util.h"
|
2019-01-27 16:41:20 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
void input_file_complain(const char *filename, size_t linenum, const char *msg)
|
2019-01-27 16:41:20 +01:00
|
|
|
{
|
2019-03-08 17:10:47 +01:00
|
|
|
fprintf(stderr, "%s: %zu: %s\n", filename, linenum, msg);
|
|
|
|
}
|
2019-01-27 16:41:20 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
struct userdata {
|
|
|
|
const keyword_handler_t *handlers;
|
|
|
|
size_t count;
|
|
|
|
void *obj;
|
|
|
|
};
|
2019-01-27 16:41:20 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
static int handle_line(void *usr, const char *filename,
|
|
|
|
size_t linenum, char *line)
|
|
|
|
{
|
|
|
|
struct userdata *u = usr;
|
|
|
|
size_t i, len;
|
2019-01-27 16:41:20 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
while (isspace(*line))
|
|
|
|
++line;
|
2019-01-27 16:41:20 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
if (*line == '\0' || *line == '#')
|
|
|
|
return 0;
|
2019-01-31 17:50:49 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
for (i = 0; i < u->count; ++i) {
|
|
|
|
len = strlen(u->handlers[i].name);
|
2019-01-31 17:50:49 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
if (strncmp(line, u->handlers[i].name, len) != 0)
|
|
|
|
continue;
|
|
|
|
if (!isspace(line[len]) && line[len] != '\0')
|
|
|
|
continue;
|
|
|
|
for (line += len; isspace(*line); ++line)
|
|
|
|
;
|
|
|
|
break;
|
|
|
|
}
|
2019-01-31 17:50:49 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
if (i == u->count) {
|
|
|
|
fprintf(stderr, "%s: %zu: unknown keyword\n",
|
|
|
|
filename, linenum);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-01-27 16:41:20 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
return u->handlers[i].handle(line, filename, linenum, u->obj);
|
2019-01-31 22:09:38 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 15:22:35 +01:00
|
|
|
int process_file(const char *filename, const keyword_handler_t *handlers,
|
2019-01-31 22:09:38 +01:00
|
|
|
size_t count, void *obj)
|
|
|
|
{
|
2019-03-08 17:10:47 +01:00
|
|
|
struct userdata u = { handlers, count, obj };
|
2019-01-31 22:09:38 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
return foreach_line_in_file(filename, &u, handle_line);
|
2019-01-31 22:09:38 +01:00
|
|
|
}
|