BRL-CAD
tops.c
Go to the documentation of this file.
1 /* T O P S . 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/tops.c
21  *
22  * The tops command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 #include "bu/getopt.h"
32 
33 #include "./ged_private.h"
34 
35 
36 int
37 ged_tops(struct ged *gedp, int argc, const char *argv[])
38 {
39  struct directory *dp;
40  int i;
41  struct directory **dirp;
42  struct directory **dirp0 = (struct directory **)NULL;
43  int c;
44 
45  int no_decorate = 0;
46  int aflag = 0;
47  int hflag = 0;
48  int pflag = 0;
49 
50  /* static const char *usage = "[-a|-h|-n|-p]"; */
51 
53  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
54 
55  /* initialize result */
56  bu_vls_trunc(gedp->ged_result_str, 0);
57 
58  /* process any options */
59  bu_optind = 1; /* re-init bu_getopt() */
60  while ((c = bu_getopt(argc, (char * const *)argv, "ahnp")) != -1) {
61  switch (c) {
62  case 'a':
63  aflag = 1;
64  break;
65  case 'h':
66  hflag = 1;
67  break;
68  case 'n':
69  no_decorate = 1;
70  break;
71  case 'p':
72  pflag = 1;
73  break;
74  default:
75  break;
76  }
77  }
78 
79  argc -= (bu_optind - 1);
80  argv += (bu_optind - 1);
81 
82  /* Can this be executed only sometimes?
83  Perhaps a "dirty bit" on the database? */
85 
86  /*
87  * Find number of possible entries and allocate memory
88  */
89  dirp = _ged_dir_getspace(gedp->ged_wdbp->dbip, 0);
90  dirp0 = dirp;
91 
92  if (db_version(gedp->ged_wdbp->dbip) < 5) {
93  for (i = 0; i < RT_DBNHASH; i++)
94  for (dp = gedp->ged_wdbp->dbip->dbi_Head[i];
95  dp != RT_DIR_NULL;
96  dp = dp->d_forw) {
97  if (dp->d_nref == 0)
98  *dirp++ = dp;
99  }
100  } else {
101  for (i = 0; i < RT_DBNHASH; i++)
102  for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
103 
104  if (dp->d_nref != 0) {
105  continue;
106  }
107 
108  if ((aflag) ||
109  (hflag && (dp->d_flags & RT_DIR_HIDDEN)) ||
110  (pflag && dp->d_addr == RT_DIR_PHONY_ADDR)) {
111 
112  /* add object because it matches an option */
113  *dirp++ = dp;
114 
115  } else if (!aflag && !hflag && !pflag &&
116  !(dp->d_flags & RT_DIR_HIDDEN) &&
117  (dp->d_addr != RT_DIR_PHONY_ADDR)) {
118 
119  /* add non-hidden real object */
120  *dirp++ = dp;
121 
122  }
123  }
124  }
125 
126  _ged_vls_col_pr4v(gedp->ged_result_str, dirp0, (int)(dirp - dirp0), no_decorate);
127  bu_free((void *)dirp0, "wdb_tops_cmd: wdb_dir_getspace");
128 
129  return GED_OK;
130 }
131 
132 
133 /*
134  * This routine walks through the directory entry list and mallocs enough
135  * space for pointers to hold:
136  * a) all of the entries if called with an argument of 0, or
137  * b) the number of entries specified by the argument if > 0.
138  */
139 struct directory **
140 _ged_dir_getspace(struct db_i *dbip,
141  int num_entries)
142 {
143  struct directory *dp;
144  int i;
145  struct directory **dir_basep;
146 
147  if (num_entries < 0) {
148  bu_log("dir_getspace: was passed %d, used 0\n",
149  num_entries);
150  num_entries = 0;
151  }
152  if (num_entries == 0) {
153  /* Set num_entries to the number of entries */
154  for (i = 0; i < RT_DBNHASH; i++)
155  for (dp = dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw)
156  num_entries++;
157  }
158 
159  /* Allocate and cast num_entries worth of pointers */
160  dir_basep = (struct directory **) bu_malloc((num_entries+1) * sizeof(struct directory *),
161  "dir_getspace *dir[]");
162  return dir_basep;
163 }
164 
165 
166 /*
167  * Local Variables:
168  * tab-width: 8
169  * mode: C
170  * indent-tabs-mode: t
171  * c-file-style: "stroustrup"
172  * End:
173  * ex: shiftwidth=4 tabstop=8
174  */
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
#define GED_OK
Definition: ged.h:55
Definition: raytrace.h:800
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
#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
long d_nref
times ref'ed by COMBs
Definition: raytrace.h:868
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
int db_version(struct db_i *dbip)
Definition: db5_scan.c:414
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
int bu_optind
Definition: globals.c:89
struct directory * d_forw
link to next dir entry
Definition: raytrace.h:864
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
#define GED_ERROR
Definition: ged.h:61
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define RT_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
#define RT_DIR_HIDDEN
object name is hidden
Definition: raytrace.h:886
struct bu_vls * ged_result_str
Definition: ged.h:357
int ged_tops(struct ged *gedp, int argc, const char *argv[])
Definition: tops.c:37
struct directory ** _ged_dir_getspace(struct db_i *dbip, int num_entries)
Definition: tops.c:140
struct directory * dbi_Head[RT_DBNHASH]
Definition: raytrace.h:814
#define RT_DIR_NULL
Definition: raytrace.h:875
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
int d_flags
flags
Definition: raytrace.h:869
void db_update_nref(struct db_i *dbip, struct resource *resp)
Definition: db_match.c:75