gerb_stats.c

Go to the documentation of this file.
00001 /*
00002  * gEDA - GNU Electronic Design Automation
00003  *   gerbv_stats.c -- a part of gerbv.
00004  *
00005  *   Copyright (C) Stuart Brorson (sdb@cloud9.net)
00006  *
00007  * $Id$
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
00022  */
00023 
00029 #ifdef HAVE_CONFIG_H
00030 #include "config.h"
00031 #endif
00032 
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <glib.h>
00036 #include <math.h>
00037 
00038 #include "gerbv.h"
00039 #include "gerb_stats.h"
00040 
00041 
00042 /* DEBUG printing.  #define DEBUG 1 in config.h to use this fcn. */
00043 #define dprintf if(DEBUG) printf
00044 
00045 /* ------------------------------------------------------- */
00048 gerbv_stats_t *
00049 gerbv_stats_new(void) {
00050 
00051     gerbv_stats_t *stats;
00052     gerbv_error_list_t *error_list;
00053     gerbv_aperture_list_t *aperture_list;
00054     gerbv_aperture_list_t *D_code_list;
00055 
00056     /* Malloc space for new stats struct.  Return NULL if error. */
00057     if ((stats = (gerbv_stats_t *)g_malloc(sizeof(gerbv_stats_t))) == NULL) {
00058         return NULL;
00059     }
00060 
00061     /* Set new stats struct to zero */
00062     memset((void *)stats, 0, sizeof(gerbv_stats_t));
00063 
00064     /* Initialize error list */
00065     error_list = gerbv_stats_new_error_list();
00066     if (error_list == NULL)
00067         GERB_FATAL_ERROR("malloc error_list failed\n");
00068     stats->error_list = (gerbv_error_list_t *) error_list;
00069 
00070     /* Initialize aperture list */
00071     aperture_list = gerbv_stats_new_aperture_list();
00072     if (aperture_list == NULL)
00073         GERB_FATAL_ERROR("malloc aperture_list failed\n");
00074     stats->aperture_list = (gerbv_aperture_list_t *) aperture_list;
00075 
00076     /* Initialize D codes list */
00077     D_code_list = gerbv_stats_new_aperture_list();
00078     if (D_code_list == NULL)
00079         GERB_FATAL_ERROR("malloc D_code_list failed\n");
00080     stats->D_code_list = (gerbv_aperture_list_t *) D_code_list;
00081 
00082     return stats;
00083 }
00084 
00085 
00086 /* ------------------------------------------------------- */
00093 void
00094 gerbv_stats_add_layer(gerbv_stats_t *accum_stats, 
00095                    gerbv_stats_t *input_stats,
00096                    int this_layer) {
00097     
00098     dprintf("---> Entering gerbv_stats_add_layer ... \n");
00099 
00100     gerbv_error_list_t *error;
00101     gerbv_aperture_list_t *aperture;
00102     gerbv_aperture_list_t *D_code;
00103 
00104     accum_stats->layer_count++;
00105     accum_stats->G0 += input_stats->G0;
00106     accum_stats->G1 += input_stats->G1;
00107     accum_stats->G2 += input_stats->G2;
00108     accum_stats->G3 += input_stats->G3;
00109     accum_stats->G4 += input_stats->G4;
00110     accum_stats->G10 += input_stats->G10;
00111     accum_stats->G11 += input_stats->G11;
00112     accum_stats->G12 += input_stats->G12;
00113     accum_stats->G36 += input_stats->G36;
00114     accum_stats->G37 += input_stats->G37;
00115     accum_stats->G54 += input_stats->G54;
00116     accum_stats->G55 += input_stats->G55;
00117     accum_stats->G70 += input_stats->G70;
00118     accum_stats->G71 += input_stats->G71;
00119     accum_stats->G74 += input_stats->G74;
00120     accum_stats->G75 += input_stats->G75;
00121     accum_stats->G90 += input_stats->G90;
00122     accum_stats->G91 += input_stats->G91;
00123     accum_stats->G_unknown += input_stats->G_unknown;
00124 
00125     accum_stats->D1 += input_stats->D1;
00126     accum_stats->D2 += input_stats->D2;
00127     accum_stats->D3 += input_stats->D3;
00128     /* Create list of user-defined D codes from aperture list */
00129     for (D_code = input_stats->D_code_list;
00130          D_code != NULL;
00131          D_code = D_code->next) {
00132         if (D_code->number != -1) {
00133          dprintf("     .... In gerbv_stats_add_layer, D code section, adding number = %d to accum_stats D list ...\n",
00134                 D_code->number);
00135          gerbv_stats_add_to_D_list(accum_stats->D_code_list,
00136                                D_code->number);
00137          dprintf("     .... In gerbv_stats_add_layer, D code section, calling increment_D_count with count %d ...\n", 
00138                 D_code->count);
00139          gerbv_stats_increment_D_list_count(accum_stats->D_code_list,
00140                                        D_code->number,
00141                                        D_code->count,
00142                                        accum_stats->error_list);
00143       }
00144     }
00145     accum_stats->D_unknown += input_stats->D_unknown;
00146     accum_stats->D_error += input_stats->D_error;
00147 
00148     accum_stats->M0 += input_stats->M0;
00149     accum_stats->M1 += input_stats->M1;
00150     accum_stats->M2 += input_stats->M2;
00151     accum_stats->M_unknown += input_stats->M_unknown;
00152 
00153     accum_stats->X += input_stats->X;
00154     accum_stats->Y += input_stats->Y;
00155     accum_stats->I += input_stats->I;
00156     accum_stats->J += input_stats->J;
00157 
00158     accum_stats->star += input_stats->star;
00159     accum_stats->unknown += input_stats->unknown;
00160 
00161     /* ==== Now deal with the error list ==== */
00162     for (error = input_stats->error_list;
00163          error != NULL;
00164          error = error->next) {
00165         if (error->error_text != NULL) {
00166             gerbv_stats_add_error(accum_stats->error_list,
00167                                   this_layer,
00168                                   error->error_text,
00169                                   error->type);
00170         }
00171     }
00172 
00173     /* ==== Now deal with the aperture list ==== */
00174     for (aperture = input_stats->aperture_list;
00175          aperture != NULL;
00176          aperture = aperture->next) {
00177         if (aperture->number != -1) {
00178             gerbv_stats_add_aperture(accum_stats->aperture_list,
00179                                 this_layer,
00180                                 aperture->number,
00181                                 aperture->type,
00182                                 aperture->parameter);
00183         }
00184     }
00185 
00186     dprintf("<---- .... Leaving gerbv_stats_add_layer. \n");
00187 
00188     return;
00189 }
00190 
00191 /* ------------------------------------------------------- */
00192 gerbv_error_list_t *
00193 gerbv_stats_new_error_list() {
00194     gerbv_error_list_t *error_list;
00195 
00196     /* Malloc space for new error_list struct.  Return NULL if error. */
00197     if ((error_list = (gerbv_error_list_t *)g_malloc(sizeof(gerbv_error_list_t))) == NULL) {
00198         return NULL;
00199     }
00200 
00201     error_list->layer = -1;
00202     error_list->error_text = NULL;
00203     error_list->next = NULL;
00204     return error_list;
00205 }
00206 
00207 
00208 /* ------------------------------------------------------- */
00209 void
00210 gerbv_stats_add_error(gerbv_error_list_t *error_list_in,
00211                       int layer, const char *error_text,
00212                       gerbv_message_type_t type) {
00213 
00214     gerbv_error_list_t *error_list_new;
00215     gerbv_error_list_t *error_last = NULL;
00216     gerbv_error_list_t *error;
00217 
00218     /* Replace embedded error messages */
00219     switch (type) {
00220         case GERBV_MESSAGE_FATAL:
00221             GERB_FATAL_ERROR(error_text);
00222             break;
00223         case GERBV_MESSAGE_ERROR:
00224             GERB_COMPILE_ERROR(error_text);
00225             break;
00226         case GERBV_MESSAGE_WARNING:
00227             GERB_COMPILE_WARNING(error_text);
00228             break;
00229         case GERBV_MESSAGE_NOTE:
00230             break;
00231     }
00232 
00233     /* First handle case where this is the first list element */
00234     if (error_list_in->error_text == NULL) {
00235         error_list_in->layer = layer;
00236         error_list_in->error_text = g_strdup_printf("%s", error_text);
00237         error_list_in->type = type;
00238         error_list_in->next = NULL;
00239         return;
00240     }
00241 
00242     /* Next check to see if this error is already in the list */
00243     for(error = error_list_in; error != NULL; error = error->next) {
00244         if ((strcmp(error->error_text, error_text) == 0) &&
00245             (error->layer == layer) ) {
00246             return;  /* This error text is already in the error list */
00247         }
00248         error_last = error;  /* point to last element in error list */
00249     }
00250     /* This error text is unique.  Therefore, add it to the list */
00251 
00252     /* Now malloc space for new error list element */
00253     error_list_new = (gerbv_error_list_t *) g_malloc(sizeof(gerbv_error_list_t));
00254     if (error_list_new == NULL) {
00255         GERB_FATAL_ERROR("malloc error_list failed\n");
00256     }
00257 
00258     /* Set member elements */
00259     error_list_new->layer = layer;
00260     error_list_new->error_text = g_strdup_printf("%s", error_text);
00261     error_list_new->type = type;
00262     error_list_new->next = NULL;
00263     error_last->next = error_list_new;
00264 
00265     return;
00266 }
00267 
00268 /* ------------------------------------------------------- */
00269 gerbv_aperture_list_t *
00270 gerbv_stats_new_aperture_list() {
00271     gerbv_aperture_list_t *aperture_list;
00272     int i;
00273 
00274     dprintf("Mallocing new gerb aperture list\n");
00275     /* Malloc space for new aperture_list struct.  Return NULL if error. */
00276     if ((aperture_list = (gerbv_aperture_list_t *)g_malloc(sizeof(gerbv_aperture_list_t))) 
00277         == NULL) {
00278         return NULL;
00279     }
00280 
00281     dprintf("   Placing values in certain structs.\n");
00282     aperture_list->number = -1;
00283     aperture_list->count = 0;
00284     aperture_list->type = 0;
00285     for (i = 0; i<5; i++) {
00286        aperture_list->parameter[i] = 0.0;
00287     }
00288     aperture_list->next = NULL;
00289     return aperture_list;
00290 }
00291 
00292 
00293 /* ------------------------------------------------------- */
00294 void
00295 gerbv_stats_add_aperture(gerbv_aperture_list_t *aperture_list_in,
00296                      int layer, int number, gerbv_aperture_type_t type,
00297                      double parameter[5]) {
00298 
00299     gerbv_aperture_list_t *aperture_list_new;
00300     gerbv_aperture_list_t *aperture_last = NULL;
00301     gerbv_aperture_list_t *aperture;
00302     int i;
00303 
00304     dprintf("   --->  Entering gerbv_stats_add_aperture ....\n"); 
00305 
00306     /* First handle case where this is the first list element */
00307     if (aperture_list_in->number == -1) {
00308        dprintf("     .... Adding first aperture to aperture list ... \n"); 
00309        dprintf("     .... Aperture type = %d ... \n", type); 
00310         aperture_list_in->number = number;
00311         aperture_list_in->type = type;
00312        aperture_list_in->layer = layer;
00313        for(i=0; i<5; i++) { 
00314            aperture_list_in->parameter[i] = parameter[i];
00315        }
00316         aperture_list_in->next = NULL;
00317        dprintf("   <---  .... Leaving gerbv_stats_add_aperture.\n"); 
00318         return;
00319     }
00320 
00321     /* Next check to see if this aperture is already in the list */
00322     for(aperture = aperture_list_in; 
00323        aperture != NULL; 
00324        aperture = aperture->next) {
00325         if ((aperture->number == number) &&
00326             (aperture->layer == layer) ) {
00327          dprintf("     .... This aperture is already in the list ... \n"); 
00328            dprintf("   <---  .... Leaving gerbv_stats_add_aperture.\n"); 
00329             return;  
00330         }
00331         aperture_last = aperture;  /* point to last element in list */
00332     }
00333     /* This aperture number is unique.  Therefore, add it to the list */
00334     dprintf("     .... Adding another aperture to list ... \n"); 
00335     dprintf("     .... Aperture type = %d ... \n", type); 
00336        
00337     /* Now malloc space for new aperture list element */
00338     aperture_list_new = (gerbv_aperture_list_t *) g_malloc(sizeof(gerbv_aperture_list_t));
00339     if (aperture_list_new == NULL) {
00340         GERB_FATAL_ERROR("malloc aperture_list failed\n");
00341     }
00342 
00343     /* Set member elements */
00344     aperture_list_new->layer = layer;
00345     aperture_list_new->number = number;
00346     aperture_list_new->type = type;
00347     aperture_list_new->next = NULL;
00348     for(i=0; i<5; i++) { 
00349        aperture_list_new->parameter[i] = parameter[i];
00350     }
00351     aperture_last->next = aperture_list_new;
00352 
00353     dprintf("   <---  .... Leaving gerbv_stats_add_aperture.\n"); 
00354 
00355     return;
00356 }
00357 
00358 /* ------------------------------------------------------- */
00359 void
00360 gerbv_stats_add_to_D_list(gerbv_aperture_list_t *D_list_in,
00361                       int number) {
00362   
00363   gerbv_aperture_list_t *D_list;
00364   gerbv_aperture_list_t *D_list_last=NULL;
00365   gerbv_aperture_list_t *D_list_new;
00366 
00367     dprintf("   ----> Entering add_to_D_list, numbr = %d\n", number);
00368 
00369     /* First handle case where this is the first list element */
00370     if (D_list_in->number == -1) {
00371        dprintf("     .... Adding first D code to D code list ... \n"); 
00372        dprintf("     .... Aperture number = %d ... \n", number); 
00373         D_list_in->number = number;
00374        D_list_in->count = 0;
00375         D_list_in->next = NULL;
00376        dprintf("   <---  .... Leaving add_to_D_list.\n"); 
00377         return;
00378     }
00379 
00380     /* Look to see if this is already in list */
00381     for(D_list = D_list_in; 
00382        D_list != NULL; 
00383        D_list = D_list->next) {
00384         if (D_list->number == number) {
00385            dprintf("    .... Found in D list .... \n");
00386            dprintf("   <---  .... Leaving add_to_D_list.\n"); 
00387             return;  
00388         }
00389         D_list_last = D_list;  /* point to last element in list */
00390     }
00391 
00392     /* This aperture number is unique.  Therefore, add it to the list */
00393     dprintf("     .... Adding another D code to D code list ... \n"); 
00394        
00395     /* Malloc space for new aperture list element */
00396     D_list_new = (gerbv_aperture_list_t *) g_malloc(sizeof(gerbv_aperture_list_t));
00397     if (D_list_new == NULL) {
00398         GERB_FATAL_ERROR("malloc D_list failed\n");
00399     }
00400 
00401     /* Set member elements */
00402     D_list_new->number = number;
00403     D_list_new->count = 0;
00404     D_list_new->next = NULL;
00405     D_list_last->next = D_list_new;
00406 
00407     dprintf("   <---  .... Leaving add_to_D_list.\n"); 
00408 
00409     return;
00410 }
00411 
00412 /* ------------------------------------------------------- */
00413 int 
00414 gerbv_stats_increment_D_list_count(gerbv_aperture_list_t *D_list_in,
00415                                 int number, 
00416                                 int count,
00417                                 gerbv_error_list_t *error) {
00418   
00419     gerbv_aperture_list_t *D_list;
00420 
00421     dprintf("   Entering inc_D_list_count, code = D%d, input count to add = %d\n", number, count);
00422 
00423     /* Find D code in list and increment it */
00424     for(D_list = D_list_in; 
00425        D_list != NULL; 
00426        D_list = D_list->next) {
00427         if (D_list->number == number) {
00428            dprintf("    old count = %d\n", D_list->count);
00429            D_list->count += count;  /* Add to this aperture count, then return */
00430            dprintf("    updated count = %d\n", D_list->count);
00431             return 0;  /* Return 0 for success */  
00432         }
00433     }
00434 
00435     /* This D number is not defined.  Therefore, flag error */
00436     dprintf("    .... Didn't find this D code in defined list .... \n");
00437     dprintf("   <---  .... Leaving inc_D_list_count.\n"); 
00438     gerbv_stats_add_error(error,
00439                       -1,
00440                       "Undefined aperture number called out in D code.\n",
00441                       GERBV_MESSAGE_ERROR);
00442     return -1;  /* Return -1 for failure */
00443 }
00444 

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