gerbv
drill_stats.c
Go to the documentation of this file.
1 /*
2  * gEDA - GNU Electronic Design Automation
3  * drill_stats.c -- a part of gerbv.
4  *
5  * Copyright (C) 2007 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 <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <math.h>
35 
36 #include "common.h"
37 #include "drill_stats.h"
38 #include "gerb_stats.h"
39 
40 #undef DPRINTF
41 #define DPRINTF(...) do { if (DEBUG) printf(__VA_ARGS__); } while (0)
42 
43 
44 /* ------------------------------------------------------- */
49 
50  gerbv_drill_stats_t *stats;
53 
54  /* Malloc space for new stats struct. Return NULL if error. */
55  if (NULL == (stats = g_new0(gerbv_drill_stats_t, 1))) {
56  return NULL;
57  }
58 
59  /* Initialize drill list */
60  drill_list = gerbv_drill_stats_new_drill_list();
61  if (drill_list == NULL)
62  GERB_FATAL_ERROR("malloc drill_list failed in %s()", __FUNCTION__);
63  stats->drill_list = (gerbv_drill_list_t *) drill_list;
64 
65  /* Initialize error list */
66  error_list = gerbv_drill_stats_new_error_list();
67  if (error_list == NULL)
68  GERB_FATAL_ERROR("malloc error_list failed in %s()", __FUNCTION__);
69  stats->error_list = (gerbv_error_list_t *) error_list;
70 
71  stats->detect = NULL;
72 
73  return stats;
74 }
75 
76 void
77 gerbv_drill_destroy_error_list (gerbv_error_list_t *errorList) {
78  gerbv_error_list_t *nextError=errorList,*tempError;
79 
80  while (nextError) {
81  tempError = nextError->next;
82  g_free (nextError->error_text);
83  g_free (nextError);
84  nextError = tempError;
85  }
86 }
87 
88 void
89 gerbv_drill_destroy_drill_list (gerbv_drill_list_t *apertureList) {
90  gerbv_drill_list_t *nextAperture=apertureList,*tempAperture;
91 
92  while (nextAperture) {
93  tempAperture = nextAperture->next;
94  g_free(nextAperture->drill_unit);
95  g_free (nextAperture);
96  nextAperture = tempAperture;
97  }
98 }
99 
100 void
102  if (stats == NULL)
103  return;
104  gerbv_drill_destroy_error_list (stats->error_list);
105  gerbv_drill_destroy_drill_list (stats->drill_list);
106  g_free (stats);
107 }
108 
109 /* ------------------------------------------------------- */
110 void
112  gerbv_drill_stats_t *input_stats,
113  int this_layer) {
114 
115  gerbv_drill_list_t *drill;
116  gerbv_error_list_t *error;
117  char *tmps, *tmps2;
118 
119  DPRINTF("---> Entering gerbv_drill_stats_add_layer ..... \n");
120 
121  accum_stats->layer_count++;
122 
123  accum_stats->comment += input_stats->comment;
124  /* F codes go here */
125 
126  accum_stats->G00 += input_stats->G00;
127  accum_stats->G01 += input_stats->G01;
128  accum_stats->G02 += input_stats->G02;
129  accum_stats->G03 += input_stats->G03;
130  accum_stats->G04 += input_stats->G04;
131  accum_stats->G05 += input_stats->G05;
132  accum_stats->G85 += input_stats->G85;
133  accum_stats->G87 += input_stats->G87;
134  accum_stats->G90 += input_stats->G90;
135  accum_stats->G91 += input_stats->G91;
136  accum_stats->G93 += input_stats->G93;
137  accum_stats->G_machine_only += input_stats->G_machine_only;
138  accum_stats->G_unknown += input_stats->G_unknown;
139 
140  accum_stats->M00 += input_stats->M00;
141  accum_stats->M01 += input_stats->M01;
142  accum_stats->M02 += input_stats->M02;
143  accum_stats->M18 += input_stats->M18;
144  accum_stats->M25 += input_stats->M25;
145  accum_stats->M30 += input_stats->M30;
146  accum_stats->M31 += input_stats->M31;
147  accum_stats->M45 += input_stats->M45;
148  accum_stats->M47 += input_stats->M47;
149  accum_stats->M48 += input_stats->M48;
150  accum_stats->M70 += input_stats->M70;
151  accum_stats->M71 += input_stats->M71;
152  accum_stats->M72 += input_stats->M72;
153  accum_stats->M80 += input_stats->M80;
154  accum_stats->M90 += input_stats->M90;
155  accum_stats->M95 += input_stats->M95;
156  accum_stats->M97 += input_stats->M97;
157  accum_stats->M98 += input_stats->M98;
158  accum_stats->M_machine_only += input_stats->M_machine_only;
159  accum_stats->M_unknown += input_stats->M_unknown;
160 
161  accum_stats->R += input_stats->R;
162 
163  /* ==== Now deal with the drill list ==== */
164  for (drill = input_stats->drill_list;
165  drill != NULL;
166  drill = drill->next) {
167  DPRINTF(" In gerbv_drill_stats_add_layer, adding drill_num = %d to list\n",
168  drill->drill_num);
169  /* First add this input drill to the accumulated list.
170  * Drills already in accum list will not be added. */
171  drill_stats_add_to_drill_list(accum_stats->drill_list,
172  drill->drill_num,
173  drill->drill_size,
174  drill->drill_unit);
175 
176  /* Now add count of input drill to accum list */
177  DPRINTF(" adding count %d of drills for drill %d\n",
178  drill->drill_count, drill->drill_num);
179  drill_stats_add_to_drill_counter(accum_stats->drill_list,
180  drill->drill_num,
181  drill->drill_count);
182  accum_stats->total_count += drill->drill_count;
183  }
184 
185  /* ==== Now deal with the error list ==== */
186  for (error = input_stats->error_list; error != NULL; error = error->next) {
187  if (error->error_text != NULL)
188  gerbv_stats_printf(accum_stats->error_list, error->type,
189  this_layer, "%s", error->error_text);
190  }
191 
192  /* ==== Now deal with the misc header stuff ==== */
193  tmps = NULL;
194  tmps2 = NULL;
195  if (input_stats->detect) {
196  tmps2 = g_strdup_printf (_("Broken tool detect %s (layer %d)"), input_stats->detect, this_layer);
197  }
198  if (accum_stats->detect) {
199  if (tmps2) {
200  tmps = g_strdup_printf ("%s\n%s", accum_stats->detect, tmps2);
201  g_free (accum_stats->detect);
202  accum_stats->detect = NULL;
203  }
204  } else {
205  if (tmps2) {
206  tmps = g_strdup_printf ("%s", tmps2);
207  }
208  }
209  if (tmps2) {
210  g_free (tmps2);
211  }
212  if (tmps != NULL) {
213  accum_stats->detect = tmps;
214  }
215 
216  for (error = input_stats->error_list;
217  error != NULL;
218  error = error->next) {
219  if (error->error_text != NULL) {
220  gerbv_stats_printf(accum_stats->error_list, error->type,
221  this_layer, "%s", error->error_text);
222  }
223  }
224 
225 
226  DPRINTF("<--- .... Leaving gerbv_drill_stats_add_layer.\n");
227 
228  return;
229 }
230 
231 
232 /* ------------------------------------------------------- */
233 gboolean
234 drill_stats_in_drill_list(gerbv_drill_list_t *drill_list_in,
235  int drill_num_in) {
236  gerbv_drill_list_t *drill;
237  for(drill = drill_list_in; drill != NULL; drill = drill->next) {
238  if (drill_num_in == drill->drill_num) {
239  return TRUE;
240  }
241  }
242  return FALSE;
243 
244 }
245 
246 /* ------------------------------------------------------- */
248 gerbv_drill_stats_new_drill_list() {
250 
251  /* Malloc space for new drill_list struct. Return NULL if error. */
252  if (NULL == (drill_list = g_new(gerbv_drill_list_t, 1))) {
253  return NULL;
254  }
255 
256  drill_list->drill_count = 0;
257  drill_list->drill_num = -1; /* default val */
258  drill_list->drill_size = 0.0;
259  drill_list->drill_unit = NULL;
260  drill_list->next = NULL;
261  return drill_list;
262 }
263 
264 
265 /* ------------------------------------------------------- */
266 void
267 drill_stats_add_to_drill_list(gerbv_drill_list_t *drill_list_in,
268  int drill_num_in, double drill_size_in,
269  char *drill_unit_in) {
270 
271  gerbv_drill_list_t *drill_list_new;
272  gerbv_drill_list_t *drill;
273  gerbv_drill_list_t *drill_last = NULL;
274 
275  DPRINTF("%s(%p, %d, %g, \"%s\")\n", __FUNCTION__, drill_list_in, drill_num_in,
276  drill_size_in, drill_unit_in);
277 
278  DPRINTF(" ---> Entering drill_stats_add_to_drill_list, first drill_num in list = %d ...\n",
279  drill_list_in->drill_num);
280 
281  /* First check for empty list. If empty, then just add this drill */
282  if (drill_list_in->drill_num == -1) {
283  DPRINTF(" .... In drill_stats_add_to_drill_list, adding first drill, no %d\n",
284  drill_num_in);
285  drill_list_in->drill_num = drill_num_in;
286  drill_list_in->drill_size = drill_size_in;
287  drill_list_in->drill_count = 0;
288  drill_list_in->drill_unit = g_strdup_printf("%s", drill_unit_in);
289  drill_list_in->next = NULL;
290  return;
291  }
292  /* Else check to see if this drill is already in the list */
293  for(drill = drill_list_in;
294  drill != NULL;
295  drill = (gerbv_drill_list_t *) drill->next) {
296  DPRINTF("checking this drill_num %d against that in list %d.\n",
297  drill_num_in, drill->drill_num);
298  if (drill_num_in == drill->drill_num) {
299  DPRINTF(" .... In drill_stats_add_to_drill_list, drill no %d already in list\n",
300  drill_num_in);
301  return; /* Found it in list, so return */
302  }
303  drill_last = drill;
304  }
305 
306  /* Now malloc space for new drill list element */
307  if (NULL == (drill_list_new = g_new(gerbv_drill_list_t, 1))) {
308  GERB_FATAL_ERROR("malloc format failed in %s()", __FUNCTION__);
309  }
310 
311  /* Now set various parameters based upon calling args */
312  DPRINTF(" .... In drill_stats_add_to_drill_list, adding new drill, no %d\n",
313  drill_num_in);
314  drill_list_new->drill_num = drill_num_in;
315  drill_list_new->drill_size = drill_size_in;
316  drill_list_new->drill_count = 0;
317  drill_list_new->drill_unit = g_strdup_printf("%s", drill_unit_in);
318  drill_list_new->next = NULL;
319  drill_last->next = drill_list_new;
320 
321  DPRINTF(" <---- ... leaving drill_stats_add_to_drill_list.\n");
322  return;
323 }
324 
325 /* ------------------------------------------------------- */
326 void
327 drill_stats_modify_drill_list(gerbv_drill_list_t *drill_list_in,
328  int drill_num_in, double drill_size_in,
329  char *drill_unit_in) {
330 
331  gerbv_drill_list_t *drill;
332 
333  DPRINTF(" ---> Entering drill_stats_modify_drill_list, first drill_num in list = %d ...\n",
334  drill_list_in->drill_num);
335 
336  /* Look for this drill num in list */
337  for(drill = drill_list_in;
338  drill != NULL;
339  drill = (gerbv_drill_list_t *) drill->next) {
340  DPRINTF("checking this drill_num %d against that in list %d.\n",
341  drill_num_in, drill->drill_num);
342  if (drill_num_in == drill->drill_num) {
343  DPRINTF(" .... Found it, now update it ....\n");
344  drill->drill_size = drill_size_in;
345  if (drill->drill_unit)
346  g_free(drill->drill_unit);
347  drill->drill_unit = g_strdup_printf("%s", drill_unit_in);
348  DPRINTF(" <---- ... Modified drill. leaving drill_stats_modify_drill_list.\n");
349  return;
350  }
351  }
352  DPRINTF(" <---- ... Did not find drill. leaving drill_stats_modify_drill_list.\n");
353  return;
354 }
355 
356 /* ------------------------------------------------------- */
357 void
358 drill_stats_increment_drill_counter(gerbv_drill_list_t *drill_list_in,
359  int drill_num_in) {
360 
361  DPRINTF(" ----> Entering drill_stats_increment_drill_counter......\n");
362  /* First check to see if this drill is already in the list */
363  gerbv_drill_list_t *drill;
364  for(drill = drill_list_in; drill != NULL; drill = drill->next) {
365  if (drill_num_in == drill->drill_num) {
366  drill->drill_count++;
367  DPRINTF(" .... incrementing drill count. drill_num = %d, drill_count = %d.\n",
368  drill_list_in->drill_num, drill->drill_count);
369  DPRINTF(" <---- .... Leaving drill_stats_increment_drill_counter after incrementing counter.\n");
370  return;
371  }
372  }
373  DPRINTF(" <---- .... Leaving drill_stats_increment_drill_counter without incrementing any counter.\n");
374 
375 }
376 
377 /* ------------------------------------------------------- */
378 void
379 drill_stats_add_to_drill_counter(gerbv_drill_list_t *drill_list_in,
380  int drill_num_in,
381  int increment) {
382 
383  gerbv_drill_list_t *drill;
384  for(drill = drill_list_in; drill != NULL; drill = drill->next) {
385  if (drill_num_in == drill->drill_num) {
386  DPRINTF(" In drill_stats_add_to_drill_counter, adding increment = %d drills to drill list\n", increment);
387  drill->drill_count += increment;
388  return;
389  }
390  }
391 }
392 
393 
394 /* ------------------------------------------------------- */
396 gerbv_drill_stats_new_error_list() {
398 
399  /* Malloc space for new error_list struct. Return NULL if error. */
400  if (NULL == (error_list = g_new(gerbv_error_list_t, 1))) {
401  return NULL;
402  }
403 
404  error_list->layer = -1;
405  error_list->error_text = NULL;
406  error_list->next = NULL;
407  return error_list;
408 }
409 
410 /* ------------------------------------------------------- */
413 void
415  int layer, const char *error_text,
417 {
418  gerbv_stats_add_error(error_list_in, layer, error_text, type);
419 }
void gerbv_drill_stats_destroy(gerbv_drill_stats_t *stats)
Definition: drill_stats.c:101
void gerbv_drill_stats_add_layer(gerbv_drill_stats_t *accum_stats, gerbv_drill_stats_t *input_stats, int this_layer)
Definition: drill_stats.c:111
void drill_stats_add_error(gerbv_error_list_t *error_list_in, int layer, const char *error_text, gerbv_message_type_t type)
Add statistic message for drill layer.
Definition: drill_stats.c:414
gerbv_drill_stats_t * gerbv_drill_stats_new(void)
Allocates a new drill_stats structure.
Definition: drill_stats.c:48
Header info to the statistics generating functions for Excellon drill files.
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:149