don't update min/max bounding box when drawing with CLEAR polarity
[geda-gerbv.git] / src / tooltable.c
blob935b274fac77f10bff9f7b35d5c66c86c9774634
1 /*
2 * tooltable.c
3 * Copyright (C) 2004 dmitri (at) users.sourceforge.net
5 * $Id$
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22 /** \file tooltable.c
23 \brief Tool file parsing functions
24 \ingroup libgerbv
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #define MIN_TOOL_NUMBER 1 /* T01 */
33 #define MAX_TOOL_NUMBER 99 /* T99 */
35 static int have_tools_file = 0;
36 static double tools[1+MAX_TOOL_NUMBER];
38 static void
39 ProcessToolLine(const char *cp)
41 const char *cp0 = cp;
42 int toolNumber;
43 double toolDia;
45 if (cp == NULL)
46 return;
48 /* Skip leading spaces if there are some */
49 while (isspace((int) *cp)) {
50 if (*(++cp) == '\0')
51 return;
54 if (*cp != 'T') {
55 fprintf(stderr, "*** WARNING: Strange tool \"%s\" ignored.\n", cp0);
56 return;
58 if ((!isdigit((int) cp[1])) || (!isdigit((int) cp[2]))) {
59 fprintf(stderr, "*** WARNING: No tool number in \"%s\".\n", cp0);
60 return;
62 do {
63 char tnb[3];
64 tnb[0] = cp[1];
65 tnb[1] = cp[2];
66 tnb[2] = '\0';
67 toolNumber = atoi(tnb);
68 if ((toolNumber < MIN_TOOL_NUMBER) || (toolNumber > MAX_TOOL_NUMBER)) {
69 fprintf(stderr, "*** WARNING: Can't parse tool number in \"%s\".\n", cp0);
70 return;
72 } while (0);
74 cp += 3; /* Skip Tnn */
76 /* Skip following spaces if there are some */
77 while (isspace((int) *cp)) {
78 if (*(++cp) == '\0')
79 return;
82 /* The rest of the line is supposed to be the tool diameter in inches. */
83 toolDia = atof(cp);
85 if (toolDia <= 0) {
86 fprintf(stderr, "*** WARNING: Tool T%02d diameter is impossible.\n", toolNumber);
87 return;
89 if (toolDia < 0.001) {
90 fprintf(stderr, "*** WARNING: Tool T%02d diameter is very small - "
91 "are you sure?\n", toolNumber);
94 if (tools[toolNumber] != 0) {
95 fprintf(stderr, "*** ERROR: Tool T%02d is already defined.\n", toolNumber);
96 fprintf(stderr, "*** Exiting because this is a HOLD error at any board house.\n");
97 exit(1);
98 return;
101 tools[toolNumber] = toolDia;
102 } /* ProcessToolLine */
105 int
106 gerbv_process_tools_file(const char *tf)
108 FILE *f;
109 char buf[80];
111 have_tools_file = 0;
112 memset(tools, 0, sizeof(tools));
114 if (tf == NULL)
115 return 0;
117 f = fopen(tf, "r");
118 if (f == NULL) {
119 fprintf(stderr, "*** ERROR: Failed to open file \"%s\" to read.\n", tf);
120 return 0;
122 while (!feof(f)) {
123 memset(buf, 0, sizeof(buf));
124 if (NULL == fgets(buf, sizeof(buf)-1, f))
125 break;
126 ProcessToolLine(buf);
128 fclose(f);
129 have_tools_file = 1;
130 return 1;
131 } /* gerbv_process_tools_file */
134 double
135 gerbv_get_tool_diameter(int toolNumber)
137 if (!have_tools_file)
138 return 0;
139 if ((toolNumber < MIN_TOOL_NUMBER) || (toolNumber > MAX_TOOL_NUMBER))
140 return 0;
141 return tools[toolNumber];
142 } /* gerbv_get_tool_diameter */