gerbv  2.10.1-dev~93f1b5
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 \
39  if (DEBUG) \
40  printf
41 
42 /* ------------------------------------------------------- */
47 
48  gerbv_stats_t* stats;
50  gerbv_aperture_list_t* aperture_list;
51  gerbv_aperture_list_t* D_code_list;
52 
53  /* Malloc space for new stats struct. Return NULL if error. */
54  if (NULL == (stats = g_new0(gerbv_stats_t, 1))) {
55  return NULL;
56  }
57 
58  /* Initialize error list */
59  error_list = gerbv_stats_new_error_list();
60  if (error_list == NULL)
61  GERB_FATAL_ERROR("malloc error_list failed in %s()", __FUNCTION__);
62  stats->error_list = (gerbv_error_list_t*)error_list;
63 
64  /* Initialize aperture list */
65  aperture_list = gerbv_stats_new_aperture_list();
66  if (aperture_list == NULL)
67  GERB_FATAL_ERROR("malloc aperture_list failed in %s()", __FUNCTION__);
68  stats->aperture_list = (gerbv_aperture_list_t*)aperture_list;
69 
70  /* Initialize D codes list */
71  D_code_list = gerbv_stats_new_aperture_list();
72  if (D_code_list == NULL)
73  GERB_FATAL_ERROR("malloc D_code_list failed in %s()", __FUNCTION__);
74  stats->D_code_list = (gerbv_aperture_list_t*)D_code_list;
75 
76  return stats;
77 }
78 
79 void
80 gerbv_destroy_error_list(gerbv_error_list_t* errorList) {
81  gerbv_error_list_t *nextError = errorList, *tempError;
82 
83  while (nextError) {
84  tempError = nextError->next;
85  g_free(nextError->error_text);
86  g_free(nextError);
87  nextError = tempError;
88  }
89 }
90 
91 void
92 gerbv_destroy_aperture_list(gerbv_aperture_list_t* apertureList) {
93  gerbv_aperture_list_t *nextAperture = apertureList, *tempAperture;
94 
95  while (nextAperture) {
96  tempAperture = nextAperture->next;
97  g_free(nextAperture);
98  nextAperture = tempAperture;
99  }
100 }
101 
102 /* ------------------------------------------------------- */
103 void
105  if (stats == NULL)
106  return;
107  gerbv_destroy_error_list(stats->error_list);
108  gerbv_destroy_aperture_list(stats->aperture_list);
109  gerbv_destroy_aperture_list(stats->D_code_list);
110  g_free(stats);
111 }
112 
113 /* ------------------------------------------------------- */
120 void
121 gerbv_stats_add_layer(gerbv_stats_t* accum_stats, gerbv_stats_t* input_stats, int this_layer) {
122 
123  dprintf("---> Entering gerbv_stats_add_layer ... \n");
124 
125  gerbv_error_list_t* error;
126  gerbv_aperture_list_t* aperture;
127  gerbv_aperture_list_t* D_code;
128 
129  accum_stats->layer_count++;
130  accum_stats->G0 += input_stats->G0;
131  accum_stats->G1 += input_stats->G1;
132  accum_stats->G2 += input_stats->G2;
133  accum_stats->G3 += input_stats->G3;
134  accum_stats->G4 += input_stats->G4;
135  accum_stats->G10 += input_stats->G10;
136  accum_stats->G11 += input_stats->G11;
137  accum_stats->G12 += input_stats->G12;
138  accum_stats->G36 += input_stats->G36;
139  accum_stats->G37 += input_stats->G37;
140  accum_stats->G54 += input_stats->G54;
141  accum_stats->G55 += input_stats->G55;
142  accum_stats->G70 += input_stats->G70;
143  accum_stats->G71 += input_stats->G71;
144  accum_stats->G74 += input_stats->G74;
145  accum_stats->G75 += input_stats->G75;
146  accum_stats->G90 += input_stats->G90;
147  accum_stats->G91 += input_stats->G91;
148  accum_stats->G_unknown += input_stats->G_unknown;
149 
150  accum_stats->D1 += input_stats->D1;
151  accum_stats->D2 += input_stats->D2;
152  accum_stats->D3 += input_stats->D3;
153  /* Create list of user-defined D codes from aperture list */
154  for (D_code = input_stats->D_code_list; D_code != NULL; D_code = D_code->next) {
155  if (D_code->number != -1) {
156  dprintf(
157  " .... In gerbv_stats_add_layer, D code section, adding number = %d to accum_stats D list ...\n",
158  D_code->number
159  );
160  gerbv_stats_add_to_D_list(accum_stats->D_code_list, D_code->number);
161  dprintf(
162  " .... In gerbv_stats_add_layer, D code section, calling increment_D_count with count %d ...\n",
163  D_code->count
164  );
165  gerbv_stats_increment_D_list_count(
166  accum_stats->D_code_list, D_code->number, D_code->count, accum_stats->error_list
167  );
168  }
169  }
170  accum_stats->D_unknown += input_stats->D_unknown;
171  accum_stats->D_error += input_stats->D_error;
172 
173  accum_stats->M0 += input_stats->M0;
174  accum_stats->M1 += input_stats->M1;
175  accum_stats->M2 += input_stats->M2;
176  accum_stats->M_unknown += input_stats->M_unknown;
177 
178  accum_stats->X += input_stats->X;
179  accum_stats->Y += input_stats->Y;
180  accum_stats->I += input_stats->I;
181  accum_stats->J += input_stats->J;
182 
183  accum_stats->star += input_stats->star;
184  accum_stats->unknown += input_stats->unknown;
185 
186  /* ==== Now deal with the error list ==== */
187  for (error = input_stats->error_list; error != NULL; error = error->next) {
188  if (error->error_text != NULL) {
189  gerbv_stats_add_error(accum_stats->error_list, this_layer, error->error_text, error->type);
190  }
191  }
192 
193  /* ==== Now deal with the aperture list ==== */
194  for (aperture = input_stats->aperture_list; aperture != NULL; aperture = aperture->next) {
195  if (aperture->number != -1) {
196  gerbv_stats_add_aperture(
197  accum_stats->aperture_list, this_layer, aperture->number, aperture->type, aperture->parameter
198  );
199  }
200  }
201 
202  dprintf("<---- .... Leaving gerbv_stats_add_layer. \n");
203 
204  return;
205 }
206 
207 /* ------------------------------------------------------- */
209 gerbv_stats_new_error_list() {
211 
212  /* Malloc space for new error_list struct. Return NULL if error. */
213  if (NULL == (error_list = g_new(gerbv_error_list_t, 1))) {
214  return NULL;
215  }
216 
217  error_list->layer = -1;
218  error_list->error_text = NULL;
219  error_list->next = NULL;
220  return error_list;
221 }
222 
223 /* ------------------------------------------------------- */
224 void
225 gerbv_stats_printf(gerbv_error_list_t* list, gerbv_message_type_t type, int layer, const char* text, ...) {
226  gchar* str;
227  va_list args;
228 
229  va_start(args, text);
230  str = g_strdup_vprintf(text, args);
231  va_end(args);
232  gerbv_stats_add_error(list, layer, str, type);
233  g_free(str);
234 }
235 
240 /* ------------------------------------------------------- */
241 int
243  int i = 0;
244  char* ec = (char*)&i;
245 
246  ec[0] = '\\';
247 
248  switch (c) {
249  case '\0': ec[1] = '0'; break;
250  case '\a': ec[1] = 'a'; break;
251  case '\b': ec[1] = 'b'; break;
252  case '\f': ec[1] = 'f'; break;
253  case '\n': ec[1] = 'n'; break;
254  case '\r': ec[1] = 'r'; break;
255  case '\t': ec[1] = 't'; break;
256  case '\v': ec[1] = 'v'; break;
257  case '\\': ec[1] = '\\'; break;
258  case '"': ec[1] = '"'; break;
259  default: ec[0] = c;
260  }
261 
262  return i;
263 }
264 
265 /* ------------------------------------------------------- */
266 void
267 gerbv_stats_add_error(gerbv_error_list_t* error_list_in, int layer, const char* error_text, gerbv_message_type_t type) {
268 
269  gerbv_error_list_t* error_list_new;
270  gerbv_error_list_t* error_last = NULL;
271  gerbv_error_list_t* error;
272 
273  /* Replace embedded error messages */
274  switch (type) {
275  case GERBV_MESSAGE_FATAL: GERB_FATAL_ERROR("%s", error_text); break;
276  case GERBV_MESSAGE_ERROR: GERB_COMPILE_ERROR("%s", error_text); break;
277  case GERBV_MESSAGE_WARNING: GERB_COMPILE_WARNING("%s", error_text); break;
278  case GERBV_MESSAGE_NOTE: break;
279  }
280 
281  /* First handle case where this is the first list element */
282  if (error_list_in->error_text == NULL) {
283  error_list_in->layer = layer;
284  error_list_in->error_text = g_strdup_printf("%s", error_text);
285  error_list_in->type = type;
286  error_list_in->next = NULL;
287  return;
288  }
289 
290  /* Next check to see if this error is already in the list */
291  for (error = error_list_in; error != NULL; error = error->next) {
292  if ((strcmp(error->error_text, error_text) == 0) && (error->layer == layer)) {
293  return; /* This error text is already in the error list */
294  }
295  error_last = error; /* point to last element in error list */
296  }
297  /* This error text is unique. Therefore, add it to the list */
298 
299  /* Now malloc space for new error list element */
300  if (NULL == (error_list_new = g_new(gerbv_error_list_t, 1))) {
301  GERB_FATAL_ERROR("malloc error_list failed in %s()", __FUNCTION__);
302  }
303 
304  /* Set member elements */
305  error_list_new->layer = layer;
306  error_list_new->error_text = g_strdup_printf("%s", error_text);
307  error_list_new->type = type;
308  error_list_new->next = NULL;
309  error_last->next = error_list_new;
310 
311  return;
312 }
313 
314 /* ------------------------------------------------------- */
315 gerbv_aperture_list_t*
316 gerbv_stats_new_aperture_list() {
317  gerbv_aperture_list_t* aperture_list;
318  int i;
319 
320  dprintf("Mallocing new gerb aperture list\n");
321  /* Malloc space for new aperture_list struct. Return NULL if error. */
322  if (NULL == (aperture_list = g_new(gerbv_aperture_list_t, 1))) {
323  dprintf("malloc new gerb aperture list failed in %s()\n", __FUNCTION__);
324  return NULL;
325  }
326 
327  dprintf(" Placing values in certain structs.\n");
328  aperture_list->number = -1;
329  aperture_list->count = 0;
330  aperture_list->type = 0;
331  for (i = 0; i < 5; i++) {
332  aperture_list->parameter[i] = 0.0;
333  }
334  aperture_list->next = NULL;
335  return aperture_list;
336 }
337 
338 /* ------------------------------------------------------- */
339 void
340 gerbv_stats_add_aperture(
341  gerbv_aperture_list_t* aperture_list_in, int layer, int number, gerbv_aperture_type_t type, double parameter[5]
342 ) {
343 
344  gerbv_aperture_list_t* aperture_list_new;
345  gerbv_aperture_list_t* aperture_last = NULL;
346  gerbv_aperture_list_t* aperture;
347  int i;
348 
349  dprintf(" ---> Entering gerbv_stats_add_aperture ....\n");
350 
351  /* First handle case where this is the first list element */
352  if (aperture_list_in->number == -1) {
353  dprintf(" .... Adding first aperture to aperture list ... \n");
354  dprintf(" .... Aperture type = %d ... \n", type);
355  aperture_list_in->number = number;
356  aperture_list_in->type = type;
357  aperture_list_in->layer = layer;
358  for (i = 0; i < 5; i++) {
359  aperture_list_in->parameter[i] = parameter[i];
360  }
361  aperture_list_in->next = NULL;
362  dprintf(" <--- .... Leaving gerbv_stats_add_aperture.\n");
363  return;
364  }
365 
366  /* Next check to see if this aperture is already in the list */
367  for (aperture = aperture_list_in; aperture != NULL; aperture = aperture->next) {
368  if ((aperture->number == number) && (aperture->layer == layer)) {
369  dprintf(" .... This aperture is already in the list ... \n");
370  dprintf(" <--- .... Leaving gerbv_stats_add_aperture.\n");
371  return;
372  }
373  aperture_last = aperture; /* point to last element in list */
374  }
375  /* This aperture number is unique. Therefore, add it to the list */
376  dprintf(" .... Adding another aperture to list ... \n");
377  dprintf(" .... Aperture type = %d ... \n", type);
378 
379  /* Now malloc space for new aperture list element */
380  if (NULL == (aperture_list_new = g_new(gerbv_aperture_list_t, 1))) {
381  GERB_FATAL_ERROR("malloc aperture_list failed in %s()", __FUNCTION__);
382  }
383 
384  /* Set member elements */
385  aperture_list_new->layer = layer;
386  aperture_list_new->number = number;
387  aperture_list_new->type = type;
388  aperture_list_new->next = NULL;
389  for (i = 0; i < 5; i++) {
390  aperture_list_new->parameter[i] = parameter[i];
391  }
392  aperture_last->next = aperture_list_new;
393 
394  dprintf(" <--- .... Leaving gerbv_stats_add_aperture.\n");
395 
396  return;
397 }
398 
399 /* ------------------------------------------------------- */
400 void
401 gerbv_stats_add_to_D_list(gerbv_aperture_list_t* D_list_in, int number) {
402 
403  gerbv_aperture_list_t* D_list;
404  gerbv_aperture_list_t* D_list_last = NULL;
405  gerbv_aperture_list_t* D_list_new;
406 
407  dprintf(" ----> Entering add_to_D_list, numbr = %d\n", number);
408 
409  /* First handle case where this is the first list element */
410  if (D_list_in->number == -1) {
411  dprintf(" .... Adding first D code to D code list ... \n");
412  dprintf(" .... Aperture number = %d ... \n", number);
413  D_list_in->number = number;
414  D_list_in->count = 0;
415  D_list_in->next = NULL;
416  dprintf(" <--- .... Leaving add_to_D_list.\n");
417  return;
418  }
419 
420  /* Look to see if this is already in list */
421  for (D_list = D_list_in; D_list != NULL; D_list = D_list->next) {
422  if (D_list->number == number) {
423  dprintf(" .... Found in D list .... \n");
424  dprintf(" <--- .... Leaving add_to_D_list.\n");
425  return;
426  }
427  D_list_last = D_list; /* point to last element in list */
428  }
429 
430  /* This aperture number is unique. Therefore, add it to the list */
431  dprintf(" .... Adding another D code to D code list ... \n");
432 
433  /* Malloc space for new aperture list element */
434  if (NULL == (D_list_new = g_new(gerbv_aperture_list_t, 1))) {
435  GERB_FATAL_ERROR("malloc D_list failed in %s()", __FUNCTION__);
436  }
437 
438  /* Set member elements */
439  D_list_new->number = number;
440  D_list_new->count = 0;
441  D_list_new->next = NULL;
442  D_list_last->next = D_list_new;
443 
444  dprintf(" <--- .... Leaving add_to_D_list.\n");
445 
446  return;
447 }
448 
449 /* ------------------------------------------------------- */
450 int
451 gerbv_stats_increment_D_list_count(gerbv_aperture_list_t* D_list_in, int number, int count, gerbv_error_list_t* error) {
452 
453  gerbv_aperture_list_t* D_list;
454 
455  dprintf(" Entering inc_D_list_count, code = D%d, input count to add = %d\n", number, count);
456 
457  /* Find D code in list and increment it */
458  for (D_list = D_list_in; D_list != NULL; D_list = D_list->next) {
459  if (D_list->number == number) {
460  dprintf(" old count = %d\n", D_list->count);
461  D_list->count += count; /* Add to this aperture count, then return */
462  dprintf(" updated count = %d\n", D_list->count);
463  return 0; /* Return 0 for success */
464  }
465  }
466 
467  /* This D number is not defined. Therefore, flag error */
468  dprintf(" .... Didn't find this D code in defined list .... \n");
469  dprintf(" <--- .... Leaving inc_D_list_count.\n");
470 
471  gerbv_stats_printf(error, GERBV_MESSAGE_ERROR, -1, _("Undefined aperture number called out in D code"));
472 
473  return -1; /* Return -1 for failure */
474 }
Contains basic defines.
int gerbv_escape_char_return_int(char c)
Escape special ASCII char (' ', '\0').
Definition: gerb_stats.c:242
gerbv_stats_t * gerbv_stats_new(void)
Allocates a new gerbv_stats structure.
Definition: gerb_stats.c:46
void gerbv_stats_add_layer(gerbv_stats_t *accum_stats, gerbv_stats_t *input_stats, int this_layer)
Definition: gerb_stats.c:121
void gerbv_stats_destroy(gerbv_stats_t *stats)
Definition: gerb_stats.c:104
Header info for the statistics generating functions for RS274X files.
The main header file for the libgerbv library.
gerbv_message_type_t
Definition: gerbv.h:140
@ GERBV_MESSAGE_ERROR
Definition: gerbv.h:142
@ GERBV_MESSAGE_NOTE
Definition: gerbv.h:144
@ GERBV_MESSAGE_FATAL
Definition: gerbv.h:141
@ GERBV_MESSAGE_WARNING
Definition: gerbv.h:143
gerbv_aperture_type_t
Definition: gerbv.h:150