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->G32 += input_stats->G32;
133  accum_stats->G33 += input_stats->G33;
134  accum_stats->G85 += input_stats->G85;
135  accum_stats->G87 += input_stats->G87;
136  accum_stats->G90 += input_stats->G90;
137  accum_stats->G91 += input_stats->G91;
138  accum_stats->G93 += input_stats->G93;
139  accum_stats->G_machine_only += input_stats->G_machine_only;
140  accum_stats->G_unknown += input_stats->G_unknown;
141 
142  accum_stats->M00 += input_stats->M00;
143  accum_stats->M01 += input_stats->M01;
144  accum_stats->M02 += input_stats->M02;
145  accum_stats->M18 += input_stats->M18;
146  accum_stats->M25 += input_stats->M25;
147  accum_stats->M30 += input_stats->M30;
148  accum_stats->M31 += input_stats->M31;
149  accum_stats->M45 += input_stats->M45;
150  accum_stats->M47 += input_stats->M47;
151  accum_stats->M48 += input_stats->M48;
152  accum_stats->M70 += input_stats->M70;
153  accum_stats->M71 += input_stats->M71;
154  accum_stats->M72 += input_stats->M72;
155  accum_stats->M80 += input_stats->M80;
156  accum_stats->M90 += input_stats->M90;
157  accum_stats->M95 += input_stats->M95;
158  accum_stats->M97 += input_stats->M97;
159  accum_stats->M98 += input_stats->M98;
160  accum_stats->M_machine_only += input_stats->M_machine_only;
161  accum_stats->M_unknown += input_stats->M_unknown;
162 
163  accum_stats->R += input_stats->R;
164 
165  /* ==== Now deal with the drill list ==== */
166  for (drill = input_stats->drill_list;
167  drill != NULL;
168  drill = drill->next) {
169  DPRINTF(" In gerbv_drill_stats_add_layer, adding drill_num = %d to list\n",
170  drill->drill_num);
171  /* First add this input drill to the accumulated list.
172  * Drills already in accum list will not be added. */
173  drill_stats_add_to_drill_list(accum_stats->drill_list,
174  drill->drill_num,
175  drill->drill_size,
176  drill->drill_unit);
177 
178  /* Now add count of input drill to accum list */
179  DPRINTF(" adding count %d of drills for drill %d\n",
180  drill->drill_count, drill->drill_num);
181  drill_stats_add_to_drill_counter(accum_stats->drill_list,
182  drill->drill_num,
183  drill->drill_count);
184  accum_stats->total_count += drill->drill_count;
185  }
186 
187  /* ==== Now deal with the error list ==== */
188  for (error = input_stats->error_list; error != NULL; error = error->next) {
189  if (error->error_text != NULL)
190  gerbv_stats_printf(accum_stats->error_list, error->type,
191  this_layer, "%s", error->error_text);
192  }
193 
194  /* ==== Now deal with the misc header stuff ==== */
195  tmps = NULL;
196  tmps2 = NULL;
197  if (input_stats->detect) {
198  tmps2 = g_strdup_printf (_("Broken tool detect %s (layer %d)"), input_stats->detect, this_layer);
199  }
200  if (accum_stats->detect) {
201  if (tmps2) {
202  tmps = g_strdup_printf ("%s\n%s", accum_stats->detect, tmps2);
203  g_free (accum_stats->detect);
204  accum_stats->detect = NULL;
205  }
206  } else {
207  if (tmps2) {
208  tmps = g_strdup_printf ("%s", tmps2);
209  }
210  }
211  if (tmps2) {
212  g_free (tmps2);
213  }
214  if (tmps != NULL) {
215  accum_stats->detect = tmps;
216  }
217 
218  for (error = input_stats->error_list;
219  error != NULL;
220  error = error->next) {
221  if (error->error_text != NULL) {
222  gerbv_stats_printf(accum_stats->error_list, error->type,
223  this_layer, "%s", error->error_text);
224  }
225  }
226 
227 
228  DPRINTF("<--- .... Leaving gerbv_drill_stats_add_layer.\n");
229 
230  return;
231 }
232 
233 
234 /* ------------------------------------------------------- */
235 gboolean
236 drill_stats_in_drill_list(gerbv_drill_list_t *drill_list_in,
237  int drill_num_in) {
238  gerbv_drill_list_t *drill;
239  for(drill = drill_list_in; drill != NULL; drill = drill->next) {
240  if (drill_num_in == drill->drill_num) {
241  return TRUE;
242  }
243  }
244  return FALSE;
245 
246 }
247 
248 /* ------------------------------------------------------- */
250 gerbv_drill_stats_new_drill_list() {
252 
253  /* Malloc space for new drill_list struct. Return NULL if error. */
254  if (NULL == (drill_list = g_new(gerbv_drill_list_t, 1))) {
255  return NULL;
256  }
257 
258  drill_list->drill_count = 0;
259  drill_list->drill_num = -1; /* default val */
260  drill_list->drill_size = 0.0;
261  drill_list->drill_unit = NULL;
262  drill_list->next = NULL;
263  return drill_list;
264 }
265 
266 
267 /* ------------------------------------------------------- */
268 void
269 drill_stats_add_to_drill_list(gerbv_drill_list_t *drill_list_in,
270  int drill_num_in, double drill_size_in,
271  char *drill_unit_in) {
272 
273  gerbv_drill_list_t *drill_list_new;
274  gerbv_drill_list_t *drill;
275  gerbv_drill_list_t *drill_last = NULL;
276 
277  DPRINTF("%s(%p, %d, %g, \"%s\")\n", __FUNCTION__, drill_list_in, drill_num_in,
278  drill_size_in, drill_unit_in);
279 
280  DPRINTF(" ---> Entering drill_stats_add_to_drill_list, first drill_num in list = %d ...\n",
281  drill_list_in->drill_num);
282 
283  /* First check for empty list. If empty, then just add this drill */
284  if (drill_list_in->drill_num == -1) {
285  DPRINTF(" .... In drill_stats_add_to_drill_list, adding first drill, no %d\n",
286  drill_num_in);
287  drill_list_in->drill_num = drill_num_in;
288  drill_list_in->drill_size = drill_size_in;
289  drill_list_in->drill_count = 0;
290  drill_list_in->drill_unit = g_strdup_printf("%s", drill_unit_in);
291  drill_list_in->next = NULL;
292  return;
293  }
294  /* Else check to see if this drill is already in the list */
295  for(drill = drill_list_in;
296  drill != NULL;
297  drill = (gerbv_drill_list_t *) drill->next) {
298  DPRINTF("checking this drill_num %d against that in list %d.\n",
299  drill_num_in, drill->drill_num);
300  if (drill_num_in == drill->drill_num) {
301  DPRINTF(" .... In drill_stats_add_to_drill_list, drill no %d already in list\n",
302  drill_num_in);
303  return; /* Found it in list, so return */
304  }
305  drill_last = drill;
306  }
307 
308  /* Now malloc space for new drill list element */
309  if (NULL == (drill_list_new = g_new(gerbv_drill_list_t, 1))) {
310  GERB_FATAL_ERROR("malloc format failed in %s()", __FUNCTION__);
311  }
312 
313  /* Now set various parameters based upon calling args */
314  DPRINTF(" .... In drill_stats_add_to_drill_list, adding new drill, no %d\n",
315  drill_num_in);
316  drill_list_new->drill_num = drill_num_in;
317  drill_list_new->drill_size = drill_size_in;
318  drill_list_new->drill_count = 0;
319  drill_list_new->drill_unit = g_strdup_printf("%s", drill_unit_in);
320  drill_list_new->next = NULL;
321  drill_last->next = drill_list_new;
322 
323  DPRINTF(" <---- ... leaving drill_stats_add_to_drill_list.\n");
324  return;
325 }
326 
327 /* ------------------------------------------------------- */
328 void
329 drill_stats_modify_drill_list(gerbv_drill_list_t *drill_list_in,
330  int drill_num_in, double drill_size_in,
331  char *drill_unit_in) {
332 
333  gerbv_drill_list_t *drill;
334 
335  DPRINTF(" ---> Entering drill_stats_modify_drill_list, first drill_num in list = %d ...\n",
336  drill_list_in->drill_num);
337 
338  /* Look for this drill num in list */
339  for(drill = drill_list_in;
340  drill != NULL;
341  drill = (gerbv_drill_list_t *) drill->next) {
342  DPRINTF("checking this drill_num %d against that in list %d.\n",
343  drill_num_in, drill->drill_num);
344  if (drill_num_in == drill->drill_num) {
345  DPRINTF(" .... Found it, now update it ....\n");
346  drill->drill_size = drill_size_in;
347  if (drill->drill_unit)
348  g_free(drill->drill_unit);
349  drill->drill_unit = g_strdup_printf("%s", drill_unit_in);
350  DPRINTF(" <---- ... Modified drill. leaving drill_stats_modify_drill_list.\n");
351  return;
352  }
353  }
354  DPRINTF(" <---- ... Did not find drill. leaving drill_stats_modify_drill_list.\n");
355  return;
356 }
357 
358 /* ------------------------------------------------------- */
359 void
360 drill_stats_increment_drill_counter(gerbv_drill_list_t *drill_list_in,
361  int drill_num_in) {
362 
363  DPRINTF(" ----> Entering drill_stats_increment_drill_counter......\n");
364  /* First check to see if this drill is already in the list */
365  gerbv_drill_list_t *drill;
366  for(drill = drill_list_in; drill != NULL; drill = drill->next) {
367  if (drill_num_in == drill->drill_num) {
368  drill->drill_count++;
369  DPRINTF(" .... incrementing drill count. drill_num = %d, drill_count = %d.\n",
370  drill_list_in->drill_num, drill->drill_count);
371  DPRINTF(" <---- .... Leaving drill_stats_increment_drill_counter after incrementing counter.\n");
372  return;
373  }
374  }
375  DPRINTF(" <---- .... Leaving drill_stats_increment_drill_counter without incrementing any counter.\n");
376 
377 }
378 
379 /* ------------------------------------------------------- */
380 void
381 drill_stats_add_to_drill_counter(gerbv_drill_list_t *drill_list_in,
382  int drill_num_in,
383  int increment) {
384 
385  gerbv_drill_list_t *drill;
386  for(drill = drill_list_in; drill != NULL; drill = drill->next) {
387  if (drill_num_in == drill->drill_num) {
388  DPRINTF(" In drill_stats_add_to_drill_counter, adding increment = %d drills to drill list\n", increment);
389  drill->drill_count += increment;
390  return;
391  }
392  }
393 }
394 
395 
396 /* ------------------------------------------------------- */
398 gerbv_drill_stats_new_error_list() {
400 
401  /* Malloc space for new error_list struct. Return NULL if error. */
402  if (NULL == (error_list = g_new(gerbv_error_list_t, 1))) {
403  return NULL;
404  }
405 
406  error_list->layer = -1;
407  error_list->error_text = NULL;
408  error_list->next = NULL;
409  return error_list;
410 }
411 
412 /* ------------------------------------------------------- */
415 void
417  int layer, const char *error_text,
419 {
420  gerbv_stats_add_error(error_list_in, layer, error_text, type);
421 }
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:416
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