summaryrefslogtreecommitdiff
path: root/firmware/export/pathfuncs.h
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/export/pathfuncs.h')
-rw-r--r--firmware/export/pathfuncs.h100
1 files changed, 100 insertions, 0 deletions
diff --git a/firmware/export/pathfuncs.h b/firmware/export/pathfuncs.h
new file mode 100644
index 0000000000..26eb4a1067
--- /dev/null
+++ b/firmware/export/pathfuncs.h
@@ -0,0 +1,100 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2014 by Michael Sevakis
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#ifndef _PATHFUNCS_H_
22#define _PATHFUNCS_H_
23
24#include <sys/types.h>
25#define __need_size_t
26#include <stddef.h>
27#include <stdbool.h>
28#include "config.h"
29
30/* useful char constants that could be reconfigured if desired */
31#define PATH_SEPCH '/'
32#define PATH_SEPSTR "/"
33#define PATH_ROOTSTR "/"
34#define PATH_BADSEPCH '\\'
35#define PATH_DRVSEPCH ':'
36
37/* a nicer way to check for "." and ".." than two strcmp() calls */
38static inline bool is_dotdir_name(const char *name)
39{
40 return name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2]));
41}
42
43static inline bool name_is_dot(const char *name)
44{
45 return name[0] == '.' && !name[1];
46}
47
48static inline bool name_is_dot_dot(const char *name)
49{
50 return name[0] == '.' && name[1] == '.' && !name[2];
51}
52
53/* return a pointer to the character following path separators */
54#define GOBBLE_PATH_SEPCH(p) \
55 ({ int _c; \
56 const char *_p = (p); \
57 while ((_c = *_p) == PATH_SEPCH) \
58 ++_p; \
59 _p; })
60
61/* return a pointer to the character following a path component which may
62 be a separator or the terminating nul */
63#define GOBBLE_PATH_COMP(p) \
64 ({ int _c; \
65 const char *_p = (p); \
66 while ((_c = *_p) && _c != PATH_SEPCH) \
67 ++_p; \
68 _p; })
69
70/* does the character terminate a path component? */
71#define IS_COMP_TERMINATOR(c) \
72 ({ int _c = (c); \
73 !_c || _c == PATH_SEPCH; })
74
75#ifdef HAVE_MULTIVOLUME
76int path_strip_volume(const char *name, const char **nameptr, bool greedy);
77int get_volume_name(int volume, char *name);
78#endif
79
80int path_strip_drive(const char *name, const char **nameptr, bool greedy);
81size_t path_trim_whitespace(const char *name, const char **nameptr);
82size_t path_basename(const char *name, const char **nameptr);
83size_t path_dirname(const char *name, const char **nameptr);
84size_t path_strip_trailing_separators(const char *name, const char **nameptr);
85void path_correct_separators(char *dstpath, const char *path);
86
87/* constants useable in basepath and component */
88#define PA_SEP_HARD NULL /* separate even if base is empty */
89#define PA_SEP_SOFT "" /* separate only if base is nonempty */
90size_t path_append(char *buffer, const char *basepath, const char *component,
91 size_t bufsize);
92ssize_t parse_path_component(const char **pathp, const char **namep);
93
94/* return true if path begins with a root '/' component and is not NULL */
95static inline bool path_is_absolute(const char *path)
96{
97 return path && path[0] == PATH_SEPCH;
98}
99
100#endif /* _PATHFUNCS_H_ */