BRL-CAD
Loading...
Searching...
No Matches
search.h
Go to the documentation of this file.
1/* S E A R C H . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2008-2026 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 db_search
21 * @brief
22 * Functionality for searching .g files
23 */
24/** @{ */
25/** @file include/rt/search.h */
26
27#ifndef RT_SEARCH_H
28#define RT_SEARCH_H
29
30#include "common.h"
31
32#include "bu/glob.h"
33#include "bu/list.h"
34#include "bu/ptbl.h"
35
36#include "rt/db_instance.h"
37#include "rt/defines.h"
38
40
41/**
42 * @brief Search for objects in a geometry database using filters
43 *
44 * The db_search function is a programmatic find-style interface that
45 * lets you search for objects in a geometry database. This function
46 * searches the database using a supplied list of filter criteria.
47 *
48 * The function returns a count of objects matching the filter
49 * criteria and can provide the resulting matches in binary format as
50 * either db_full_path or directory objects depending on the flags
51 * (i.e., depending on whether this is a flat or hierarchical search).
52 *
53 * There are a LOT of filter possibilities. See the search(n) manual
54 * page for details.
55 *
56 * @param[out] results is a bu_ptbl holding either db_full_path or
57 * directory pointers.
58 *
59 * @param flags is a bit field for setting various search options.
60 *
61 * @param filter is a string defining search filters to be used.
62 *
63 * @param path_c is the count of directory paths to be searched.
64 *
65 * @param path_v is one or more directory paths to be searched. If
66 * path_v itself is NULL, all top-level objects are searched
67 *
68 * @param dbip The database instance pointer corresponding to the
69 * current geometry database.
70 *
71 * @param clbk Optional callback function to call for -exec
72 *
73 * @param u1 Optional user data pointer
74 *
75 * @param u2 Optional user data pointer
76 *
77 * @return Negative return values indicate a problem with the search,
78 * and non-negative values indicate a successful search. Non-negative
79 * values correspond with the number of objects found.
80 *
81 * @retval -2 Return code when db_search is called with a NULL dbip.
82 * @retval -1 Return code when the plan search string is invalid.
83 * @retval 0 Return code when the search completed successfully but no matches were found.
84 * @retval >0 Return code when the search completed successfully and matched one or more objects.
85 *
86 * The following example assumes a database instance pointer (dbip) is
87 * available and ready to use.
88 *
89 * @code
90 * size_t i = 0;
91 * struct bu_ptbl results = BU_PTBL_INIT_ZERO;
92 * const char *plan = "-name *.s -or -below -type region";
93 * int matches = db_search(&results, DB_SEARCH_HIDDEN | DB_SEARCH_QUIET , plan, 0, NULL, dbip, ctx);
94 * for (i = 0; matches > 0 && i < BU_PTBL_LEN(&results); i++) {
95 * char *path_str = db_path_to_string((struct db_full_path *)BU_PTBL_GET(&results, i));
96 * bu_log("%s\n", path_str);
97 * bu_free(path_str, "free db_fullpath_to_string allocation");
98 * }
99 * db_search_free(&results);
100 * @endcode
101 *
102 * Note:
103 * Be aware that if you are using db_search to filter pre-built lists of paths,
104 * you need to check that your generated path list is NOT empty before calling
105 * db_search. If you accidentally send an empty path list into db_search,
106 * it will assume you wanted a tops list, which has a good chance of returning
107 * unwanted results.
108 *
109 */
111 int flags,
112 const char *filter,
113 int path_c,
114 struct directory **path_v,
115 struct db_i *dbip,
117 void *u1,
118 void *u2
119 );
120
121/* These are the possible search flags. */
122#define DB_SEARCH_TREE 0x0 /**< @brief Do a hierarchy-aware search. This is the default. */
123#define DB_SEARCH_FLAT 0x1 /**< @brief Do a flat search without hierarchy */
124#define DB_SEARCH_HIDDEN 0x2 /**< @brief Search using hidden objects */
125#define DB_SEARCH_RETURN_UNIQ_DP 0x4 /**< @brief Return the set of unique directory pointers instead of full paths */
126#define DB_SEARCH_QUIET 0x8 /**< @brief Silence all warnings */
127#define DB_SEARCH_PRINT_TOTAL 0x10 /**< @brief Print total number of items found in search */
128
129/**
130 * Properly free the table contents returned by db_search. The bu_ptbl
131 * itself, if not put on the stack, will need to be freed by the same
132 * calling function that allocated it.
133 */
135
136
137/* db_ls.c */
138/**
139 * db_ls takes a database instance pointer and assembles a directory
140 * pointer array of objects in the database according to a set of
141 * flags. An optional pattern can be supplied for match filtering via
142 * globbing rules (see bu_path_match()). If pattern is NULL,
143 * filtering is performed using only the flags.
144 *
145 * The caller is responsible for freeing the array.
146 *
147 * Returns -
148 * integer count of objects in dpv
149 * struct directory ** array of objects in dpv via argument
150 *
151 */
152RT_EXPORT extern size_t db_ls(const struct db_i *dbip,
153 int flags,
154 const char *pattern,
155 struct directory ***dpv);
156
157/* These are the possible listing flags. */
158#define DB_LS_PRIM 0x1 /**< @brief filter for primitives (solids)*/
159#define DB_LS_COMB 0x2 /**< @brief filter for combinations */
160#define DB_LS_REGION 0x4 /**< @brief filter for regions */
161#define DB_LS_HIDDEN 0x8 /**< @brief include hidden objects in results */
162#define DB_LS_NON_GEOM 0x10 /**< @brief filter for non-geometry objects */
163#define DB_LS_TOPS 0x20 /**< @brief filter for objects un-referenced by other objects */
164#define DB_LS_CYCLIC 0x40 /**< @brief filter for objects with a cyclic reference in subtrees */
165#define DB_LS_PHONY 0x80 /**< @brief enable and filter for objects such as the nirt display list entries */
166/* TODO - implement this flag
167 #define DB_LS_REGEX 0x100*/ /* interpret pattern using regex rules, instead of
168 globbing rules (default) */
169
170/* cyclic.c */
171/**
172 * db_cyclic_paths searches for cyclic paths in the database, either in all
173 * objects or checking whether a specific dp is cyclic within its subtree.
174 *
175 * If sdp is NULL, ALL directory pointers in the database are checked. This is
176 * a complete validation of the whole .g file, and the only way to
177 * comprehensively search for any cyclic paths present. The return count will
178 * be the number of combs with a cyclic reference in their subtrees.
179 *
180 * If sdp is non-NULL, the search will be limited to checking only the tree
181 * below sdp for a cyclic reference to sdp.
182 *
183 * If a cyclic_paths is non-NULL it will be used to return db_fullpath entries
184 * for the cyclic paths found.
185 */
186RT_EXPORT extern int db_cyclic_paths(struct bu_ptbl *cyclic_paths, const struct db_i *dbip, struct directory *sdp);
188
189/**
190 * Expand a glob pattern against the geometry database.
191 *
192 * Uses the bu_glob callback API with a geometry-database backend so
193 * that patterns such as "*.s" or "vehicle*\/wheel*" are matched against
194 * object names in the database rather than the filesystem.
195 *
196 * Matching results accumulate in @a gp->gl_pathv / @a gp->gl_pathc.
197 * The context must be initialised with bu_glob_ctx_create() before the first
198 * call and released with bu_glob_ctx_destroy() after the last. Passing
199 * BU_GLOB_APPEND in @a flags appends to any results already in @a gp.
200 *
201 * Flat patterns (no '/') match all objects in the database by name.
202 * Hierarchical patterns ('/' separated) walk the combination tree
203 * starting from the top-level objects in the database.
204 *
205 * @param[in,out] gp Initialised glob context.
206 * @param[in] pattern Glob pattern string.
207 * @param[in] flags BU_GLOB_* flags (see bu/glob.h).
208 * @param[in] dbip Geometry database instance.
209 * @return 0 on success (possibly with no matches), negative on error.
210 */
211RT_EXPORT extern int db_path_glob(struct bu_glob_context *gp,
212 const char *pattern,
213 int flags,
214 const struct db_i *dbip);
215
216
217/* Deprecated */
218typedef int(*db_search_callback_t)(int, const char*[],void*);
220 db_search_callback_t _e_callback; /**< @brief A function that evaluates an array of strings and returns a boolean. */
221 void *_e_userdata; /**< @brief A pointer that will be passed to the callback, usually a pointer to an interpreter. */
223RT_EXPORT extern struct db_search_context *db_search_context_create(void); /* FIXME: is this really needed? why not just use the struct directly from the stack or let the user handle allocation? */
228 int flags,
229 const char *filter,
230 int path_c,
231 struct directory **path_v,
232 struct db_i *dbip,
233 struct db_search_context *ctx
234 );
235
237
238#endif /* RT_SEARCH_H*/
239/** @} */
240/*
241 * Local Variables:
242 * mode: C
243 * tab-width: 8
244 * indent-tabs-mode: t
245 * c-file-style: "stroustrup"
246 * End:
247 * ex: shiftwidth=4 tabstop=8
248 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
int(* bu_clbk_t)(int, const char **, void *, void *)
Definition defines.h:204
int(* db_search_callback_t)(int, const char *[], void *)
Definition search.h:219
int db_search_old(struct bu_ptbl *results, int flags, const char *filter, int path_c, struct directory **path_v, struct db_i *dbip, struct db_search_context *ctx)
int db_search(struct bu_ptbl *results, int flags, const char *filter, int path_c, struct directory **path_v, struct db_i *dbip, bu_clbk_t clbk, void *u1, void *u2)
Search for objects in a geometry database using filters.
int db_path_glob(struct bu_glob_context *gp, const char *pattern, int flags, const struct db_i *dbip)
void db_search_register_data(struct db_search_context *, void *)
struct db_search_context * db_search_context_create(void)
void db_search_free(struct bu_ptbl *search_results)
size_t db_ls(const struct db_i *dbip, int flags, const char *pattern, struct directory ***dpv)
void db_search_register_exec(struct db_search_context *, db_search_callback_t)
void db_search_context_destroy(struct db_search_context *ctx)
int db_cyclic_paths(struct bu_ptbl *cyclic_paths, const struct db_i *dbip, struct directory *sdp)
Definition ptbl.h:53
db_search_callback_t _e_callback
A function that evaluates an array of strings and returns a boolean.
Definition search.h:221
void * _e_userdata
A pointer that will be passed to the callback, usually a pointer to an interpreter.
Definition search.h:222