1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-27 02:49:22 +02:00

sch2fig/main.c (read_file): move to file.c

This is to allow further I/O abstraction, later.
This commit is contained in:
Werner Almesberger 2016-07-31 15:10:14 -03:00
parent 3e18e7a72e
commit 71aeab9ab8
4 changed files with 71 additions and 29 deletions

View File

@ -12,6 +12,7 @@
NAME = sch2fig NAME = sch2fig
OBJS = main.o sch-parse.o sch-render.o lib-parse.o lib-render.o \ OBJS = main.o sch-parse.o sch-render.o lib-parse.o lib-render.o \
file.o \
style.o fig.o cairo.o gfx.o dwg.o text.o misc.o style.o fig.o cairo.o gfx.o dwg.o text.o misc.o
CFLAGS = -g -O -Wall -Wextra -Wno-unused-parameter -Wshadow \ CFLAGS = -g -O -Wall -Wextra -Wno-unused-parameter -Wshadow \

41
sch2fig/file.c Normal file
View File

@ -0,0 +1,41 @@
/*
* 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);
}

22
sch2fig/file.h Normal file
View File

@ -0,0 +1,22 @@
/*
* file.h - 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.
*/
#ifndef FILE_H
#define FILE_H
#include <stdbool.h>
void file_read(const char *name, bool (*parse)(void *user, const char *line),
void *user);
#endif /* !FILE_H */

View File

@ -19,42 +19,20 @@
#include "fig.h" #include "fig.h"
#include "cairo.h" #include "cairo.h"
#include "gfx.h" #include "gfx.h"
#include "file.h"
#include "lib.h" #include "lib.h"
#include "sch.h" #include "sch.h"
static bool do_sch_parse(void *ctx, const char *line) static bool do_sch_parse(void *user, const char *line)
{ {
return sch_parse(ctx, line); return sch_parse(user, line);
} }
static bool do_lib_parse(void *ctx, const char *line) static bool do_lib_parse(void *user, const char *line)
{ {
return lib_parse(ctx, line); return lib_parse(user, line);
}
static void read_file(const char *name, void *ctx,
bool (*parse)(void *ctx, const char *line))
{
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(ctx, buf))
break;
}
fclose(file);
} }
@ -99,11 +77,11 @@ int main(int argc, char **argv)
struct lib_ctx ctx; struct lib_ctx ctx;
lib_init(&ctx); lib_init(&ctx);
read_file(argv[arg], &ctx, do_lib_parse); file_read(argv[arg], do_lib_parse, &ctx);
} }
sch_init(&sch_ctx); sch_init(&sch_ctx);
read_file(argv[argc - 1], &sch_ctx, do_sch_parse); file_read(argv[argc - 1], do_sch_parse, &sch_ctx);
gfx_init(&fig_ops, template, n_vars, vars); gfx_init(&fig_ops, template, n_vars, vars);
//gfx_init(&cairo_ops, template, n_vars, vars); //gfx_init(&cairo_ops, template, n_vars, vars);
sch_render(&sch_ctx); sch_render(&sch_ctx);