BRL-CAD
find.c
Go to the documentation of this file.
1 /* F I N D . 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/find.c
21  *
22  * The find 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 HIDDEN void
37 find_ref(struct db_i *dbip,
38  struct rt_comb_internal *comb,
39  union tree *comb_leaf,
40  void *object,
41  void *comb_name_ptr,
42  void *user_ptr3,
43  void *UNUSED(user_ptr4))
44 {
45  char *obj_name;
46  char *comb_name;
47  struct ged *gedp = (struct ged *)user_ptr3;
48 
49  if (dbip) RT_CK_DBI(dbip);
50  if (comb) RT_CK_COMB(comb);
51  RT_CK_TREE(comb_leaf);
52 
53  obj_name = (char *)object;
54  if (!BU_STR_EQUAL(comb_leaf->tr_l.tl_name, obj_name))
55  return;
56 
57  comb_name = (char *)comb_name_ptr;
58 
59  bu_vls_printf(gedp->ged_result_str, "%s ", comb_name);
60 }
61 
62 
63 int
64 ged_find(struct ged *gedp, int argc, const char *argv[])
65 {
66  int i, k;
67  struct directory *dp;
68  struct rt_db_internal intern;
69  struct rt_comb_internal *comb=(struct rt_comb_internal *)NULL;
70  int c;
71  int aflag = 0; /* look at all objects */
72  static const char *usage = "<objects>";
73 
75  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
76 
77  /* initialize result */
78  bu_vls_trunc(gedp->ged_result_str, 0);
79 
80  /* must be wanting help */
81  if (argc == 1) {
82  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
83  return GED_HELP;
84  }
85 
86  bu_optind = 1; /* re-init bu_getopt() */
87  while ((c = bu_getopt(argc, (char * const *)argv, "a")) != -1) {
88  switch (c) {
89  case 'a':
90  aflag = 1;
91  break;
92  default:
93  bu_vls_printf(gedp->ged_result_str, "Unrecognized option - %c", c);
94  return GED_ERROR;
95  }
96  }
97  argc -= (bu_optind - 1);
98  argv += (bu_optind - 1);
99 
100  /* Examine all COMB nodes */
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  if (!(dp->d_flags & RT_DIR_COMB) ||
104  (!aflag && (dp->d_flags & RT_DIR_HIDDEN)))
105  continue;
106 
107  if (rt_db_get_internal(&intern,
108  dp,
109  gedp->ged_wdbp->dbip,
110  (fastf_t *)NULL,
111  &rt_uniresource) < 0) {
112  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting");
113  return GED_ERROR;
114  }
115 
116  comb = (struct rt_comb_internal *)intern.idb_ptr;
117  for (k = 1; k < argc; k++)
119  comb,
120  comb->tree,
121  find_ref,
122  (void *)argv[k],
123  (void *)dp->d_namep,
124  (void *)gedp,
125  (void *)NULL);
126 
127  rt_db_free_internal(&intern);
128  }
129  }
130 
131  return GED_OK;
132 }
133 
134 
135 /*
136  * Local Variables:
137  * tab-width: 8
138  * mode: C
139  * indent-tabs-mode: t
140  * c-file-style: "stroustrup"
141  * End:
142  * ex: shiftwidth=4 tabstop=8
143  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
Definition: raytrace.h:800
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
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
#define RT_CK_COMB(_p)
Definition: raytrace.h:955
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
int bu_optind
Definition: globals.c:89
int ged_find(struct ged *gedp, int argc, const char *argv[])
Definition: find.c:64
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
#define HIDDEN
Definition: common.h:86
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_HIDDEN
object name is hidden
Definition: raytrace.h:886
char * tl_name
Name of this leaf (bu_strdup'ed)
Definition: raytrace.h:1174
#define UNUSED(parameter)
Definition: common.h:239
struct bu_vls * ged_result_str
Definition: ged.h:357
#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
union tree * tree
Leading to tree_db_leaf leaves.
Definition: raytrace.h:938
HIDDEN void find_ref(struct db_i *dbip, struct rt_comb_internal *comb, union tree *comb_leaf, void *object, void *comb_name_ptr, void *user_ptr3, void *user_ptr4)
Definition: find.c:37
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
#define GED_HELP
Definition: ged.h:62
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
double fastf_t
Definition: defines.h:300
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126