BRL-CAD
db_match.c
Go to the documentation of this file.
1 /* D B _ M A T C H . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1994-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/db_match.c
23  *
24  * Routines to determine if a string matches a given pattern.
25  *
26  */
27 
28 #include "common.h"
29 
30 #include "bio.h"
31 
32 #include "vmath.h"
33 #include "db.h"
34 #include "rtgeom.h"
35 #include "raytrace.h"
36 
37 /* Deprecated - use db_ls */
38 int
39 db_regexp_match_all(struct bu_vls *dest, struct db_i *dbip, const char *pattern)
40 {
41  register int i, num;
42  register struct directory *dp;
43 
44  for (i = num = 0; i < RT_DBNHASH; i++) {
45  for (dp = dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
46  if (bu_fnmatch(pattern, dp->d_namep, 0) != 0)
47  continue;
48  if (num == 0)
49  bu_vls_strcat(dest, dp->d_namep);
50  else {
51  bu_vls_strcat(dest, " ");
52  bu_vls_strcat(dest, dp->d_namep);
53  }
54  ++num;
55  }
56  }
57 
58  return num;
59 }
60 
61 
62 HIDDEN void
63 db_count_refs(struct db_i *dbip, struct rt_comb_internal *comb, union tree *comb_leaf, void *UNUSED(dummy1), void *UNUSED(dummy2), void *UNUSED(dummy3), void *UNUSED(dummy4))
64 {
65  struct directory *dp;
66 
67  if (comb) RT_CK_COMB(comb);
68  RT_CK_TREE(comb_leaf);
69 
70  if ((dp=db_lookup(dbip, comb_leaf->tr_l.tl_name, LOOKUP_QUIET)) != RT_DIR_NULL)
71  ++dp->d_nref;
72 }
73 
74 void
75 db_update_nref(struct db_i *dbip, struct resource *resp)
76 {
77  register int i;
78  register struct directory *dp;
79  struct rt_db_internal intern;
80  struct rt_comb_internal *comb;
81 
82  RT_CK_DBI(dbip);
83  RT_CK_RESOURCE(resp);
84 
85  /* First, clear any existing counts */
86  for (i = 0; i < RT_DBNHASH; i++)
87  for (dp = dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw)
88  dp->d_nref = 0;
89 
90  /* Examine all COMB nodes */
91  for (i = 0; i < RT_DBNHASH; i++) {
92  for (dp = dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
93 
94  /* handle non-combination objects that reference other objects */
95  if (dp->d_major_type == DB5_MAJORTYPE_BRLCAD) {
96  struct directory *dp2;
97 
98  /* initialize for good measure */
99  RT_DB_INTERNAL_INIT(&intern);
100 
101  if (dp->d_minor_type == DB5_MINORTYPE_BRLCAD_EXTRUDE) {
102  struct rt_extrude_internal *extr;
103 
104  if (rt_db_get_internal(&intern, dp, dbip, NULL, resp) < 0)
105  continue;
106  extr = (struct rt_extrude_internal *)intern.idb_ptr;
107  RT_EXTRUDE_CK_MAGIC(extr);
108  if (extr->sketch_name) {
109  dp2 = db_lookup(dbip, extr->sketch_name, LOOKUP_QUIET);
110  if (dp2 != RT_DIR_NULL) {
111  dp2->d_nref++;
112  }
113  }
114  rt_db_free_internal(&intern);
115  } else if (dp->d_minor_type == DB5_MINORTYPE_BRLCAD_REVOLVE) {
116  struct rt_revolve_internal *revolve;
117 
118  if (rt_db_get_internal(&intern, dp, dbip, NULL, resp) < 0)
119  continue;
120  revolve = (struct rt_revolve_internal *)intern.idb_ptr;
121  RT_REVOLVE_CK_MAGIC(revolve);
122  if (bu_vls_strlen(&revolve->sketch_name) > 0) {
123  dp2 = db_lookup(dbip, bu_vls_addr(&revolve->sketch_name), LOOKUP_QUIET);
124  if (dp2 != RT_DIR_NULL) {
125  dp2->d_nref++;
126  }
127  }
128  rt_db_free_internal(&intern);
129  } else if (dp->d_minor_type == DB5_MINORTYPE_BRLCAD_DSP) {
130  struct rt_dsp_internal *dsp;
131 
132  if (rt_db_get_internal(&intern, dp, dbip, NULL, resp) < 0)
133  continue;
134  dsp = (struct rt_dsp_internal *)intern.idb_ptr;
135  RT_DSP_CK_MAGIC(dsp);
136  if (dsp->dsp_datasrc == RT_DSP_SRC_OBJ && bu_vls_strlen(&dsp->dsp_name) > 0) {
137  dp2 = db_lookup(dbip, bu_vls_addr(&dsp->dsp_name), LOOKUP_QUIET);
138  if (dp2 != RT_DIR_NULL) {
139  dp2->d_nref++;
140  }
141  }
142  rt_db_free_internal(&intern);
143  }
144  }
145  if (!(dp->d_flags & RT_DIR_COMB))
146  continue;
147  if (rt_db_get_internal(&intern, dp, dbip, NULL, resp) < 0)
148  continue;
149  if (intern.idb_type != ID_COMBINATION) {
150  bu_log("NOTICE: %s was marked a combination, but isn't one? Clearing flag\n",
151  dp->d_namep);
152  dp->d_flags &= ~RT_DIR_COMB;
153  rt_db_free_internal(&intern);
154  continue;
155  }
156  comb = (struct rt_comb_internal *)intern.idb_ptr;
157  db_tree_funcleaf(dbip, comb, comb->tree,
158  db_count_refs, (void *)NULL,
159  (void *)NULL, (void *)NULL, (void *)NULL);
160  rt_db_free_internal(&intern);
161  }
162  }
163 }
164 
165 
166 /** @} */
167 /*
168  * Local Variables:
169  * mode: C
170  * tab-width: 8
171  * indent-tabs-mode: t
172  * c-file-style: "stroustrup"
173  * End:
174  * ex: shiftwidth=4 tabstop=8
175  */
char * d_namep
pointer to name string
Definition: raytrace.h:859
Definition: raytrace.h:800
unsigned char d_major_type
object major type
Definition: raytrace.h:870
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int rt_db_get_internal(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
Definition: dir.c:76
#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
HIDDEN void db_count_refs(struct db_i *dbip, struct rt_comb_internal *comb, union tree *comb_leaf, void *dummy1, void *dummy2, void *dummy3, void *dummy4)
Definition: db_match.c:63
long d_nref
times ref'ed by COMBs
Definition: raytrace.h:868
void db_update_nref(struct db_i *dbip, struct resource *resp)
Definition: db_match.c:75
void bu_vls_strcat(struct bu_vls *vp, const char *s)
Definition: vls.c:368
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
#define RT_CK_COMB(_p)
Definition: raytrace.h:955
int db_regexp_match_all(struct bu_vls *dest, struct db_i *dbip, const char *pattern)
Definition: db_match.c:39
Header file for the BRL-CAD common definitions.
#define ID_COMBINATION
Combination Record.
Definition: raytrace.h:499
struct directory * d_forw
link to next dir entry
Definition: raytrace.h:864
#define HIDDEN
Definition: common.h:86
unsigned char d_minor_type
object minor type
Definition: raytrace.h:871
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
#define LOOKUP_QUIET
Definition: raytrace.h:893
int bu_fnmatch(const char *pattern, const char *pathname, int flags)
Definition: fnmatch.c:311
size_t bu_vls_strlen(const struct bu_vls *vp)
Definition: vls.c:189
char * tl_name
Name of this leaf (bu_strdup'ed)
Definition: raytrace.h:1174
#define UNUSED(parameter)
Definition: common.h:239
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
struct tree::tree_db_leaf tr_l
void * idb_ptr
Definition: raytrace.h:195
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
struct directory * dbi_Head[RT_DBNHASH]
Definition: raytrace.h:814
#define RT_CK_RESOURCE(_p)
Definition: raytrace.h:1490
union tree * tree
Leading to tree_db_leaf leaves.
Definition: raytrace.h:938
#define RT_DIR_NULL
Definition: raytrace.h:875
void db_tree_funcleaf(struct db_i *dbip, struct rt_comb_internal *comb, union tree *comb_tree, void(*leaf_func)(struct db_i *, struct rt_comb_internal *, union tree *, void *, void *, void *, void *), void *user_ptr1, void *user_ptr2, void *user_ptr3, void *user_ptr4)
Definition: db_tree.c:640
#define RT_CK_TREE(_p)
Definition: raytrace.h:1182
int d_flags
flags
Definition: raytrace.h:869
Definition: vls.h:56
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216