BRL-CAD
summary.c
Go to the documentation of this file.
1 /* S U M M A R Y . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libged/summary.c
21  *
22  * The summary command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 
32 #include "./ged_private.h"
33 
34 
35 /*
36  * Summarize the contents of the directory by categories
37  * (solid, comb, region). If flag is != 0, it is interpreted
38  * as a request to print all the names in that category (e.g., RT_DIR_SOLID).
39  */
40 static void
41 summary_dir(struct ged *gedp,
42  int flag)
43 {
44  struct directory *dp;
45  int i;
46  static int sol, comb, reg;
47  struct directory **dirp;
48  struct directory **dirp0 = (struct directory **)NULL;
49 
50  sol = comb = reg = 0;
51  for (i = 0; i < RT_DBNHASH; i++) {
52  for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
53  if (dp->d_flags & RT_DIR_SOLID)
54  sol++;
55  if (dp->d_flags & RT_DIR_COMB) {
56  if (dp->d_flags & RT_DIR_REGION)
57  reg++;
58  else
59  comb++;
60  }
61  }
62  }
63 
64  bu_vls_printf(gedp->ged_result_str, "Summary:\n");
65  bu_vls_printf(gedp->ged_result_str, " %5d primitives\n", sol);
66  bu_vls_printf(gedp->ged_result_str, " %5d region; %d non-region combinations\n", reg, comb);
67  bu_vls_printf(gedp->ged_result_str, " %5d total objects\n\n", sol+reg+comb);
68 
69  if (flag == 0)
70  return;
71 
72  /* Print all names matching the flags parameter */
73  /* THIS MIGHT WANT TO BE SEPARATED OUT BY CATEGORY */
74 
75  dirp = _ged_dir_getspace(gedp->ged_wdbp->dbip, 0);
76  dirp0 = dirp;
77  /*
78  * Walk the directory list adding pointers (to the directory entries
79  * of interest) to the array
80  */
81  for (i = 0; i < RT_DBNHASH; i++)
82  for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw)
83  if (dp->d_flags & flag)
84  *dirp++ = dp;
85 
86  _ged_vls_col_pr4v(gedp->ged_result_str, dirp0, (int)(dirp - dirp0), 0);
87  bu_free((void *)dirp0, "dir_getspace");
88 }
89 
90 
91 int
92 ged_summary(struct ged *gedp, int argc, const char *argv[])
93 {
94  char *cp;
95  int flags = 0;
96  static const char *usage = "[p r g]";
97 
99  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
100 
101  /* initialize result */
102  bu_vls_trunc(gedp->ged_result_str, 0);
103 
104  if (argc == 1) {
105  summary_dir(gedp, 0);
106  return GED_OK;
107  }
108 
109  if (2 < argc) {
110  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
111  return GED_ERROR;
112  }
113 
114  cp = (char *)argv[1];
115  while (*cp) {
116  switch (*cp++) {
117  case 'p':
118  flags |= RT_DIR_SOLID;
119  break;
120  case 'r':
121  flags |= RT_DIR_REGION;
122  break;
123  case 'g':
124  flags |= RT_DIR_COMB;
125  break;
126  default:
127  bu_vls_printf(gedp->ged_result_str, "%s: p, r or g are the only valid parameters\n", argv[0]);
128  return GED_ERROR;
129  }
130  }
131 
132  summary_dir(gedp, flags);
133  return GED_OK;
134 }
135 
136 
137 /*
138  * Local Variables:
139  * tab-width: 8
140  * mode: C
141  * indent-tabs-mode: t
142  * c-file-style: "stroustrup"
143  * End:
144  * ex: shiftwidth=4 tabstop=8
145  */
void _ged_vls_col_pr4v(struct bu_vls *vls, struct directory **list_of_names, size_t num_in_list, int no_decorate)
Definition: ls.c:84
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
#define RT_DBNHASH
hash table is an array of linked lists with this many array pointer elements (Memory use for 32-bit: ...
Definition: raytrace.h:755
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define RT_DIR_REGION
region
Definition: raytrace.h:885
struct directory * d_forw
link to next dir entry
Definition: raytrace.h:864
#define GED_ERROR
Definition: ged.h:61
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define RT_DIR_SOLID
this name is a solid
Definition: raytrace.h:883
struct bu_vls * ged_result_str
Definition: ged.h:357
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
struct directory * dbi_Head[RT_DBNHASH]
Definition: raytrace.h:814
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define RT_DIR_NULL
Definition: raytrace.h:875
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
struct directory ** _ged_dir_getspace(struct db_i *dbip, int num_entries)
Definition: tops.c:140
int ged_summary(struct ged *gedp, int argc, const char *argv[])
Definition: summary.c:92
int d_flags
flags
Definition: raytrace.h:869