gerbv  2.6A
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 
34 #define MIN_TOOL_NUMBER 1 /* T01 */
35 #define MAX_TOOL_NUMBER 99 /* T99 */
36 
37 static int have_tools_file = 0;
38 static double tools[1+MAX_TOOL_NUMBER];
39 
40 static void
41 ProcessToolLine(const char *cp)
42 {
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  fprintf(stderr, _("*** WARNING: Strange tool \"%s\" ignored.\n"), cp0);
58  return;
59  }
60  if ((!isdigit((int) cp[1])) || (!isdigit((int) cp[2]))) {
61  fprintf(stderr, _("*** WARNING: No tool number in \"%s\".\n"), cp0);
62  return;
63  }
64  do {
65  char tnb[3];
66  tnb[0] = cp[1];
67  tnb[1] = cp[2];
68  tnb[2] = '\0';
69  toolNumber = atoi(tnb);
70  if ((toolNumber < MIN_TOOL_NUMBER) || (toolNumber > MAX_TOOL_NUMBER)) {
71  fprintf(stderr, _("*** WARNING: Can't parse tool number in \"%s\".\n"), cp0);
72  return;
73  }
74  } while (0);
75 
76  cp += 3; /* Skip Tnn */
77 
78  /* Skip following spaces if there are some */
79  while (isspace((int) *cp)) {
80  if (*(++cp) == '\0')
81  return;
82  }
83 
84  /* The rest of the line is supposed to be the tool diameter in inches. */
85  toolDia = atof(cp);
86 
87  if (toolDia <= 0) {
88  fprintf(stderr, _("*** WARNING: Tool T%02d diameter is impossible.\n"), toolNumber);
89  return;
90  }
91  if (toolDia < 0.001) {
92  fprintf(stderr, _("*** WARNING: Tool T%02d diameter is very small - "
93  "are you sure?\n"), toolNumber);
94  }
95 
96  if (tools[toolNumber] != 0) {
97  fprintf(stderr, _("*** ERROR: Tool T%02d is already defined.\n"), toolNumber);
98  fprintf(stderr, _("*** Exiting because this is a HOLD error at any board house.\n"));
99  exit(1);
100  return;
101  }
102 
103  tools[toolNumber] = toolDia;
104 } /* ProcessToolLine */
105 
106 
107 int
108 gerbv_process_tools_file(const char *tf)
109 {
110  FILE *f;
111  char buf[80];
112 
113  have_tools_file = 0;
114  memset(tools, 0, sizeof(tools));
115 
116  if (tf == NULL)
117  return 0;
118 
119  f = fopen(tf, "r");
120  if (f == NULL) {
121  fprintf(stderr, _("*** ERROR: Failed to open file \"%s\" to read.\n"), tf);
122  return 0;
123  }
124  while (!feof(f)) {
125  memset(buf, 0, sizeof(buf));
126  if (NULL == fgets(buf, sizeof(buf)-1, f))
127  break;
128  ProcessToolLine(buf);
129  }
130  fclose(f);
131  have_tools_file = 1;
132  return 1;
133 } /* gerbv_process_tools_file */
134 
135 
136 double
137 gerbv_get_tool_diameter(int toolNumber)
138 {
139  if (!have_tools_file)
140  return 0;
141  if ((toolNumber < MIN_TOOL_NUMBER) || (toolNumber > MAX_TOOL_NUMBER))
142  return 0;
143  return tools[toolNumber];
144 } /* gerbv_get_tool_diameter */