tooltable.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <ctype.h>
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031
00032 #define MIN_TOOL_NUMBER 1
00033 #define MAX_TOOL_NUMBER 99
00034
00035 static int have_tools_file = 0;
00036 static double tools[1+MAX_TOOL_NUMBER];
00037
00038 static void
00039 ProcessToolLine(const char *cp)
00040 {
00041 const char *cp0 = cp;
00042 int toolNumber;
00043 double toolDia;
00044
00045 if (cp == NULL)
00046 return;
00047
00048
00049 while (isspace((int) *cp)) {
00050 if (*(++cp) == '\0')
00051 return;
00052 }
00053
00054 if (*cp != 'T') {
00055 fprintf(stderr, "*** WARNING: Strange tool \"%s\" ignored.\n", cp0);
00056 return;
00057 }
00058 if ((!isdigit((int) cp[1])) || (!isdigit((int) cp[2]))) {
00059 fprintf(stderr, "*** WARNING: No tool number in \"%s\".\n", cp0);
00060 return;
00061 }
00062 do {
00063 char tnb[3];
00064 tnb[0] = cp[1];
00065 tnb[1] = cp[2];
00066 tnb[2] = '\0';
00067 toolNumber = atoi(tnb);
00068 if ((toolNumber < MIN_TOOL_NUMBER) || (toolNumber > MAX_TOOL_NUMBER)) {
00069 fprintf(stderr, "*** WARNING: Can't parse tool number in \"%s\".\n", cp0);
00070 return;
00071 }
00072 } while (0);
00073
00074 cp += 3;
00075
00076
00077 while (isspace((int) *cp)) {
00078 if (*(++cp) == '\0')
00079 return;
00080 }
00081
00082
00083 toolDia = atof(cp);
00084
00085 if (toolDia <= 0) {
00086 fprintf(stderr, "*** WARNING: Tool T%02d diameter is impossible.\n", toolNumber);
00087 return;
00088 }
00089 if (toolDia < 0.001) {
00090 fprintf(stderr, "*** WARNING: Tool T%02d diameter is very small - "
00091 "are you sure?\n", toolNumber);
00092 }
00093
00094 if (tools[toolNumber] != 0) {
00095 fprintf(stderr, "*** ERROR: Tool T%02d is already defined.\n", toolNumber);
00096 fprintf(stderr, "*** Exiting because this is a HOLD error at any board house.\n");
00097 exit(1);
00098 return;
00099 }
00100
00101 tools[toolNumber] = toolDia;
00102 }
00103
00104
00105 int
00106 gerbv_process_tools_file(const char *tf)
00107 {
00108 FILE *f;
00109 char buf[80];
00110
00111 have_tools_file = 0;
00112 memset(tools, 0, sizeof(tools));
00113
00114 if (tf == NULL)
00115 return 0;
00116
00117 f = fopen(tf, "r");
00118 if (f == NULL) {
00119 fprintf(stderr, "*** ERROR: Failed to open file \"%s\" to read.\n", tf);
00120 return 0;
00121 }
00122 while (!feof(f)) {
00123 memset(buf, 0, sizeof(buf));
00124 if (NULL == fgets(buf, sizeof(buf)-1, f))
00125 break;
00126 ProcessToolLine(buf);
00127 }
00128 fclose(f);
00129 have_tools_file = 1;
00130 return 1;
00131 }
00132
00133
00134 double
00135 gerbv_get_tool_diameter(int toolNumber)
00136 {
00137 if (!have_tools_file)
00138 return 0;
00139 if ((toolNumber < MIN_TOOL_NUMBER) || (toolNumber > MAX_TOOL_NUMBER))
00140 return 0;
00141 return tools[toolNumber];
00142 }