mirror of
git://projects.qi-hardware.com/eda-tools.git
synced 2024-08-25 06:10:14 +03:00
42 lines
830 B
C
42 lines
830 B
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 "file.h"
|
|
|
|
|
|
void file_read(const char *name, bool (*parse)(void *user, const char *line),
|
|
void *user)
|
|
{
|
|
FILE *file;
|
|
char buf[1000];
|
|
char *nl;
|
|
|
|
file = fopen(name, "r");
|
|
if (!file) {
|
|
perror(name);
|
|
exit(1);
|
|
}
|
|
while (fgets(buf, sizeof(buf), file)) {
|
|
nl = strchr(buf, '\n');
|
|
if (nl)
|
|
*nl = 0;
|
|
if (!parse(user, buf))
|
|
break;
|
|
}
|
|
fclose(file);
|
|
}
|