tooltable.c

Go to the documentation of this file.
00001 /*
00002  * tooltable.c
00003  * Copyright (C) 2004 dmitri (at) users.sourceforge.net
00004  *
00005  * $Id$
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
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       /* T01 */
00033 #define MAX_TOOL_NUMBER         99      /* T99 */
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     /* Skip leading spaces if there are some */
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; /* Skip Tnn */
00075 
00076     /* Skip following spaces if there are some */
00077     while (isspace((int) *cp)) {
00078         if (*(++cp) == '\0')
00079             return;
00080     }
00081 
00082     /* The rest of the line is supposed to be the tool diameter in inches. */
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 } /* ProcessToolLine */
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 } /* gerbv_process_tools_file */
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 } /* gerbv_get_tool_diameter */

Generated on Tue Aug 19 00:14:50 2008 for gerbv by  doxygen 1.5.6