gerbv  2.10.1-dev~93f1b5
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 #define dprintf \
41  if (DEBUG) \
42  printf
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
111 gerbv_drill_stats_add_layer(gerbv_drill_stats_t* accum_stats, gerbv_drill_stats_t* input_stats, int this_layer) {
112 
113  gerbv_drill_list_t* drill;
114  gerbv_error_list_t* error;
115  char * tmps, *tmps2;
116 
117  dprintf("---> Entering gerbv_drill_stats_add_layer ..... \n");
118 
119  accum_stats->layer_count++;
120 
121  accum_stats->comment += input_stats->comment;
122  /* F codes go here */
123 
124  accum_stats->G00 += input_stats->G00;
125  accum_stats->G01 += input_stats->G01;
126  accum_stats->G02 += input_stats->G02;
127  accum_stats->G03 += input_stats->G03;
128  accum_stats->G04 += input_stats->G04;
129  accum_stats->G05 += input_stats->G05;
130  accum_stats->G85 += input_stats->G85;
131  accum_stats->G90 += input_stats->G90;
132  accum_stats->G91 += input_stats->G91;
133  accum_stats->G93 += input_stats->G93;
134  accum_stats->G_unknown += input_stats->G_unknown;
135 
136  accum_stats->M00 += input_stats->M00;
137  accum_stats->M01 += input_stats->M01;
138  accum_stats->M18 += input_stats->M18;
139  accum_stats->M25 += input_stats->M25;
140  accum_stats->M30 += input_stats->M30;
141  accum_stats->M31 += input_stats->M31;
142  accum_stats->M45 += input_stats->M45;
143  accum_stats->M47 += input_stats->M47;
144  accum_stats->M48 += input_stats->M48;
145  accum_stats->M71 += input_stats->M71;
146  accum_stats->M72 += input_stats->M72;
147  accum_stats->M95 += input_stats->M95;
148  accum_stats->M97 += input_stats->M97;
149  accum_stats->M98 += input_stats->M98;
150  accum_stats->M_unknown += input_stats->M_unknown;
151 
152  accum_stats->R += input_stats->R;
153 
154  /* ==== Now deal with the drill list ==== */
155  for (drill = input_stats->drill_list; drill != NULL; drill = drill->next) {
156  dprintf(" In gerbv_drill_stats_add_layer, adding drill_num = %d to list\n", drill->drill_num);
157  /* First add this input drill to the accumulated list.
158  * Drills already in accum list will not be added. */
159  drill_stats_add_to_drill_list(accum_stats->drill_list, drill->drill_num, drill->drill_size, drill->drill_unit);
160 
161  /* Now add count of input drill to accum list */
162  dprintf(" adding count %d of drills for drill %d\n", drill->drill_count, drill->drill_num);
163  drill_stats_add_to_drill_counter(accum_stats->drill_list, drill->drill_num, drill->drill_count);
164  accum_stats->total_count += drill->drill_count;
165  }
166 
167  /* ==== Now deal with the error list ==== */
168  for (error = input_stats->error_list; error != NULL; error = error->next) {
169  if (error->error_text != NULL)
170  gerbv_stats_printf(accum_stats->error_list, error->type, this_layer, "%s", error->error_text);
171  }
172 
173  /* ==== Now deal with the misc header stuff ==== */
174  tmps = NULL;
175  tmps2 = NULL;
176  if (input_stats->detect) {
177  tmps2 = g_strdup_printf(_("Broken tool detect %s (layer %d)"), input_stats->detect, this_layer);
178  }
179  if (accum_stats->detect) {
180  if (tmps2) {
181  tmps = g_strdup_printf("%s\n%s", accum_stats->detect, tmps2);
182  g_free(accum_stats->detect);
183  accum_stats->detect = NULL;
184  }
185  } else {
186  if (tmps2) {
187  tmps = g_strdup_printf("%s", tmps2);
188  }
189  }
190  if (tmps2) {
191  g_free(tmps2);
192  }
193  if (tmps != NULL) {
194  accum_stats->detect = tmps;
195  }
196 
197  for (error = input_stats->error_list; error != NULL; error = error->next) {
198  if (error->error_text != NULL) {
199  gerbv_stats_printf(accum_stats->error_list, error->type, this_layer, "%s", error->error_text);
200  }
201  }
202 
203  dprintf("<--- .... Leaving gerbv_drill_stats_add_layer.\n");
204 
205  return;
206 }
207 
208 /* ------------------------------------------------------- */
209 gboolean
210 drill_stats_in_drill_list(gerbv_drill_list_t* drill_list_in, int drill_num_in) {
211  gerbv_drill_list_t* drill;
212  for (drill = drill_list_in; drill != NULL; drill = drill->next) {
213  if (drill_num_in == drill->drill_num) {
214  return TRUE;
215  }
216  }
217  return FALSE;
218 }
219 
220 /* ------------------------------------------------------- */
222 gerbv_drill_stats_new_drill_list() {
224 
225  /* Malloc space for new drill_list struct. Return NULL if error. */
226  if (NULL == (drill_list = g_new(gerbv_drill_list_t, 1))) {
227  return NULL;
228  }
229 
230  drill_list->drill_count = 0;
231  drill_list->drill_num = -1; /* default val */
232  drill_list->drill_size = 0.0;
233  drill_list->drill_unit = NULL;
234  drill_list->next = NULL;
235  return drill_list;
236 }
237 
238 /* ------------------------------------------------------- */
239 void
240 drill_stats_add_to_drill_list(
241  gerbv_drill_list_t* drill_list_in, int drill_num_in, double drill_size_in, char* drill_unit_in
242 ) {
243 
244  gerbv_drill_list_t* drill_list_new;
245  gerbv_drill_list_t* drill;
246  gerbv_drill_list_t* drill_last = NULL;
247 
248  dprintf("%s(%p, %d, %g, \"%s\")\n", __FUNCTION__, drill_list_in, drill_num_in, drill_size_in, drill_unit_in);
249 
250  dprintf(
251  " ---> Entering drill_stats_add_to_drill_list, first drill_num in list = %d ...\n", drill_list_in->drill_num
252  );
253 
254  /* First check for empty list. If empty, then just add this drill */
255  if (drill_list_in->drill_num == -1) {
256  dprintf(" .... In drill_stats_add_to_drill_list, adding first drill, no %d\n", drill_num_in);
257  drill_list_in->drill_num = drill_num_in;
258  drill_list_in->drill_size = drill_size_in;
259  drill_list_in->drill_count = 0;
260  drill_list_in->drill_unit = g_strdup_printf("%s", drill_unit_in);
261  drill_list_in->next = NULL;
262  return;
263  }
264  /* Else check to see if this drill is already in the list */
265  for (drill = drill_list_in; drill != NULL; drill = (gerbv_drill_list_t*)drill->next) {
266  dprintf("checking this drill_num %d against that in list %d.\n", drill_num_in, drill->drill_num);
267  if (drill_num_in == drill->drill_num) {
268  dprintf(" .... In drill_stats_add_to_drill_list, drill no %d already in list\n", drill_num_in);
269  return; /* Found it in list, so return */
270  }
271  drill_last = drill;
272  }
273 
274  /* Now malloc space for new drill list element */
275  if (NULL == (drill_list_new = g_new(gerbv_drill_list_t, 1))) {
276  GERB_FATAL_ERROR("malloc format failed in %s()", __FUNCTION__);
277  }
278 
279  /* Now set various parameters based upon calling args */
280  dprintf(" .... In drill_stats_add_to_drill_list, adding new drill, no %d\n", drill_num_in);
281  drill_list_new->drill_num = drill_num_in;
282  drill_list_new->drill_size = drill_size_in;
283  drill_list_new->drill_count = 0;
284  drill_list_new->drill_unit = g_strdup_printf("%s", drill_unit_in);
285  drill_list_new->next = NULL;
286  drill_last->next = drill_list_new;
287 
288  dprintf(" <---- ... leaving drill_stats_add_to_drill_list.\n");
289  return;
290 }
291 
292 /* ------------------------------------------------------- */
293 void
294 drill_stats_modify_drill_list(
295  gerbv_drill_list_t* drill_list_in, int drill_num_in, double drill_size_in, char* drill_unit_in
296 ) {
297 
298  gerbv_drill_list_t* drill;
299 
300  dprintf(
301  " ---> Entering drill_stats_modify_drill_list, first drill_num in list = %d ...\n", drill_list_in->drill_num
302  );
303 
304  /* Look for this drill num in list */
305  for (drill = drill_list_in; drill != NULL; drill = (gerbv_drill_list_t*)drill->next) {
306  dprintf("checking this drill_num %d against that in list %d.\n", drill_num_in, drill->drill_num);
307  if (drill_num_in == drill->drill_num) {
308  dprintf(" .... Found it, now update it ....\n");
309  drill->drill_size = drill_size_in;
310  if (drill->drill_unit)
311  g_free(drill->drill_unit);
312  drill->drill_unit = g_strdup_printf("%s", drill_unit_in);
313  dprintf(" <---- ... Modified drill. leaving drill_stats_modify_drill_list.\n");
314  return;
315  }
316  }
317  dprintf(" <---- ... Did not find drill. leaving drill_stats_modify_drill_list.\n");
318  return;
319 }
320 
321 /* ------------------------------------------------------- */
322 void
323 drill_stats_increment_drill_counter(gerbv_drill_list_t* drill_list_in, int drill_num_in) {
324 
325  dprintf(" ----> Entering drill_stats_increment_drill_counter......\n");
326  /* First check to see if this drill is already in the list */
327  gerbv_drill_list_t* drill;
328  for (drill = drill_list_in; drill != NULL; drill = drill->next) {
329  if (drill_num_in == drill->drill_num) {
330  drill->drill_count++;
331  dprintf(
332  " .... incrementing drill count. drill_num = %d, drill_count = %d.\n",
333  drill_list_in->drill_num, drill->drill_count
334  );
335  dprintf(" <---- .... Leaving drill_stats_increment_drill_counter after incrementing counter.\n");
336  return;
337  }
338  }
339  dprintf(" <---- .... Leaving drill_stats_increment_drill_counter without incrementing any counter.\n");
340 }
341 
342 /* ------------------------------------------------------- */
343 void
344 drill_stats_add_to_drill_counter(gerbv_drill_list_t* drill_list_in, int drill_num_in, int increment) {
345 
346  gerbv_drill_list_t* drill;
347  for (drill = drill_list_in; drill != NULL; drill = drill->next) {
348  if (drill_num_in == drill->drill_num) {
349  dprintf(" In drill_stats_add_to_drill_counter, adding increment = %d drills to drill list\n", increment);
350  drill->drill_count += increment;
351  return;
352  }
353  }
354 }
355 
356 /* ------------------------------------------------------- */
358 gerbv_drill_stats_new_error_list() {
360 
361  /* Malloc space for new error_list struct. Return NULL if error. */
362  if (NULL == (error_list = g_new(gerbv_error_list_t, 1))) {
363  return NULL;
364  }
365 
366  error_list->layer = -1;
367  error_list->error_text = NULL;
368  error_list->next = NULL;
369  return error_list;
370 }
371 
372 /* ------------------------------------------------------- */
375 void
376 drill_stats_add_error(gerbv_error_list_t* error_list_in, int layer, const char* error_text, gerbv_message_type_t type) {
377  gerbv_stats_add_error(error_list_in, layer, error_text, type);
378 }
Contains basic defines.
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:376
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:140