BRL-CAD
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-2024 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/list.h"
33 #include "bu/ptbl.h"
34 
35 #include "rt/db_instance.h"
36 #include "rt/defines.h"
37 
38 __BEGIN_DECLS
39 
40 typedef int(*db_search_callback_t)(int, const char*[],void*);
41 
42 /**
43  * @brief Execution context for the -exec filter.
44  */
46  db_search_callback_t _e_callback; /**< @brief A function that evaluates an array of strings and returns a boolean. */
47  void *_e_userdata; /**< @brief A pointer that will be passed to the callback, usually a pointer to an interpreter. */
48 };
49 
50 /**
51  * @brief Allocates a new context.
52  */
53 RT_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? */
54 
55 /**
56  * @brief Free a context created by db_search_context_create.
57  */
58 RT_EXPORT extern void db_search_context_destroy(struct db_search_context *ctx);
59 
60 /**
61  * @brief Register a callback for -exec filters.
62  */
63 RT_EXPORT extern void db_search_register_exec(struct db_search_context *, db_search_callback_t);
64 
65 /**
66  * @brief Register a userdata for the callback.
67  */
68 RT_EXPORT extern void db_search_register_data(struct db_search_context *, void *);
69 
70 /**
71  * @brief Search for objects in a geometry database using filters
72  *
73  * The db_search function is a programmatic find-style interface that
74  * lets you search for objects in a geometry database. This function
75  * searches the database using a supplied list of filter criteria.
76  *
77  * The function returns a count of objects matching the filter
78  * criteria and can provide the resulting matches in binary format as
79  * either db_full_path or directory objects depending on the flags
80  * (i.e., depending on whether this is a flat or hierarchical search).
81  *
82  * There are a LOT of filter possibilities. See the search(n) manual
83  * page for details.
84  *
85  * @param[out] results is a bu_ptbl holding either db_full_path or
86  * directory pointers.
87  *
88  * @param flags is a bit field for setting various search options.
89  *
90  * @param filter is a string defining search filters to be used.
91  *
92  * @param path_c is the count of directory paths to be searched.
93  *
94  * @param path_v is one or more directory paths to be searched. If
95  * path_v itself is NULL, all top-level objects are searched
96  *
97  * @param dbip The database instance pointer corresponding to the
98  * current geometry database.
99  *
100  * @param ctx Context for -exec. Can be NULL if there are no -exec filters present.
101  *
102  *
103  * @return Negative return values indicate a problem with the search,
104  * and non-negative values indicate a successful search. Non-negative
105  * values correspond with the number of objects found.
106  *
107  * @retval -2 Return code when db_search is called with a NULL dbip.
108  * @retval -1 Return code when the plan search string is invalid.
109  * @retval 0 Return code when the search completed successfully but no matches were found.
110  * @retval >0 Return code when the search completed successfully and matched one or more objects.
111  *
112  * The following example assumes a database instance pointer (dbip) is
113  * available and ready to use.
114  *
115  * @code
116  * size_t i = 0;
117  * struct bu_ptbl results = BU_PTBL_INIT_ZERO;
118  * const char *plan = "-name *.s -or -below -type region";
119  * int matches = db_search(&results, DB_SEARCH_HIDDEN | DB_SEARCH_QUIET , plan, 0, NULL, dbip, ctx);
120  * for (i = 0; matches > 0 && i < BU_PTBL_LEN(&results); i++) {
121  * char *path_str = db_path_to_string((struct db_full_path *)BU_PTBL_GET(&results, i));
122  * bu_log("%s\n", path_str);
123  * bu_free(path_str, "free db_fullpath_to_string allocation");
124  * }
125  * db_search_free(&results);
126  * @endcode
127  *
128  * Note:
129  * Be aware that if you are using db_search to filter pre-built lists of paths,
130  * you need to check that your generated path list is NOT empty before calling
131  * db_search. If you accidentally send an empty path list into db_search,
132  * it will assume you wanted a tops list, which has a good chance of returning
133  * unwanted results.
134  *
135  */
136 RT_EXPORT extern int db_search(struct bu_ptbl *results,
137  int flags,
138  const char *filter,
139  int path_c,
140  struct directory **path_v,
141  struct db_i *dbip,
142  struct db_search_context *ctx
143  );
144 
145 /* These are the possible search flags. */
146 #define DB_SEARCH_TREE 0x0 /**< @brief Do a hierarchy-aware search. This is the default. */
147 #define DB_SEARCH_FLAT 0x1 /**< @brief Do a flat search without hierarchy */
148 #define DB_SEARCH_HIDDEN 0x2 /**< @brief Search using hidden objects */
149 #define DB_SEARCH_RETURN_UNIQ_DP 0x4 /**< @brief Return the set of unique directory pointers instead of full paths */
150 #define DB_SEARCH_QUIET 0x8 /**< @brief Silence all warnings */
151 #define DB_SEARCH_PRINT_TOTAL 0x10 /**< @brief Print total number of items found in search */
152 
153 /**
154  * Properly free the table contents returned by db_search. The bu_ptbl
155  * itself, if not put on the stack, will need to be freed by the same
156  * calling function that allocated it.
157  */
158 RT_EXPORT extern void db_search_free(struct bu_ptbl *search_results);
159 
160 
161 /* db_ls.c */
162 /**
163  * db_ls takes a database instance pointer and assembles a directory
164  * pointer array of objects in the database according to a set of
165  * flags. An optional pattern can be supplied for match filtering via
166  * globbing rules (see bu_path_match()). If pattern is NULL,
167  * filtering is performed using only the flags.
168  *
169  * The caller is responsible for freeing the array.
170  *
171  * Returns -
172  * integer count of objects in dpv
173  * struct directory ** array of objects in dpv via argument
174  *
175  */
176 RT_EXPORT extern size_t db_ls(const struct db_i *dbip,
177  int flags,
178  const char *pattern,
179  struct directory ***dpv);
180 
181 /* These are the possible listing flags. */
182 #define DB_LS_PRIM 0x1 /**< @brief filter for primitives (solids)*/
183 #define DB_LS_COMB 0x2 /**< @brief filter for combinations */
184 #define DB_LS_REGION 0x4 /**< @brief filter for regions */
185 #define DB_LS_HIDDEN 0x8 /**< @brief include hidden objects in results */
186 #define DB_LS_NON_GEOM 0x10 /**< @brief filter for non-geometry objects */
187 #define DB_LS_TOPS 0x20 /**< @brief filter for objects un-referenced by other objects */
188 #define DB_LS_CYCLIC 0x40 /**< @brief filter for objects with a cyclic reference in subtrees */
189 /* TODO - implement this flag
190  #define DB_LS_REGEX 0x80*/ /* interpret pattern using regex rules, instead of
191  globbing rules (default) */
192 
193 /* cyclic.c */
194 /**
195  * db_cyclic_paths searches for cyclic paths in the database, either in all
196  * objects or checking whether a specific dp is cyclic within its subtree.
197  *
198  * If sdp is NULL, ALL directory pointers in the database are checked. This is
199  * a complete validation of the whole .g file, and the only way to
200  * comprehensively search for any cyclic paths present. The return count will
201  * be the number of combs with a cyclic reference in their subtrees.
202  *
203  * If sdp is non-NULL, the search will be limited to checking only the tree
204  * below sdp for a cyclic reference to sdp.
205  *
206  * If a cyclic_paths is non-NULL it will be used to return db_fullpath entries
207  * for the cyclic paths found.
208  */
209 RT_EXPORT extern int db_cyclic_paths(struct bu_ptbl *cyclic_paths, const struct db_i *dbip, struct directory *sdp);
210 
211 
212 __END_DECLS
213 
214 #endif /* RT_SEARCH_H*/
215 /** @} */
216 /*
217  * Local Variables:
218  * mode: C
219  * tab-width: 8
220  * indent-tabs-mode: t
221  * c-file-style: "stroustrup"
222  * End:
223  * ex: shiftwidth=4 tabstop=8
224  */
Header file for the BRL-CAD common definitions.
int(* db_search_callback_t)(int, const char *[], void *)
Definition: search.h:40
int db_search(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)
Search for objects in a geometry database using filters.
struct db_search_context * db_search_context_create(void)
Allocates a new context.
void db_search_register_data(struct db_search_context *, void *)
Register a userdata for the callback.
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)
Register a callback for -exec filters.
void db_search_context_destroy(struct db_search_context *ctx)
Free a context created by db_search_context_create.
int db_cyclic_paths(struct bu_ptbl *cyclic_paths, const struct db_i *dbip, struct directory *sdp)
Definition: ptbl.h:53
Execution context for the -exec filter.
Definition: search.h:45
db_search_callback_t _e_callback
A function that evaluates an array of strings and returns a boolean.
Definition: search.h:46
void * _e_userdata
A pointer that will be passed to the callback, usually a pointer to an interpreter.
Definition: search.h:47