BRL-CAD
path.h
Go to the documentation of this file.
1/* P A T H . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-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_PATH_H
22#define BU_PATH_H
23
24#include "common.h"
25
26#include <stddef.h> /* for size_t */
27#include <stdlib.h> /* for getenv */
28#include <limits.h> /* for NAME_MAX */
29
30#include "bu/defines.h"
31#include "bu/magic.h"
32#include "bu/vls.h"
33
34__BEGIN_DECLS
35
36/** @addtogroup bu_path
37 *
38 * @brief Routines for manipulating path strings.
39 *
40 * operating system and geometry
41 * database path strings.
42 *
43 * Typically being used for operating system and geometry database
44 * path strings, routines in this header do NOT have knowledge of or
45 * access the filesystem. They will not check for the presence or
46 * absence of objects on disk or in a geometry database - they operate
47 * only on the the path string itself. Any validation of the path is
48 * the responsibility of the caller or other routines.
49 */
50/** @{ */
51/** @file bu/path.h */
52
53/**
54 * Given a string containing a hierarchical path, return a dynamic
55 * string to the parent path.
56 *
57 * This function is similar if not identical to most dirname() BSD
58 * system function implementations; but that system function cannot be
59 * used due to significantly inconsistent behavior across platforms.
60 *
61 * This function always recognizes paths separated by a '/' (i.e.,
62 * geometry paths) as well as whatever the native platform directory
63 * separator may be. It is assumed that all file and directory names
64 * in the path will not contain a path separator, even if escaped.
65 *
66 * It is the caller's responsibility to bu_free() the pointer returned
67 * from this routine.
68 *
69 * Examples of strings returned:
70 *
71 * Input String | Output String
72 * ------------- | -------------
73 * /usr/dir/file | /usr/dir
74 * /usr/dir/ | /usr
75 * /usr/file | /usr
76 * /usr/ | /
77 * /usr | /
78 * / | /
79 * . | .
80 * .. | .
81 * usr | .
82 * a/b | a
83 * a/ | .
84 * ../a/b | ../a
85 *
86 * This routine will return "." if other valid results are not available
87 * but should never return NULL.
88 */
89BU_EXPORT extern char *bu_path_dirname(const char *path);
90
91
92/**
93 * A libbu re-implementation of basename() for cross-platform
94 * compatibility.
95 *
96 * Given a string containing a hierarchically delimited path (e.g.,
97 * /path/to/file), determine the file name portion after the last path
98 * separator.
99 *
100 * This function is similar to most basename() BSD system function
101 * implementations; but that system function cannot be used due to
102 * significantly inconsistent behavior across platforms, particularly
103 * with respect to memory allocation and threading behavior.
104 *
105 * This function always recognizes paths separated by a '/' (i.e.,
106 * geometry paths) as well as whatever the native platform directory
107 * separator may be. It is assumed that all file and directory names
108 * in the path do not contain a path separator. That is, there is
109 * currently no support for escaped characters.
110 *
111 * It is the caller's responsibility to provide a basename buffer with
112 * enough memory for a string with length at least strlen(path) + 1
113 * (for terminating nul) characters as that is the maximum possible
114 * basename size. If the basename output argument is NULL, then
115 * dynamic memory will be returned and the caller is responsible for
116 * releasing memory (via bu_free()), otherwise the caller-provided
117 * basename pointer is returned and managed by the caller accordingly.
118 *
119 * Examples of strings returned:
120 *
121 * Input String | Output String
122 * ------------- | -------------
123 * /usr/dir/file | file
124 * /usr/dir/ | dir
125 * /usr/ | usr
126 * /usr | usr
127 * / | /
128 * . | .
129 * .. | ..
130 * usr | usr
131 * a/b | b
132 * a/ | a
133 * /// | /
134 *
135 */
136BU_EXPORT extern char *bu_path_basename(const char *path, char *basename);
137
138
139/**
140 * Normalize a path according to rules used for realpath, but
141 * without filesystem (or database object) validation.
142 *
143 * @return
144 * A STATIC buffer is returned. It is the caller's responsibility to
145 * call bu_strdup() or make other provisions to save the returned
146 * string, before calling again.
147 */
148BU_EXPORT extern const char *bu_path_normalize(const char *path);
149
150
151/**
152 * Components of a path recognized by bu_path_component(), identified
153 * below in the context of the example path:
154 *
155 * /dir1/dir2/file.ext
156 */
157typedef enum {
158 BU_PATH_DIRNAME, /*!< /dir1/dir2 */
159 BU_PATH_BASENAME, /*!< file.ext */
161 BU_PATH_EXT, /*!< ext */
162 BU_PATH_EXTLESS, /*!< /dir1/dir2/file */
163 BU_PATH_UNKNOWN /*!< marks end of bu_path_component_t enums */
165
166/**
167 * Identify components of a file path such as the filename (basename),
168 * file extension, directory path (dirname) and more.
169 *
170 * Returns truthfully (non-zero) if the specified component type was
171 * found in the provided path. A copy of the component will be
172 * written to the 'component' vls if non-NULL.
173 */
174
175BU_EXPORT extern int bu_path_component(struct bu_vls *component, const char *path, bu_path_component_t type);
176
177
178/**
179 * Generate an argv array from a path
180 *
181 * Given a path string, separate the path elements into a dynamically
182 * allocated argv array with the path separators removed. It is the
183 * caller's responsibility to free the array that is returned as well
184 * as all elements in the array using bu_argv_free() or manually
185 * calling bu_free().
186 */
187BU_EXPORT extern char **bu_path_to_argv(const char *path, int *ac);
188
189#define BU_PATH_MATCH_NOMATCH 1
190
191#define BU_PATH_MATCH_NOESCAPE 0x01 /**< bu_path_match() flag:
192 * Backslash is ordinary
193 * character, not an escape
194 * character.
195 */
196#define BU_PATH_MATCH_PATHNAME 0x02 /**< bu_path_match() flag:
197 * Match a slash in pathname
198 * only with a slash in pattern
199 * and not by an asterisk or a
200 * question mark metacharacter,
201 * nor by a bracket expression
202 * containing a slash.
203 */
204#define BU_PATH_MATCH_PERIOD 0x04 /**< bu_path_match() flag:
205 * Leading period in pathname
206 * has to be matched exactly by
207 * a period in pattern.
208 */
209
210#define BU_PATH_MATCH_LEADING_DIR 0x08 /**< bu_path_match() flag:
211 * Ignore /[tail] after Imatch.
212 */
213
214#define BU_PATH_MATCH_CASEFOLD 0x10 /**< bu_path_match() flag:
215 * Case-insensitive
216 * matching.
217 */
218/**
219 * Compares a string filename or pathname to a pattern.
220 *
221 * This is a portable implementation of the fnmatch() function as
222 * specified in POSIX 1003.2-1992, section B.6.
223 *
224 * Returns 0 if a match is found or 1 otherwise.
225 *
226 */
227BU_EXPORT extern int bu_path_match(const char *pattern, const char *pathname, int flags);
228
229
230/** @} */
231
232__END_DECLS
233
234#endif /* BU_PATH_H */
235
236/*
237 * Local Variables:
238 * mode: C
239 * tab-width: 8
240 * indent-tabs-mode: t
241 * c-file-style: "stroustrup"
242 * End:
243 * ex: shiftwidth=4 tabstop=8
244 */
Header file for the BRL-CAD common definitions.
bu_path_component_t
Definition: path.h:157
const char * bu_path_normalize(const char *path)
char * bu_path_basename(const char *path, char *basename)
char ** bu_path_to_argv(const char *path, int *ac)
int bu_path_component(struct bu_vls *component, const char *path, bu_path_component_t type)
char * bu_path_dirname(const char *path)
int bu_path_match(const char *pattern, const char *pathname, int flags)
@ BU_PATH_BASENAME
Definition: path.h:159
@ BU_PATH_EXTLESS
Definition: path.h:162
@ BU_PATH_DIRNAME
Definition: path.h:158
@ BU_PATH_EXT
Definition: path.h:161
@ BU_PATH_BASENAME_EXTLESS
Definition: path.h:160
@ BU_PATH_UNKNOWN
Definition: path.h:163
Global registry of recognized magic numbers.
Definition: vls.h:53