From 71aeab9ab80bd29fe7049db9872df65da7cdbb3b Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 31 Jul 2016 15:10:14 -0300 Subject: [PATCH] sch2fig/main.c (read_file): move to file.c This is to allow further I/O abstraction, later. --- sch2fig/Makefile | 1 + sch2fig/file.c | 41 +++++++++++++++++++++++++++++++++++++++++ sch2fig/file.h | 22 ++++++++++++++++++++++ sch2fig/main.c | 36 +++++++----------------------------- 4 files changed, 71 insertions(+), 29 deletions(-) create mode 100644 sch2fig/file.c create mode 100644 sch2fig/file.h diff --git a/sch2fig/Makefile b/sch2fig/Makefile index 45c060f..87d78f0 100644 --- a/sch2fig/Makefile +++ b/sch2fig/Makefile @@ -12,6 +12,7 @@ NAME = sch2fig 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 CFLAGS = -g -O -Wall -Wextra -Wno-unused-parameter -Wshadow \ diff --git a/sch2fig/file.c b/sch2fig/file.c new file mode 100644 index 0000000..c94536c --- /dev/null +++ b/sch2fig/file.c @@ -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 +#include +#include +#include + +#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); +} diff --git a/sch2fig/file.h b/sch2fig/file.h new file mode 100644 index 0000000..fabc2e4 --- /dev/null +++ b/sch2fig/file.h @@ -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 + + +void file_read(const char *name, bool (*parse)(void *user, const char *line), + void *user); + +#endif /* !FILE_H */ diff --git a/sch2fig/main.c b/sch2fig/main.c index 0284dca..db8cf85 100644 --- a/sch2fig/main.c +++ b/sch2fig/main.c @@ -19,42 +19,20 @@ #include "fig.h" #include "cairo.h" #include "gfx.h" +#include "file.h" #include "lib.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); -} - - -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); + return lib_parse(user, line); } @@ -99,11 +77,11 @@ int main(int argc, char **argv) struct lib_ctx ctx; lib_init(&ctx); - read_file(argv[arg], &ctx, do_lib_parse); + file_read(argv[arg], do_lib_parse, &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(&cairo_ops, template, n_vars, vars); sch_render(&sch_ctx);