gerbv  2.6A
gerb_stats.c
Go to the documentation of this file.
1 /*
2  * gEDA - GNU Electronic Design Automation
3  * gerbv_stats.c -- a part of gerbv.
4  *
5  * Copyright (C) Stuart Brorson (sdb@cloud9.net)
6  *
7  * $Id$
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22  */
23 
29 #include "gerbv.h"
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
34 
35 #include "common.h"
36 #include "gerb_stats.h"
37 
38 #define dprintf if(DEBUG) printf
39 
40 /* ------------------------------------------------------- */
45 
46  gerbv_stats_t *stats;
48  gerbv_aperture_list_t *aperture_list;
49  gerbv_aperture_list_t *D_code_list;
50 
51  /* Malloc space for new stats struct. Return NULL if error. */
52  if ((stats = (gerbv_stats_t *)g_malloc(sizeof(gerbv_stats_t))) == NULL) {
53  return NULL;
54  }
55 
56  /* Set new stats struct to zero */
57  memset((void *)stats, 0, sizeof(gerbv_stats_t));
58 
59  /* Initialize error list */
60  error_list = gerbv_stats_new_error_list();
61  if (error_list == NULL)
62  GERB_FATAL_ERROR(_("malloc error_list failed"));
63  stats->error_list = (gerbv_error_list_t *) error_list;
64 
65  /* Initialize aperture list */
66  aperture_list = gerbv_stats_new_aperture_list();
67  if (aperture_list == NULL)
68  GERB_FATAL_ERROR(_("malloc aperture_list failed"));
69  stats->aperture_list = (gerbv_aperture_list_t *) aperture_list;
70 
71  /* Initialize D codes list */
72  D_code_list = gerbv_stats_new_aperture_list();
73  if (D_code_list == NULL)
74  GERB_FATAL_ERROR(_("malloc D_code_list failed"));
75  stats->D_code_list = (gerbv_aperture_list_t *) D_code_list;
76 
77  return stats;
78 }
79 
80 void
81 gerbv_destroy_error_list (gerbv_error_list_t *errorList) {
82  gerbv_error_list_t *nextError=errorList,*tempError;
83 
84  while (nextError) {
85  tempError = nextError->next;
86  g_free (nextError->error_text);
87  g_free (nextError);
88  nextError = tempError;
89  }
90 }
91 
92 void
93 gerbv_destroy_aperture_list (gerbv_aperture_list_t *apertureList) {
94  gerbv_aperture_list_t *nextAperture=apertureList,*tempAperture;
95 
96  while (nextAperture) {
97  tempAperture = nextAperture->next;
98  g_free (nextAperture);
99  nextAperture = tempAperture;
100  }
101 }
102 
103 /* ------------------------------------------------------- */
104 void
106  if (stats == NULL)
107  return;
108  gerbv_destroy_error_list (stats->error_list);
109  gerbv_destroy_aperture_list (stats->aperture_list);
110  gerbv_destroy_aperture_list (stats->D_code_list);
111  g_free (stats);
112 }
113 
114 
115 /* ------------------------------------------------------- */
122 void
124  gerbv_stats_t *input_stats,
125  int this_layer) {
126 
127  dprintf("---> Entering gerbv_stats_add_layer ... \n");
128 
129  gerbv_error_list_t *error;
130  gerbv_aperture_list_t *aperture;
131  gerbv_aperture_list_t *D_code;
132 
133  accum_stats->layer_count++;
134  accum_stats->G0 += input_stats->G0;
135  accum_stats->G1 += input_stats->G1;
136  accum_stats->G2 += input_stats->G2;
137  accum_stats->G3 += input_stats->G3;
138  accum_stats->G4 += input_stats->G4;
139  accum_stats->G10 += input_stats->G10;
140  accum_stats->G11 += input_stats->G11;
141  accum_stats->G12 += input_stats->G12;
142  accum_stats->G36 += input_stats->G36;
143  accum_stats->G37 += input_stats->G37;
144  accum_stats->G54 += input_stats->G54;
145  accum_stats->G55 += input_stats->G55;
146  accum_stats->G70 += input_stats->G70;
147  accum_stats->G71 += input_stats->G71;
148  accum_stats->G74 += input_stats->G74;
149  accum_stats->G75 += input_stats->G75;
150  accum_stats->G90 += input_stats->G90;
151  accum_stats->G91 += input_stats->G91;
152  accum_stats->G_unknown += input_stats->G_unknown;
153 
154  accum_stats->D1 += input_stats->D1;
155  accum_stats->D2 += input_stats->D2;
156  accum_stats->D3 += input_stats->D3;
157  /* Create list of user-defined D codes from aperture list */
158  for (D_code = input_stats->D_code_list;
159  D_code != NULL;
160  D_code = D_code->next) {
161  if (D_code->number != -1) {
162  dprintf(" .... In gerbv_stats_add_layer, D code section, adding number = %d to accum_stats D list ...\n",
163  D_code->number);
164  gerbv_stats_add_to_D_list(accum_stats->D_code_list,
165  D_code->number);
166  dprintf(" .... In gerbv_stats_add_layer, D code section, calling increment_D_count with count %d ...\n",
167  D_code->count);
168  gerbv_stats_increment_D_list_count(accum_stats->D_code_list,
169  D_code->number,
170  D_code->count,
171  accum_stats->error_list);
172  }
173  }
174  accum_stats->D_unknown += input_stats->D_unknown;
175  accum_stats->D_error += input_stats->D_error;
176 
177  accum_stats->M0 += input_stats->M0;
178  accum_stats->M1 += input_stats->M1;
179  accum_stats->M2 += input_stats->M2;
180  accum_stats->M_unknown += input_stats->M_unknown;
181 
182  accum_stats->X += input_stats->X;
183  accum_stats->Y += input_stats->Y;
184  accum_stats->I += input_stats->I;
185  accum_stats->J += input_stats->J;
186 
187  accum_stats->star += input_stats->star;
188  accum_stats->unknown += input_stats->unknown;
189 
190  /* ==== Now deal with the error list ==== */
191  for (error = input_stats->error_list;
192  error != NULL;
193  error = error->next) {
194  if (error->error_text != NULL) {
195  gerbv_stats_add_error(accum_stats->error_list,
196  this_layer,
197  error->error_text,
198  error->type);
199  }
200  }
201 
202  /* ==== Now deal with the aperture list ==== */
203  for (aperture = input_stats->aperture_list;
204  aperture != NULL;
205  aperture = aperture->next) {
206  if (aperture->number != -1) {
207  gerbv_stats_add_aperture(accum_stats->aperture_list,
208  this_layer,
209  aperture->number,
210  aperture->type,
211  aperture->parameter);
212  }
213  }
214 
215  dprintf("<---- .... Leaving gerbv_stats_add_layer. \n");
216 
217  return;
218 }
219 
220 /* ------------------------------------------------------- */
222 gerbv_stats_new_error_list() {
224 
225  /* Malloc space for new error_list struct. Return NULL if error. */
226  if ((error_list = (gerbv_error_list_t *)g_malloc(sizeof(gerbv_error_list_t))) == NULL) {
227  return NULL;
228  }
229 
230  error_list->layer = -1;
231  error_list->error_text = NULL;
232  error_list->next = NULL;
233  return error_list;
234 }
235 
236 
237 /* ------------------------------------------------------- */
238 void
239 gerbv_stats_add_error(gerbv_error_list_t *error_list_in,
240  int layer, const char *error_text,
241  gerbv_message_type_t type) {
242 
243  gerbv_error_list_t *error_list_new;
244  gerbv_error_list_t *error_last = NULL;
245  gerbv_error_list_t *error;
246 
247  /* Replace embedded error messages */
248  switch (type) {
249  case GERBV_MESSAGE_FATAL:
250  GERB_FATAL_ERROR("%s",error_text);
251  break;
252  case GERBV_MESSAGE_ERROR:
253  GERB_COMPILE_ERROR("%s",error_text);
254  break;
256  GERB_COMPILE_WARNING("%s",error_text);
257  break;
258  case GERBV_MESSAGE_NOTE:
259  break;
260  }
261 
262  /* First handle case where this is the first list element */
263  if (error_list_in->error_text == NULL) {
264  error_list_in->layer = layer;
265  error_list_in->error_text = g_strdup_printf("%s", error_text);
266  error_list_in->type = type;
267  error_list_in->next = NULL;
268  return;
269  }
270 
271  /* Next check to see if this error is already in the list */
272  for(error = error_list_in; error != NULL; error = error->next) {
273  if ((strcmp(error->error_text, error_text) == 0) &&
274  (error->layer == layer) ) {
275  return; /* This error text is already in the error list */
276  }
277  error_last = error; /* point to last element in error list */
278  }
279  /* This error text is unique. Therefore, add it to the list */
280 
281  /* Now malloc space for new error list element */
282  error_list_new = (gerbv_error_list_t *) g_malloc(sizeof(gerbv_error_list_t));
283  if (error_list_new == NULL) {
284  GERB_FATAL_ERROR(_("malloc error_list failed"));
285  }
286 
287  /* Set member elements */
288  error_list_new->layer = layer;
289  error_list_new->error_text = g_strdup_printf("%s", error_text);
290  error_list_new->type = type;
291  error_list_new->next = NULL;
292  error_last->next = error_list_new;
293 
294  return;
295 }
296 
297 /* ------------------------------------------------------- */
298 gerbv_aperture_list_t *
299 gerbv_stats_new_aperture_list() {
300  gerbv_aperture_list_t *aperture_list;
301  int i;
302 
303  dprintf("Mallocing new gerb aperture list\n");
304  /* Malloc space for new aperture_list struct. Return NULL if error. */
305  if ((aperture_list = (gerbv_aperture_list_t *)g_malloc(sizeof(gerbv_aperture_list_t)))
306  == NULL) {
307  dprintf("malloc new gerb aperture list failed\n");
308  return NULL;
309  }
310 
311  dprintf(" Placing values in certain structs.\n");
312  aperture_list->number = -1;
313  aperture_list->count = 0;
314  aperture_list->type = 0;
315  for (i = 0; i<5; i++) {
316  aperture_list->parameter[i] = 0.0;
317  }
318  aperture_list->next = NULL;
319  return aperture_list;
320 }
321 
322 
323 /* ------------------------------------------------------- */
324 void
325 gerbv_stats_add_aperture(gerbv_aperture_list_t *aperture_list_in,
326  int layer, int number, gerbv_aperture_type_t type,
327  double parameter[5]) {
328 
329  gerbv_aperture_list_t *aperture_list_new;
330  gerbv_aperture_list_t *aperture_last = NULL;
331  gerbv_aperture_list_t *aperture;
332  int i;
333 
334  dprintf(" ---> Entering gerbv_stats_add_aperture ....\n");
335 
336  /* First handle case where this is the first list element */
337  if (aperture_list_in->number == -1) {
338  dprintf(" .... Adding first aperture to aperture list ... \n");
339  dprintf(" .... Aperture type = %d ... \n", type);
340  aperture_list_in->number = number;
341  aperture_list_in->type = type;
342  aperture_list_in->layer = layer;
343  for(i=0; i<5; i++) {
344  aperture_list_in->parameter[i] = parameter[i];
345  }
346  aperture_list_in->next = NULL;
347  dprintf(" <--- .... Leaving gerbv_stats_add_aperture.\n");
348  return;
349  }
350 
351  /* Next check to see if this aperture is already in the list */
352  for(aperture = aperture_list_in;
353  aperture != NULL;
354  aperture = aperture->next) {
355  if ((aperture->number == number) &&
356  (aperture->layer == layer) ) {
357  dprintf(" .... This aperture is already in the list ... \n");
358  dprintf(" <--- .... Leaving gerbv_stats_add_aperture.\n");
359  return;
360  }
361  aperture_last = aperture; /* point to last element in list */
362  }
363  /* This aperture number is unique. Therefore, add it to the list */
364  dprintf(" .... Adding another aperture to list ... \n");
365  dprintf(" .... Aperture type = %d ... \n", type);
366 
367  /* Now malloc space for new aperture list element */
368  aperture_list_new = (gerbv_aperture_list_t *) g_malloc(sizeof(gerbv_aperture_list_t));
369  if (aperture_list_new == NULL) {
370  GERB_FATAL_ERROR(_("malloc aperture_list failed"));
371  }
372 
373  /* Set member elements */
374  aperture_list_new->layer = layer;
375  aperture_list_new->number = number;
376  aperture_list_new->type = type;
377  aperture_list_new->next = NULL;
378  for(i=0; i<5; i++) {
379  aperture_list_new->parameter[i] = parameter[i];
380  }
381  aperture_last->next = aperture_list_new;
382 
383  dprintf(" <--- .... Leaving gerbv_stats_add_aperture.\n");
384 
385  return;
386 }
387 
388 /* ------------------------------------------------------- */
389 void
390 gerbv_stats_add_to_D_list(gerbv_aperture_list_t *D_list_in,
391  int number) {
392 
393  gerbv_aperture_list_t *D_list;
394  gerbv_aperture_list_t *D_list_last=NULL;
395  gerbv_aperture_list_t *D_list_new;
396 
397  dprintf(" ----> Entering add_to_D_list, numbr = %d\n", number);
398 
399  /* First handle case where this is the first list element */
400  if (D_list_in->number == -1) {
401  dprintf(" .... Adding first D code to D code list ... \n");
402  dprintf(" .... Aperture number = %d ... \n", number);
403  D_list_in->number = number;
404  D_list_in->count = 0;
405  D_list_in->next = NULL;
406  dprintf(" <--- .... Leaving add_to_D_list.\n");
407  return;
408  }
409 
410  /* Look to see if this is already in list */
411  for(D_list = D_list_in;
412  D_list != NULL;
413  D_list = D_list->next) {
414  if (D_list->number == number) {
415  dprintf(" .... Found in D list .... \n");
416  dprintf(" <--- .... Leaving add_to_D_list.\n");
417  return;
418  }
419  D_list_last = D_list; /* point to last element in list */
420  }
421 
422  /* This aperture number is unique. Therefore, add it to the list */
423  dprintf(" .... Adding another D code to D code list ... \n");
424 
425  /* Malloc space for new aperture list element */
426  D_list_new = (gerbv_aperture_list_t *) g_malloc(sizeof(gerbv_aperture_list_t));
427  if (D_list_new == NULL) {
428  GERB_FATAL_ERROR(_("malloc D_list failed"));
429  }
430 
431  /* Set member elements */
432  D_list_new->number = number;
433  D_list_new->count = 0;
434  D_list_new->next = NULL;
435  D_list_last->next = D_list_new;
436 
437  dprintf(" <--- .... Leaving add_to_D_list.\n");
438 
439  return;
440 }
441 
442 /* ------------------------------------------------------- */
443 int
444 gerbv_stats_increment_D_list_count(gerbv_aperture_list_t *D_list_in,
445  int number,
446  int count,
447  gerbv_error_list_t *error) {
448 
449  gerbv_aperture_list_t *D_list;
450 
451  dprintf(" Entering inc_D_list_count, code = D%d, input count to add = %d\n", number, count);
452 
453  /* Find D code in list and increment it */
454  for(D_list = D_list_in;
455  D_list != NULL;
456  D_list = D_list->next) {
457  if (D_list->number == number) {
458  dprintf(" old count = %d\n", D_list->count);
459  D_list->count += count; /* Add to this aperture count, then return */
460  dprintf(" updated count = %d\n", D_list->count);
461  return 0; /* Return 0 for success */
462  }
463  }
464 
465  /* This D number is not defined. Therefore, flag error */
466  dprintf(" .... Didn't find this D code in defined list .... \n");
467  dprintf(" <--- .... Leaving inc_D_list_count.\n");
468  gerbv_stats_add_error(error,
469  -1,
470  _("Undefined aperture number called out in D code"),
472  return -1; /* Return -1 for failure */
473 }
474