1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-23 01:14:04 +02:00
eda-tools/sch2fig/file.c
Werner Almesberger 91efd87b73 sch2fig/git-file.c (send_data): don't pass newline to parser
We tried to do what fgets does, but file.c undid that for regular
files, so let's be compatible.
2016-08-02 09:57:47 -03:00

77 lines
1.4 KiB
C

/*
* 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>
#include "util.h"
#include "main.h"
#include "git-file.h"
#include "file.h"
bool file_cat(void *user, const char *line)
{
printf("%s\n", line);
return 1;
}
static void read_from_file(FILE *file,
bool (*parse)(void *user, const char *line), void *user)
{
char buf[1000];
char *nl;
while (fgets(buf, sizeof(buf), file)) {
nl = strchr(buf, '\n');
if (nl)
*nl = 0;
if (!parse(user, buf))
break;
}
}
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);
}