gerbv  2.10.1-dev~93f1b5
tooltable.c
Go to the documentation of this file.
1 /*
2  * tooltable.c
3  * Copyright (C) 2004 dmitri (at) users.sourceforge.net
4  *
5  * $Id$
6  *
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.
11  *
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.
16  *
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
20  */
21 
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include "common.h"
33 #include "gerbv.h"
34 
35 #define MIN_TOOL_NUMBER 1 /* T01 */
36 #define MAX_TOOL_NUMBER 99 /* T99 */
37 
38 static int have_tools_file = 0;
39 static double tools[1 + MAX_TOOL_NUMBER];
40 
41 static void
42 ProcessToolLine(const char* cp, const char* file_name, long int file_line) {
43  const char* cp0 = cp;
44  int toolNumber;
45  double toolDia;
46 
47  if (cp == NULL)
48  return;
49 
50  /* Skip leading spaces if there are some */
51  while (isspace((int)*cp)) {
52  if (*(++cp) == '\0')
53  return;
54  }
55 
56  if (*cp != 'T') {
57  GERB_COMPILE_WARNING(
58  _("Ignored strange tool \"%s\" "
59  "at line %ld in file \"%s\""),
60  cp0, file_line, file_name
61  );
62  return;
63  }
64  if ((!isdigit((int)cp[1])) || (!isdigit((int)cp[2]))) {
65  GERB_COMPILE_WARNING(
66  _("No tool number in \"%s\" "
67  "at line %ld in file \"%s\""),
68  cp0, file_line, file_name
69  );
70  return;
71  }
72  do {
73  char tnb[3];
74  tnb[0] = cp[1];
75  tnb[1] = cp[2];
76  tnb[2] = '\0';
77  toolNumber = atoi(tnb);
78  if ((toolNumber < MIN_TOOL_NUMBER) || (toolNumber > MAX_TOOL_NUMBER)) {
79  GERB_COMPILE_WARNING(
80  _("Can't parse tool number in \"%s\" "
81  "at line %ld in file \"%s\""),
82  cp0, file_line, file_name
83  );
84  return;
85  }
86  } while (0);
87 
88  cp += 3; /* Skip Tnn */
89 
90  /* Skip following spaces if there are some */
91  while (isspace((int)*cp)) {
92  if (*(++cp) == '\0')
93  return;
94  }
95 
96  /* The rest of the line is supposed to be the tool diameter in inches. */
97  toolDia = atof(cp);
98 
99  if (toolDia <= 0) {
100  GERB_COMPILE_ERROR(
101  _("Tool T%02d diameter is impossible "
102  "at line %ld in file \"%s\""),
103  toolNumber, file_line, file_name
104  );
105  return;
106  }
107  if (toolDia < 0.001) {
108  GERB_COMPILE_WARNING(
109  _("Tool T%02d diameter is very small "
110  "at line %ld in file \"%s\""),
111  toolNumber, file_line, file_name
112  );
113  }
114 
115  if (tools[toolNumber] != 0) {
116  GERB_COMPILE_ERROR(
117  _("Tool T%02d is already defined, occurred "
118  "at line %ld in file \"%s\""),
119  toolNumber, file_line, file_name
120  );
121  GERB_FATAL_ERROR(
122  _("Exiting because this is a HOLD error "
123  "at any board house.")
124  );
125  exit(1);
126  return;
127  }
128 
129  tools[toolNumber] = toolDia;
130 } /* ProcessToolLine */
131 
132 int
133 gerbv_process_tools_file(const char* tf) {
134  FILE* f;
135  char buf[80];
136  long int file_line = 0;
137 
138  have_tools_file = 0;
139  memset(tools, 0, sizeof(tools));
140 
141  if (tf == NULL)
142  return 0;
143 
144  f = fopen(tf, "r");
145  if (f == NULL) {
146  GERB_COMPILE_ERROR(_("Failed to open \"%s\" for reading"), tf);
147  return 0;
148  }
149  while (!feof(f)) {
150  memset(buf, 0, sizeof(buf));
151  if (NULL == fgets(buf, sizeof(buf) - 1, f))
152  break;
153 
154  file_line++;
155  ProcessToolLine(buf, tf, file_line);
156  }
157  fclose(f);
158  have_tools_file = 1;
159  return 1;
160 } /* gerbv_process_tools_file */
161 
162 double
163 gerbv_get_tool_diameter(int toolNumber) {
164  if (!have_tools_file)
165  return 0;
166  if ((toolNumber < MIN_TOOL_NUMBER) || (toolNumber > MAX_TOOL_NUMBER))
167  return 0;
168  return tools[toolNumber];
169 } /* gerbv_get_tool_diameter */
Contains basic defines.
The main header file for the libgerbv library.