BRL-CAD
ls.c
Go to the documentation of this file.
1 /* L S . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-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 /** @addtogroup dbio */
21 /** @{ */
22 /** @file librt/ls.c
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 #include "bio.h"
30 
31 #include "vmath.h"
32 #include "db.h"
33 #include "raytrace.h"
34 
35 HIDDEN int
36 dp_eval_flags(struct directory *dp, int flags)
37 {
38  int flag_eval = 0;
39 
40  /* TODO - do we ever write these? */
41  if (dp->d_addr == RT_DIR_PHONY_ADDR) return 0;
42 
43  /* Unless we're explicitly listing hidden objects, it's game over if the
44  * hidden flag is set */
45  if (!(flags & DB_LS_HIDDEN) && (dp->d_flags & RT_DIR_HIDDEN)) return 0;
46 
47  /* For any other flags that were provided, if we don't match them we don't return
48  * true. If no flags are present, we default to true. */
49  if (flags & DB_LS_PRIM) { flag_eval += (dp->d_flags & RT_DIR_SOLID) ? 0 : 1; }
50  if (flags & DB_LS_COMB) { flag_eval += (dp->d_flags & RT_DIR_COMB) ? 0 : 1; }
51  if (flags & DB_LS_REGION) { flag_eval += (dp->d_flags & RT_DIR_REGION) ? 0 : 1; }
52  if (flags & DB_LS_NON_GEOM) { flag_eval += (dp->d_flags & RT_DIR_NON_GEOM) ? 0 : 1; }
53  if (flags & DB_LS_TOPS) { flag_eval += (dp->d_nref == 0) ? 0 : 1; }
54  return (flag_eval) ? 0 : 1;
55 }
56 
57 size_t
58 db_ls(const struct db_i *dbip, int flags, const char *pattern, struct directory ***dpv)
59 {
60  size_t i;
61  size_t objcount = 0;
62  struct directory *dp;
63 
64  RT_CK_DBI(dbip);
65 
66  for (i = 0; i < RT_DBNHASH; i++) {
67  for (dp = dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
68  objcount += dp_eval_flags(dp, flags);
69  }
70  }
71  if (objcount > 0) {
72  (*dpv) = (struct directory **)bu_malloc(sizeof(struct directory *) * (objcount + 1), "directory pointer array");
73  objcount = 0;
74  for (i = 0; i < RT_DBNHASH; i++) {
75  for (dp = dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
76  if (dp_eval_flags(dp,flags)) {
77  if (!pattern || !bu_fnmatch(pattern, dp->d_namep, 0)) {
78  (*dpv)[objcount] = dp;
79  objcount++;
80  }
81  }
82  }
83  }
84  (*dpv)[objcount] = NULL;
85  }
86  return objcount;
87 }
88 
89 struct directory **
90 db_argv_to_dpv(const struct db_i *dbip, const char **argv)
91 {
92  struct directory **dpv = NULL;
93  struct directory *dp = RT_DIR_NULL;
94  int argv_cnt = 0;
95  const char *arg = argv[0];
96  if (!argv) return dpv;
97  while (arg) {
98  argv_cnt++;
99  arg = argv[argv_cnt];
100  }
101  if (argv_cnt > 0) {
102  dpv = (struct directory **)bu_malloc(sizeof(struct directory *) * (argv_cnt + 1), "directory pointer array");
103  argv_cnt = 0;
104  arg = argv[0];
105  while (arg) {
106  dp = db_lookup(dbip, arg, LOOKUP_QUIET);
107  if (dp == RT_DIR_NULL) {
108  /* Doesn't exist in the .g file - create an empty structure */
109  BU_ALLOC(dpv[argv_cnt], struct directory);
110  dpv[argv_cnt]->d_addr = RT_DIR_PHONY_ADDR;
111  dpv[argv_cnt]->d_namep = bu_strdup(arg);
112  } else {
113  dpv[argv_cnt] = dp;
114  }
115  argv_cnt++;
116  arg = argv[argv_cnt];
117  }
118  }
119  return dpv;
120 }
121 
122 char **
124 {
125  char **argv = NULL;
126  int dpv_cnt = 0;
127  struct directory *dp = dpv[dpv_cnt];
128  if (!dpv) return argv;
129  while (dp) {
130  dpv_cnt++;
131  dp = dpv[dpv_cnt];
132  }
133  if (dpv_cnt > 0) {
134  argv = (char **)bu_malloc(sizeof(char *) * (dpv_cnt + 1), "char pointer array");
135  dpv_cnt = 0;
136  dp = dpv[0];
137  while (dpv[dpv_cnt]) {
138  argv[dpv_cnt] = dpv[dpv_cnt]->d_namep;
139  dpv_cnt++;
140  }
141  }
142  return argv;
143 }
144 
145 
146 /** @} */
147 /*
148  * Local Variables:
149  * mode: C
150  * tab-width: 8
151  * indent-tabs-mode: t
152  * c-file-style: "stroustrup"
153  * End:
154  * ex: shiftwidth=4 tabstop=8
155  */
char * d_namep
pointer to name string
Definition: raytrace.h:859
Definition: raytrace.h:800
#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
size_t db_ls(const struct db_i *dbip, int flags, const char *pattern, struct directory ***dpv)
Definition: ls.c:58
long d_nref
times ref'ed by COMBs
Definition: raytrace.h:868
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
Header file for the BRL-CAD common definitions.
#define RT_DIR_REGION
region
Definition: raytrace.h:885
struct directory ** db_argv_to_dpv(const struct db_i *dbip, const char **argv)
Definition: ls.c:90
struct directory * d_forw
link to next dir entry
Definition: raytrace.h:864
#define HIDDEN
Definition: common.h:86
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
#define DB_LS_REGION
Definition: raytrace.h:4654
char ** db_dpv_to_argv(struct directory **dpv)
Definition: ls.c:123
#define RT_DIR_NON_GEOM
object is not geometry (e.g. binary object)
Definition: raytrace.h:887
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
#define RT_DIR_SOLID
this name is a solid
Definition: raytrace.h:883
#define LOOKUP_QUIET
Definition: raytrace.h:893
#define DB_LS_TOPS
Definition: raytrace.h:4657
int bu_fnmatch(const char *pattern, const char *pathname, int flags)
Definition: fnmatch.c:311
#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
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
HIDDEN int dp_eval_flags(struct directory *dp, int flags)
Definition: ls.c:36
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
struct directory * dbi_Head[RT_DBNHASH]
Definition: raytrace.h:814
#define RT_DIR_NULL
Definition: raytrace.h:875
#define DB_LS_COMB
Definition: raytrace.h:4653
#define DB_LS_PRIM
Definition: raytrace.h:4652
int d_flags
flags
Definition: raytrace.h:869
#define DB_LS_NON_GEOM
Definition: raytrace.h:4656
#define bu_strdup(s)
Definition: str.h:71
#define DB_LS_HIDDEN
Definition: raytrace.h:4655