From 57e76aeb9ed8b14ec75afac2310b53af221cac6e Mon Sep 17 00:00:00 2001 From: werner Date: Tue, 11 Aug 2009 20:17:39 +0000 Subject: [PATCH] - vector labels are already in a per-frame namespace, so we don't need to add the frame name when auto-generating them - moved file I/O from gui.c to file.c - ps_line used the wrong endpoint coordinate - option -k makes fped write KiCad non-interactively - option -p makes fped write Postscript non-interactively git-svn-id: http://svn.openmoko.org/trunk/eda/fped@5418 99fdad57-331a-0410-800a-d7fa5415bdb3 --- Makefile | 2 +- dump.c | 3 +- file.c | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++ file.h | 28 ++++++++ fped.c | 47 +++++++++++--- gui.c | 164 ++--------------------------------------------- postscript.c | 2 +- 7 files changed, 251 insertions(+), 172 deletions(-) create mode 100644 file.c create mode 100644 file.h diff --git a/Makefile b/Makefile index 3672954..46bf4d7 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ # OBJS = fped.o expr.o coord.o obj.o delete.o inst.o util.o error.o \ - unparse.o dump.o kicad.o postscript.o meas.o \ + unparse.o file.o dump.o kicad.o postscript.o meas.o \ cpp.o lex.yy.o y.tab.o \ gui.o gui_util.o gui_style.o gui_inst.o gui_status.o gui_canvas.o \ gui_tool.o gui_over.o gui_meas.o gui_frame.o diff --git a/dump.c b/dump.c index c65600f..7f817fb 100644 --- a/dump.c +++ b/dump.c @@ -90,8 +90,7 @@ static char *generate_name(const struct vec *base) n = 0; for (walk = base->frame->vecs; walk != base; walk = walk->next) n++; - return stralloc_printf("_%s_%d", - base->frame->name ? base->frame->name : "", n); + return stralloc_printf("__%d", n); } diff --git a/file.c b/file.c new file mode 100644 index 0000000..2547e42 --- /dev/null +++ b/file.c @@ -0,0 +1,177 @@ +/* + * file.c - File handling + * + * Written 2009 by Werner Almesberger + * Copyright 2009 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 + +#include "dump.h" +#include "kicad.h" +#include "postscript.h" + +#include "util.h" +#include "file.h" + + +extern char *save_file_name; + + +/* ----- general helper functions ------------------------------------------ */ + + +char *set_extension(const char *name, const char *ext) +{ + char *s = stralloc(name); + char *slash, *dot; + char *res; + + slash = strrchr(s, '/'); + dot = strrchr(slash ? slash : s, '.'); + if (dot) + *dot = 0; + res = stralloc_printf("%s.%s", s, ext); + free(s); + return res; +} + + +int save_to(const char *name, int (*fn)(FILE *file)) +{ + FILE *file; + + file = fopen(name, "w"); + if (!file) { + perror(name); + return 0; + } + if (!fn(file)) { + perror(name); + return 0; + } + if (fclose(file) == EOF) { + perror(name); + return 0; + } + return 1; +} + + +void save_with_backup(const char *name, int (*fn)(FILE *file)) +{ + char *s = stralloc(name); + char *back, *tmp; + char *slash, *dot; + int n; + struct stat st; + + /* save to temporary file */ + + slash = strrchr(s, '/'); + if (!slash) + tmp = stralloc_printf("~%s", s); + else { + *slash = 0; + tmp = stralloc_printf("%s/~%s", s, slash+1); + *slash = '/'; + } + + if (!save_to(tmp, fn)) + return; + + /* move existing file out of harm's way */ + + dot = strrchr(slash ? slash : s, '.'); + if (dot) + *dot = 0; + n = 0; + while (1) { + back = stralloc_printf("%s~%d%s%s", + s, n, dot ? "." : "", dot ? dot+1 : ""); + if (stat(back, &st) < 0) { + if (errno == ENOENT) + break; + perror(back); + free(back); + return; + } + free(back); + n++; + } + if (rename(name, back) < 0) { + if (errno != ENOENT) { + perror(name); + free(back); + return; + } + } else { + fprintf(stderr, "renamed %s to %s\n", name, back); + } + free(back); + + /* rename to final name */ + + if (rename(tmp, name) < 0) { + perror(name); + free(tmp); + return; + } + free(tmp); + + fprintf(stderr, "saved to %s\n", name); +} + + +/* ----- application-specific save handlers -------------------------------- */ + + +void save_fpd(void) +{ + if (save_file_name) + save_with_backup(save_file_name, dump); + else { + if (!dump(stdout)) + perror("stdout"); + } +} + + +void write_kicad(void) +{ + char *name; + + if (save_file_name) { + name = set_extension(save_file_name, "mod"); + save_to(name, kicad); + free(name); + } else { + if (!kicad(stdout)) + perror("stdout"); + } +} + + +void write_ps(void) +{ + char *name; + + if (save_file_name) { + name = set_extension(save_file_name, "ps"); + save_to(name, postscript); + free(name); + } else { + if (!postscript(stdout)) + perror("stdout"); + } +} diff --git a/file.h b/file.h new file mode 100644 index 0000000..d201258 --- /dev/null +++ b/file.h @@ -0,0 +1,28 @@ +/* + * file.h - File handling + * + * Written 2009 by Werner Almesberger + * Copyright 2009 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 + + +char *set_extension(const char *name, const char *ext); +int save_to(const char *name, int (*fn)(FILE *file)); +void save_with_backup(const char *name, int (*fn)(FILE *file)); + +void save_fpd(void); +void write_kicad(void); +void write_ps(void); + +#endif /* !FILE_H */ diff --git a/fped.c b/fped.c index 8485bd9..284b8b0 100644 --- a/fped.c +++ b/fped.c @@ -13,19 +13,21 @@ #include #include +#include #include "cpp.h" #include "util.h" #include "error.h" #include "obj.h" #include "inst.h" +#include "file.h" #include "gui.h" extern void scan_empty(void); extern int yyparse(void); -char *save_file = NULL; +char *save_file_name = NULL; static void load_file(const char *name) @@ -38,7 +40,9 @@ static void load_file(const char *name) static void usage(const char *name) { - fprintf(stderr, "usage: %s [in_file [out_file]]\n", name); + fprintf(stderr, "usage: %s [-k|-p] [in_file [out_file]]\n\n", name); + fprintf(stderr, " -k write KiCad output, then exit\n"); + fprintf(stderr, " -p write Postscript output, then exit\n"); exit(1); } @@ -47,20 +51,39 @@ int main(int argc, char **argv) { const char *name = *argv; int error; + int batch_write_kicad = 0, batch_write_ps = 0; + int c; error = gui_init(&argc, &argv); if (error) return error; - switch (argc) { - case 1: + + while ((c = getopt(argc, argv, "kp")) != EOF) + switch (c) { + case 'k': + batch_write_kicad = 1; + break; + case 'p': + batch_write_ps = 1; + break; + default: + usage(name); + } + + switch (argc-optind) { + case 0: scan_empty(); (void) yyparse(); break; - case 3: - save_file = argv[2]; - /* fall through */ + case 1: + load_file(argv[optind]); + save_file_name = argv[optind]; + break; case 2: - load_file(argv[1]); + load_file(argv[optind]); + save_file_name = argv[optind+1]; + if (!strcmp(save_file_name, "-")) + save_file_name = NULL; break; default: usage(name); @@ -72,6 +95,14 @@ int main(int argc, char **argv) reporter = report_to_stderr; if (!instantiate()) return 1; + + if (batch_write_kicad) + write_kicad(); + if (batch_write_ps) + write_ps(); + if (batch_write_kicad || batch_write_ps) + exit(0); + // inst_debug(); error = gui_main(); if (error) diff --git a/gui.c b/gui.c index b9e89dc..d921dfd 100644 --- a/gui.c +++ b/gui.c @@ -11,18 +11,10 @@ */ -#include -#include -#include -#include #include -#include "util.h" #include "inst.h" -#include "obj.h" -#include "dump.h" -#include "kicad.h" -#include "postscript.h" +#include "file.h" #include "gui_util.h" #include "gui_style.h" #include "gui_status.h" @@ -37,8 +29,6 @@ #include "icons/meas_off.xpm" -extern char *save_file; - GtkWidget *root; int show_stuff = 1; int show_meas = 1; @@ -49,162 +39,16 @@ static GtkWidget *ev_stuff, *ev_meas; static GtkWidget *stuff_image[2], *meas_image[2]; -/* ----- save/write operations --------------------------------------------- */ - - -static char *set_extension(const char *name, const char *ext) -{ - char *s = stralloc(name); - char *slash, *dot; - char *res; - - slash = strrchr(s, '/'); - dot = strrchr(slash ? slash : s, '.'); - if (dot) - *dot = 0; - res = stralloc_printf("%s.%s", s, ext); - free(s); - return res; -} - - -static int save_to(const char *name, int (*fn)(FILE *file)) -{ - FILE *file; - - file = fopen(name, "w"); - if (!file) { - perror(name); - return 0; - } - if (!fn(file)) { - perror(name); - return 0; - } - if (fclose(file) == EOF) { - perror(name); - return 0; - } - return 1; -} - - -static void save_with_backup(const char *name, int (*fn)(FILE *file)) -{ - char *s = stralloc(name); - char *back, *tmp; - char *slash, *dot; - int n; - struct stat st; - - /* save to temporary file */ - - slash = strrchr(s, '/'); - if (!slash) - tmp = stralloc_printf("~%s", s); - else { - *slash = 0; - tmp = stralloc_printf("%s/~%s", s, slash+1); - *slash = '/'; - } - - if (!save_to(tmp, fn)) - return; - - /* move existing file out of harm's way */ - - dot = strrchr(slash ? slash : s, '.'); - if (dot) - *dot = 0; - n = 0; - while (1) { - back = stralloc_printf("%s~%d%s%s", - s, n, dot ? "." : "", dot ? dot+1 : ""); - if (stat(back, &st) < 0) { - if (errno == ENOENT) - break; - perror(back); - free(back); - return; - } - free(back); - n++; - } - if (rename(name, back) < 0) { - if (errno != ENOENT) { - perror(name); - free(back); - return; - } - } else { - fprintf(stderr, "renamed %s to %s\n", name, back); - } - free(back); - - /* rename to final name */ - - if (rename(tmp, name) < 0) { - perror(name); - free(tmp); - return; - } - free(tmp); - - fprintf(stderr, "saved to %s\n", name); -} - - -static void menu_save(void) -{ - if (save_file) - save_with_backup(save_file, dump); - else { - if (!dump(stdout)) - perror("stdout"); - } -} - - -static void menu_write_kicad(void) -{ - char *name; - - if (save_file) { - name = set_extension(save_file, "mod"); - save_to(name, kicad); - free(name); - } else { - if (!kicad(stdout)) - perror("stdout"); - } -} - - -static void menu_write_ps(void) -{ - char *name; - - if (save_file) { - name = set_extension(save_file, "ps"); - save_to(name, postscript); - free(name); - } else { - if (!postscript(stdout)) - perror("stdout"); - } -} - - /* ----- menu bar ---------------------------------------------------------- */ static GtkItemFactoryEntry menu_entries[] = { { "/File", NULL, NULL, 0, "" }, - { "/File/Save", NULL, menu_save, 0, "" }, + { "/File/Save", NULL, save_fpd, 0, "" }, { "/File/sep0", NULL, NULL, 0, "" }, - { "/File/Write KiCad", NULL, menu_write_kicad, 0, "" }, + { "/File/Write KiCad", NULL, write_kicad, 0, "" }, { "/File/Write Postscript", - NULL, menu_write_ps, 0, "" }, + NULL, write_ps, 0, "" }, { "/File/sep2", NULL, NULL, 0, "" }, { "/File/Quit", NULL, gtk_main_quit, 0, "" }, }; diff --git a/postscript.c b/postscript.c index a9f3eda..7b54ed1 100644 --- a/postscript.c +++ b/postscript.c @@ -51,7 +51,7 @@ static void ps_pad(FILE *file, const struct inst *inst) static void ps_line(FILE *file, const struct inst *inst) { struct coord a = inst->base; - struct coord b = inst->u.pad.other; + struct coord b = inst->u.rect.end; fprintf(file, "1 setlinecap 0.5 setgray %d setlinewidth\n", inst->u.rect.width);