BRL-CAD
Loading...
Searching...
No Matches
glob.h
Go to the documentation of this file.
1/* G L O B . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2015-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
21#ifndef BU_GLOB_H
22#define BU_GLOB_H
23
24#include "common.h"
25
26#include "bu/defines.h"
27#include "bu/vls.h"
28
30
31
32/** @addtogroup bu_glob
33 *
34 * @brief Routines and structures for getting a list of entities that
35 * match a given pattern.
36 *
37 * The bu_glob API is a portable, callback-based glob implementation
38 * modelled on POSIX glob(3) and inspired by OpenBSD's glob.c. By
39 * supplying custom opendir/readdir/closedir/lstat/stat callbacks any
40 * hierarchical namespace (filesystem, BRL-CAD .g database, etc.) can
41 * be searched. The default callbacks use the system filesystem.
42 *
43 * See also: src/librt/db_glob.c for the geometry-database backend
44 * (db_path_glob) which plugs into this API.
45 *
46 */
47/** @{ */
48/** @file bu/glob.h */
49
50
51/**
52 * Representation of a single directory entry returned by gl_readdir.
53 * The caller allocates bu_dirent and sets @a name to point at a caller-owned
54 * bu_vls; gl_readdir fills that vls with the entry name on each call.
55 */
56struct bu_dirent {
57 struct bu_vls *name; /**< entry name (caller-supplied vls, filled by gl_readdir) */
58 void *data; /**< backend-specific per-entry data */
59};
60
61/**
62 * Stat information about a path element, filled by gl_lstat / gl_stat.
63 */
64struct bu_stat {
65 b_off_t size; /**< object size (meaning is backend-defined) */
66 int is_dir; /**< non-zero if path is a container (directory / combination) */
67 void *data; /**< backend-specific data */
68};
69
70
71/**
72 * Main context structure used by bu_glob() to specify behavior,
73 * supply custom callbacks, and accumulate results.
74 *
75 * Initialise with bu_glob_ctx_create() and release with bu_glob_ctx_destroy().
76 */
77/** Opaque implementation type; defined in glob.c. API consumers must not
78 * access this structure directly.
79 */
80struct bu_glob_ctx_impl;
82
83#define BU_GLOB_APPEND 0x0001 /**< Append to output from previous call. */
84#define BU_GLOB_NOSORT 0x0020 /**< Do not sort results. */
85#define BU_GLOB_NOESCAPE 0x2000 /**< Treat backslash as ordinary character. */
86 int gl_flags; /**< flags customising globbing behaviour */
87
88 /* --- Return values (filled by bu_glob) --- */
89
90 int gl_pathc; /**< total number of matched paths */
91 int gl_matchc; /**< number of paths matched by this call */
92 struct bu_vls **gl_pathv; /**< NULL-terminated array of matched paths */
93
94 /* --- Optional callback functions ---
95 *
96 * When NULL the default POSIX filesystem implementation is used.
97 * The @a data member is passed as the last argument to lstat, stat,
98 * errfunc, and as the second argument to opendir, allowing backends
99 * to carry private state without globals.
100 */
101
102 /**
103 * Open the directory at @a path using backend-specific data @a data.
104 * Returns an opaque handle passed back to gl_readdir / gl_closedir,
105 * or NULL on failure.
106 */
107 void *(*gl_opendir)(const char *path, void *data);
108
109 /**
110 * Read the next entry from a directory handle obtained via gl_opendir.
111 * Fills de->name with the entry name. Returns 0 on success, non-zero
112 * when there are no more entries.
113 */
114 int (*gl_readdir)(struct bu_dirent *de, void *dirhandle);
115
116 /**
117 * Close and free a directory handle obtained via gl_opendir.
118 */
120
121 /**
122 * Stat a path (do not follow symlinks, i.e. lstat semantics).
123 * Returns 0 on success, -1 on failure.
124 */
125 int (*gl_lstat)(const char *path, struct bu_stat *sb, void *data);
126
127 /**
128 * Stat a path (follow symlinks, i.e. stat semantics).
129 * Returns 0 on success, -1 on failure.
130 */
131 int (*gl_stat)(const char *path, struct bu_stat *sb, void *data);
132
133#define BU_GLOB_NOMATCH (-1) /**< Returned by bu_glob when there are no matches. */
134#define BU_GLOB_ABORTED (-2) /**< Returned by bu_glob after an unignored error. */
135
136 /**
137 * Called on directory-open errors. Receives the path, errno value,
138 * and the context data pointer. Return non-zero to abort the glob.
139 */
140 int (*gl_errfunc)(const char *path, int errnum, void *data);
141
142 /* --- For caller use --- */
143
144 void *data; /**< Passed verbatim to every callback as described above. */
145
146 /* --- Private --- */
147
148 struct bu_glob_ctx_impl *i; /**< Implementation details; do not use directly. */
149};
151
152
153/**
154 * Create a globbing context
155 */
157
158
159/**
160 * release any resources allocated during bu_glob(), including any
161 * returned paths
162 */
164
165
166/**
167 * Match a pattern against a set of elements.
168 *
169 * This interface is a somewhat simplified and abstracted version of
170 * UNIX glob matching, based loosely on the interface specified in
171 * POSIX.2. It supports user specified callback functions allowing
172 * callers to glob nearly any named storage structure. By default,
173 * globbing will map to the local filesystem.
174 *
175 * Function takes an input pattern, a set of flags, and a globbing
176 * context from bu_glob_ctx_create().
177 *
178 * Returns zero on success, non-zero on failure.
179 *
180 * gl_pathc will contain the total number of paths matched. This will
181 * increment previous glob counts if GLOB_APPEND is specified.
182 *
183 * gl_matchc will contain the number of matched paths for this
184 * invocation of bu_glob().
185 *
186 * gl_pathv contains a list of matched paths.
187 */
188BU_EXPORT extern int bu_glob(const char *pattern, int flags, struct bu_glob_context *context);
189
190/** @} */
191
192
194
195#endif /* BU_GLOB_H */
196
197/*
198 * Local Variables:
199 * mode: C
200 * tab-width: 8
201 * indent-tabs-mode: t
202 * c-file-style: "stroustrup"
203 * End:
204 * ex: shiftwidth=4 tabstop=8
205 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
struct bu_glob_context * bu_glob_ctx_create(void)
void bu_glob_ctx_destroy(struct bu_glob_context *)
int bu_glob(const char *pattern, int flags, struct bu_glob_context *context)
#define b_off_t
Definition common.h:255
void * data
Definition glob.h:58
struct bu_vls * name
Definition glob.h:57
int(* gl_lstat)(const char *path, struct bu_stat *sb, void *data)
Definition glob.h:125
void(* gl_closedir)(void *dirhandle)
Definition glob.h:119
int(* gl_errfunc)(const char *path, int errnum, void *data)
Definition glob.h:140
int(* gl_readdir)(struct bu_dirent *de, void *dirhandle)
Definition glob.h:114
int(* gl_stat)(const char *path, struct bu_stat *sb, void *data)
Definition glob.h:131
void * data
Definition glob.h:144
struct bu_vls ** gl_pathv
Definition glob.h:92
int gl_flags
Definition glob.h:86
int gl_pathc
Definition glob.h:90
int gl_matchc
Definition glob.h:91
struct bu_glob_ctx_impl * i
Definition glob.h:148
Definition glob.h:64
void * data
Definition glob.h:67
b_off_t size
Definition glob.h:65
int is_dir
Definition glob.h:66
Definition vls.h:53