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