1
0
mirror of git://projects.qi-hardware.com/fped.git synced 2024-11-29 01:28:06 +02:00

- 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
This commit is contained in:
werner 2009-08-11 20:17:39 +00:00
parent 1dfb8da99a
commit 57e76aeb9e
7 changed files with 251 additions and 172 deletions

View File

@ -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

3
dump.c
View File

@ -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);
}

177
file.c Normal file
View File

@ -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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#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");
}
}

28
file.h Normal file
View File

@ -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 <stdio.h>
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 */

47
fped.c
View File

@ -13,19 +13,21 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#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)

164
gui.c
View File

@ -11,18 +11,10 @@
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <gtk/gtk.h>
#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, "<Branch>" },
{ "/File/Save", NULL, menu_save, 0, "<Item>" },
{ "/File/Save", NULL, save_fpd, 0, "<Item>" },
{ "/File/sep0", NULL, NULL, 0, "<Separator>" },
{ "/File/Write KiCad", NULL, menu_write_kicad, 0, "<Item>" },
{ "/File/Write KiCad", NULL, write_kicad, 0, "<Item>" },
{ "/File/Write Postscript",
NULL, menu_write_ps, 0, "<Item>" },
NULL, write_ps, 0, "<Item>" },
{ "/File/sep2", NULL, NULL, 0, "<Separator>" },
{ "/File/Quit", NULL, gtk_main_quit, 0, "<Item>" },
};

View File

@ -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);