2016-07-31 21:10:14 +03:00
|
|
|
/*
|
|
|
|
* file.c - Open and read a file
|
|
|
|
*
|
|
|
|
* Written 2016 by Werner Almesberger
|
|
|
|
* Copyright 2016 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-08-02 15:04:29 +03:00
|
|
|
#include "util.h"
|
2016-08-02 12:31:23 +03:00
|
|
|
#include "main.h"
|
2016-08-02 15:04:29 +03:00
|
|
|
#include "git-file.h"
|
2016-07-31 21:10:14 +03:00
|
|
|
#include "file.h"
|
|
|
|
|
|
|
|
|
2016-08-02 15:04:29 +03:00
|
|
|
static void read_from_file(FILE *file,
|
|
|
|
bool (*parse)(void *user, const char *line), void *user)
|
2016-07-31 21:10:14 +03:00
|
|
|
{
|
|
|
|
char buf[1000];
|
|
|
|
char *nl;
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), file)) {
|
|
|
|
nl = strchr(buf, '\n');
|
|
|
|
if (nl)
|
|
|
|
*nl = 0;
|
|
|
|
if (!parse(user, buf))
|
|
|
|
break;
|
|
|
|
}
|
2016-08-02 15:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void file_read(const char *name, bool (*parse)(void *user, const char *line),
|
|
|
|
void *user)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
char *colon, *tmp;
|
|
|
|
|
|
|
|
file = fopen(name, "r");
|
|
|
|
if (file) {
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "reading %s\n", name);
|
|
|
|
read_from_file(file, parse, user);
|
|
|
|
fclose(file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
perror(name);
|
|
|
|
|
|
|
|
colon = strchr(name, ':');
|
|
|
|
if (!colon) {
|
|
|
|
if (!verbose)
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = stralloc(name);
|
|
|
|
tmp[colon - name] = 0;
|
|
|
|
git_read(tmp, colon + 1, parse, user);
|
|
|
|
free(tmp);
|
2016-07-31 21:10:14 +03:00
|
|
|
}
|