BRL-CAD
glob.h
Go to the documentation of this file.
1/* G L O B . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2015-2023 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
29__BEGIN_DECLS
30
31
32/** @addtogroup bu_experimental
33 *
34 * NOTE - the glob API below is a work in progress - until this notice is
35 * removed it should not be considered functional, much less stable!
36 *
37 * @brief Routines and structures for getting a list of entities that
38 * match a given pattern.
39 *
40 */
41/** @{ */
42/** @file bu/glob.h */
43
44
45/**
46 * representation of a path element, for use with bu_glob() callbacks.
47 */
48struct bu_dirent {
49 struct bu_vls *name;
50 void *data;
51};
52
53/**
54 * information about a path element (file, directory, object, etc),
55 * for use with bu_glob() callbacks.
56 */
57struct bu_stat {
58 struct bu_vls name;
60 void *data;
61};
62
63
64/**
65 * main structure used by bu_glob() to specify behavior, callbacks,
66 * and return results.
67 */
69
70#define BU_GLOB_APPEND 0x0001 /**< Append to output from previous call. */
71#define BU_GLOB_NOSORT 0x0020 /**< Don't sort. */
72#define BU_GLOB_NOESCAPE 0x2000 /**< Disable backslash escaping. */
73#define BU_GLOB_ALTDIRFUNC 0x0040 /**< use alternate functions. */
74 int gl_flags; /**< flags customizing globbing behavior */
75
76 /* Return values */
77
78 int gl_pathc; /**< count of total paths so far */
79 int gl_matchc; /**< count of paths matching pattern */
80 struct bu_vls **gl_pathv; /**< list of paths matching pattern */
81
82 /* Callback functions */
83
84 struct bu_glob_context *(*gl_opendir)(const char *);
85 int (*gl_readdir)(struct bu_dirent *, struct bu_glob_context *);
86 void (*gl_closedir)(struct bu_glob_context *);
87
88 int (*gl_lstat)(const char *, struct bu_stat *, struct bu_glob_context *);
89 int (*gl_stat)(const char *, struct bu_stat *, struct bu_glob_context *);
90
91#define BU_GLOB_NOMATCH (-1) /**< No match. */
92#define BU_GLOB_ABORTED (-2) /**< Unignored error. */
93 int (*gl_errfunc)(const char *, int, struct bu_glob_context *);
94
95 /* For caller use */
96
97 void *data; /**< data passed to all callbacks */
98
99 /* Private */
100
101 void *priv; /**< For internal use only */
102};
104
105
106/**
107 * declaration statement initialization of a bu_glob struct
108 */
109#define BU_GLOB_INIT_ZERO {0, 0, 0, NULL, (struct bu_glob_context *(*)(const char *))NULL, (int(*)(struct bu_dirent *, struct bu_glob_context *))NULL, (void(*)(struct bu_glob_context *))NULL, (int(*)(const char *, struct bu_stat *, struct bu_glob_context *))NULL, (int(*)(const char *, struct bu_stat *, struct bu_glob_context *))NULL, (int(*)(const char *, int, struct bu_glob_context *))NULL, NULL, NULL}
110
111
112/**
113 * initialize a globbing context for use prior to calling bu_glob()
114 */
115BU_EXPORT struct bu_glob_context *bu_glob_init(void);
116
117
118/**
119 * release any resources allocated during bu_glob(), including any
120 * returned paths
121 */
122BU_EXPORT extern void bu_glob_free(struct bu_glob_context *);
123
124
125/**
126 * match a pattern against a set of elements.
127 *
128 * This interface is a somewhat simplified and abstracted version of
129 * UNIX glob matching, based loosely on the interface specified in
130 * POSIX.2. It supports user specified callback functions allowing
131 * callers to glob nearly any named storage structure. By default,
132 * globbing will map to the local filesystem.
133 *
134 * Function takes an input pattern, a set of flags, and a globbing
135 * context from bu_glob_alloc().
136 *
137 * Returns zero on success, non-zero on failure.
138 *
139 * gl_pathc will contain the total number of paths matched. This will
140 * increment previous glob counts if GLOB_APPEND is specified.
141 *
142 * gl_matchc will contain the number of matched paths for this
143 * invocation of bu_glob().
144 *
145 * gl_pathv contains a list of matched paths.
146 */
147BU_EXPORT extern int bu_glob(const char *pattern, int flags, struct bu_glob_context *context);
148
149/** @} */
150
151
152__END_DECLS
153
154#endif /* BU_GLOB_H */
155
156/*
157 * Local Variables:
158 * mode: C
159 * tab-width: 8
160 * indent-tabs-mode: t
161 * c-file-style: "stroustrup"
162 * End:
163 * ex: shiftwidth=4 tabstop=8
164 */
Header file for the BRL-CAD common definitions.
struct bu_glob_context * bu_glob_init(void)
void bu_glob_free(struct bu_glob_context *)
int bu_glob(const char *pattern, int flags, struct bu_glob_context *context)
#define b_off_t
Definition: common.h:222
Definition: glob.h:48
void * data
Definition: glob.h:50
struct bu_vls * name
Definition: glob.h:49
int(* gl_readdir)(struct bu_dirent *, struct bu_glob_context *)
Definition: glob.h:85
void * data
Definition: glob.h:97
void * priv
Definition: glob.h:101
int(* gl_lstat)(const char *, struct bu_stat *, struct bu_glob_context *)
Definition: glob.h:88
struct bu_vls ** gl_pathv
Definition: glob.h:80
int(* gl_stat)(const char *, struct bu_stat *, struct bu_glob_context *)
Definition: glob.h:89
int gl_flags
Definition: glob.h:74
int gl_pathc
Definition: glob.h:78
int gl_matchc
Definition: glob.h:79
void(* gl_closedir)(struct bu_glob_context *)
Definition: glob.h:86
int(* gl_errfunc)(const char *, int, struct bu_glob_context *)
Definition: glob.h:93
Definition: glob.h:57
struct bu_vls name
Definition: glob.h:58
void * data
Definition: glob.h:60
b_off_t size
Definition: glob.h:59
Definition: vls.h:53