gEDA-dev: PCB HID filename handling patch
ldoolitt at recycle.lbl.gov
ldoolitt at recycle.lbl.gov
Mon Jun 26 17:05:51 EDT 2006
Patch relative to CVS as of 2006-06-25
Refactors filename handling code common to the file
export HIDs (bom, gerber, png, eps, ps).
The png code had one feature (last_made_filename)
that was missing in the others. This refactoring
brings all the HIDs to feature parity, which lets
pcb -x bom --bomfile=foo.txt bar.pcb
work as expected. The existing code ignores the
command line flag, and writes the bom to bar.bom.
Likewise for .xy, .gbr, .eps, and .ps files.
Fixes to a couple of spelling errors crept in here.
Sorry, let me know if you want them split out for
the benefit of CVS comments.
- Larry
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/hid/bom/bom.c /home/ldoolitt/src/pcb-20060625/src/hid/bom/bom.c
--- pcb/src/hid/bom/bom.c 2006-03-22 15:02:10.000000000 -0800
+++ /home/ldoolitt/src/pcb-20060625/src/hid/bom/bom.c 2006-06-26 13:15:36.000000000 -0700
@@ -58,34 +58,12 @@
static HID_Attribute *
bom_get_export_options (int *n)
{
- char *buf = 0;
-
- if (PCB)
- {
- buf = (char *) malloc (strlen (PCB->Filename) + 4);
- if (buf)
- {
- strcpy (buf, PCB->Filename);
- if (strcmp (buf + strlen (buf) - 4, ".pcb") == 0)
- buf[strlen (buf) - 4] = 0;
- strcat (buf, ".bom");
- if (bom_options[HA_bomfile].default_val.str_value)
- free (bom_options[HA_bomfile].default_val.str_value);
- bom_options[HA_bomfile].default_val.str_value = buf;
- }
-
- buf = (char *) malloc (strlen (PCB->Filename) + 4);
- if (buf)
- {
- strcpy (buf, PCB->Filename);
- if (strcmp (buf + strlen (buf) - 4, ".pcb") == 0)
- buf[strlen (buf) - 4] = 0;
- strcat (buf, ".xy");
- if (bom_options[HA_xyfile].default_val.str_value)
- free (bom_options[HA_xyfile].default_val.str_value);
- bom_options[HA_xyfile].default_val.str_value = buf;
- }
- }
+ static char *last_bom_filename = 0;
+ static char *last_xy_filename = 0;
+ if (PCB) {
+ derive_default_filename(PCB->Filename, &bom_options[HA_bomfile], ".bom", &last_bom_filename);
+ derive_default_filename(PCB->Filename, &bom_options[HA_xyfile ], ".xy" , &last_xy_filename );
+ }
if (n)
*n = NUM_OPTIONS;
@@ -545,7 +523,7 @@
0, /* bom_prompt_for */
0, /* bom_attribute_dialog */
0, /* bom_show_item */
- 0, /* bom_bee */
+ 0, /* bom_beep */
};
void
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/hid/common/hidinit.c /home/ldoolitt/src/pcb-20060625/src/hid/common/hidinit.c
--- pcb/src/hid/common/hidinit.c 2006-03-22 15:03:14.000000000 -0800
+++ /home/ldoolitt/src/pcb-20060625/src/hid/common/hidinit.c 2006-06-26 13:17:36.000000000 -0700
@@ -328,3 +328,23 @@
return 1;
}
+
+/* otherwise homeless function, refactored out of the five export HIDs */
+void derive_default_filename(const char *pcbfile, HID_Attribute *filename_attrib, const char *suffix, char **memory)
+{
+ char *buf;
+ if (!pcbfile || (memory && filename_attrib->default_val.str_value != *memory)) return;
+ buf = malloc (strlen (pcbfile) + strlen(suffix) + 1);
+ if (memory) *memory = buf;
+ if (buf) {
+ size_t bl;
+ strcpy (buf, pcbfile);
+ bl = strlen(buf);
+ if (bl > 4 && strcmp (buf + bl - 4, ".pcb") == 0)
+ buf[bl - 4] = 0;
+ strcat(buf, suffix);
+ if (filename_attrib->default_val.str_value)
+ free(filename_attrib->default_val.str_value);
+ filename_attrib->default_val.str_value = buf;
+ }
+}
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/hid/gerber/gerber.c /home/ldoolitt/src/pcb-20060625/src/hid/gerber/gerber.c
--- pcb/src/hid/gerber/gerber.c 2006-06-09 20:07:42.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/hid/gerber/gerber.c 2006-06-26 13:19:01.000000000 -0700
@@ -255,21 +255,8 @@
static HID_Attribute *
gerber_get_export_options (int *n)
{
- char *buf = 0;
-
- if (PCB && PCB->Filename)
- {
- buf = (char *) malloc (strlen (PCB->Filename) + 4);
- if (buf)
- {
- strcpy (buf, PCB->Filename);
- if (strcmp (buf + strlen (buf) - 4, ".pcb") == 0)
- buf[strlen (buf) - 4] = 0;
- if (gerber_options[HA_gerberfile].default_val.str_value)
- free (gerber_options[HA_gerberfile].default_val.str_value);
- gerber_options[HA_gerberfile].default_val.str_value = buf;
- }
- }
+ static char *last_made_filename = 0;
+ if (PCB) derive_default_filename(PCB->Filename, &gerber_options[HA_gerberfile], "", &last_made_filename);
if (n)
*n = NUM_OPTIONS;
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/hid/gtk/gui-top-window.c /home/ldoolitt/src/pcb-20060625/src/hid/gtk/gui-top-window.c
--- pcb/src/hid/gtk/gui-top-window.c 2006-06-09 20:07:43.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/hid/gtk/gui-top-window.c 2006-06-26 09:55:41.000000000 -0700
@@ -3710,7 +3710,7 @@
HID_Boolean, 0, 0, {0, 0, 0}, 0, &stdin_listen},
#define HA_listen 0
- {"bg-image", "Bacground Image",
+ {"bg-image", "Background Image",
HID_String, 0, 0, {0, 0, 0}, 0, &bg_image_file},
#define HA_bg_image 1
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/hid/hidint.h /home/ldoolitt/src/pcb-20060625/src/hid/hidint.h
--- pcb/src/hid/hidint.h 2006-03-22 15:15:31.000000000 -0800
+++ /home/ldoolitt/src/pcb-20060625/src/hid/hidint.h 2006-06-26 12:11:06.000000000 -0700
@@ -62,3 +62,5 @@
BoxType *hid_get_extents (void *item);
#endif
+
+void derive_default_filename(const char *pcbfile, HID_Attribute *filename_attrib, const char *suffix, char **memory);
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/hid/png/png.c /home/ldoolitt/src/pcb-20060625/src/hid/png/png.c
--- pcb/src/hid/png/png.c 2006-06-09 20:07:43.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/hid/png/png.c 2006-06-26 12:13:53.000000000 -0700
@@ -160,45 +160,29 @@
static HID_Attr_Val png_values[NUM_OPTIONS];
+static const char *get_file_suffix(void)
+{
+ const char *fmt;
+ const char *result;
+ fmt = filetypes[png_attribute_list[HA_filetype].default_val.int_value];
+ /* or is it filetypes[png_attribute_list[HA_filetype].default_val.int_value]; ? */
+ if (strcmp (fmt, FMT_gif) == 0) result=".gif";
+ else if (strcmp (fmt, FMT_jpg) == 0) result=".jpg";
+ else if (strcmp (fmt, FMT_png) == 0) result=".png";
+ else {
+ fprintf (stderr, "Error: Invalid graphic file format\n");
+ result=".???";
+ }
+ return result;
+}
+
static HID_Attribute *
png_get_export_options (int *n)
{
static char *last_made_filename = 0;
- char *buf = 0;
- const char *fmt;
-
- if (PCB && PCB->Filename
- && png_attribute_list[HA_pngfile].default_val.str_value ==
- last_made_filename)
- {
- /* need 4 for the ".png" and 1 for the terminating \0 */
- buf = (char *) malloc (strlen (PCB->Filename) + 5);
- last_made_filename = buf;
- if (buf)
- {
- strcpy (buf, PCB->Filename);
- if (strcmp (buf + strlen (buf) - 4, ".pcb") == 0)
- buf[strlen (buf) - 4] = 0;
+ const char *suffix = get_file_suffix();
- fmt = filetypes[png_attribute_list[HA_filetype].default_val.int_value];
-
- if (strcmp (fmt, FMT_gif) == 0)
- strcat (buf, ".gif");
- else if (strcmp (fmt, FMT_jpg) == 0)
- strcat (buf, ".jpg");
- else if (strcmp (fmt, FMT_png) == 0)
- strcat (buf, ".png");
- else
- {
- fprintf (stderr, "Error: Invalid graphic file format\n");
- strcat (buf, ".???");
- }
-
- if (png_attribute_list[HA_pngfile].default_val.str_value)
- free (png_attribute_list[HA_pngfile].default_val.str_value);
- png_attribute_list[HA_pngfile].default_val.str_value = buf;
- }
- }
+ if (PCB) derive_default_filename(PCB->Filename, &png_attribute_list[HA_pngfile], suffix, &last_made_filename);
if (n)
*n = NUM_OPTIONS;
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/hid/ps/eps.c /home/ldoolitt/src/pcb-20060625/src/hid/ps/eps.c
--- pcb/src/hid/ps/eps.c 2006-06-09 20:07:44.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/hid/ps/eps.c 2006-06-26 12:19:39.000000000 -0700
@@ -73,25 +73,8 @@
eps_get_export_options (int *n)
{
static char *last_made_filename = 0;
- char *buf = 0;
- if (PCB && PCB->Filename
- && eps_attribute_list[HA_psfile].default_val.str_value ==
- last_made_filename)
- {
- buf = (char *) malloc (strlen (PCB->Filename) + 5);
- last_made_filename = buf;
- if (buf)
- {
- strcpy (buf, PCB->Filename);
- if (strcmp (buf + strlen (buf) - 4, ".pcb") == 0)
- buf[strlen (buf) - 4] = 0;
- strcat (buf, ".eps");
- if (eps_attribute_list[HA_psfile].default_val.str_value)
- free (eps_attribute_list[HA_psfile].default_val.str_value);
- eps_attribute_list[HA_psfile].default_val.str_value = buf;
- }
- }
+ if (PCB) derive_default_filename(PCB->Filename, &eps_attribute_list[HA_psfile], ".eps", &last_made_filename);
if (n)
*n = NUM_OPTIONS;
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/hid/ps/ps.c /home/ldoolitt/src/pcb-20060625/src/hid/ps/ps.c
--- pcb/src/hid/ps/ps.c 2006-06-09 20:07:44.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/hid/ps/ps.c 2006-06-26 13:19:28.000000000 -0700
@@ -123,22 +123,8 @@
static HID_Attribute *
ps_get_export_options (int *n)
{
- char *buf = 0;
-
- if (PCB && PCB->Filename)
- {
- buf = (char *) malloc (strlen (PCB->Filename) + 4);
- if (buf)
- {
- strcpy (buf, PCB->Filename);
- if (strcmp (buf + strlen (buf) - 4, ".pcb") == 0)
- buf[strlen (buf) - 4] = 0;
- strcat (buf, ".ps");
- if (ps_attribute_list[HA_psfile].default_val.str_value)
- free (ps_attribute_list[HA_psfile].default_val.str_value);
- ps_attribute_list[HA_psfile].default_val.str_value = buf;
- }
- }
+ static char *last_made_filename = 0;
+ if (PCB) derive_default_filename(PCB->Filename, &ps_attribute_list[HA_psfile], ".ps", &last_made_filename);
if (n)
*n = NUM_OPTIONS;
More information about the geda-dev
mailing list