My Kernel v0.1.0
path.h
1
27#ifndef LIB_LIBPATH_H
28#define LIB_LIBPATH_H
29
30#include <kernel/types.h>
31
32#include <utils/compiler.h>
33
34#include <stdbool.h>
35#include <stddef.h>
36
38#define LIBPATH_SEPARATOR '/'
39
43typedef struct libpath_path {
44 const char *path;
45 size_t len;
46} path_t;
47
49#define NEW_PATH(_path, _len) \
50 (path_t) \
51 { \
52 .path = (_path), .len = (_len) \
53 }
54
58#define NEW_DYNAMIC_PATH(_path) NEW_PATH((_path), strlen((_path)))
59
61static inline bool path_is_absolute(const path_t *path)
62{
63 return path->len > 0 && path->path[0] == LIBPATH_SEPARATOR;
64}
65
67static inline bool path_is_empty(const path_t *path)
68{
69 return path->len == 0 || (path_is_absolute(path) && path->len == 1);
70}
71
84ssize_t path_load_parent(char *parent, const path_t *path, size_t size);
85
107typedef struct libpath_segment {
108 const char *start;
109 const char *end;
110 const path_t *path;
111
114 const char *next;
115
118 const char *prev;
120
127#define DO_FOREACH_SEGMENT(_segment, _path, _body) \
128 do { \
129 path_segment_t _segment; \
130 if (path_walk_first((_path), &(_segment))) { \
131 do { \
132 _body; \
133 } while (path_walk_next(&(_segment))); \
134 } \
135 } while (0)
136
138static ALWAYS_INLINE bool path_segment_is_first(const path_segment_t *segment)
139{
140 return segment->prev == NULL;
141}
142
144static ALWAYS_INLINE bool path_segment_is_last(const path_segment_t *segment)
145{
146 return segment->next == NULL;
147}
148
150static ALWAYS_INLINE size_t path_segment_length(const path_segment_t *segment)
151{
152 if (segment->end == NULL) {
153 // If this is the last segment of a path not terminated by a separator
154 // We need to compute the length based on the original full path length
155 return (segment->path->len - (segment->start - segment->path->path));
156 }
157
158 return segment->end - segment->start;
159}
160
170bool path_walk_first(const path_t *, path_segment_t *);
171
181bool path_walk_last(const path_t *, path_segment_t *);
182
193bool path_walk_next(path_segment_t *segment);
194
204bool path_walk_prev(path_segment_t *segment);
205
207bool path_segment_is(const char *, const path_segment_t *);
208
213#endif /* LIB_LIBPATH_H */
bool path_segment_is(const char *, const path_segment_t *)
Definition: path.c:170
static ALWAYS_INLINE bool path_segment_is_first(const path_segment_t *segment)
Check whether a segment is the first of ots containing path.
Definition: path.h:138
static ALWAYS_INLINE bool path_segment_is_last(const path_segment_t *segment)
Check whether a segment is the first of ots containing path.
Definition: path.h:144
bool path_walk_last(const path_t *, path_segment_t *)
Retrieve the last segment of a path.
Definition: path.c:109
static ALWAYS_INLINE size_t path_segment_length(const path_segment_t *segment)
Retieve the length of a segment's content.
Definition: path.h:150
bool path_walk_next(path_segment_t *segment)
Retrieve the next segment.
Definition: path.c:138
bool path_walk_first(const path_t *, path_segment_t *)
Retrieve the first segment of a path.
Definition: path.c:98
bool path_walk_prev(path_segment_t *segment)
Retrieve the previous segment.
Definition: path.c:153
ssize_t path_load_parent(char *parent, const path_t *path, size_t size)
Store the raw path of path's parent inside a string.
Definition: path.c:176
static bool path_is_absolute(const path_t *path)
Check whether a path is an absolute one.
Definition: path.h:61
static bool path_is_empty(const path_t *path)
Check whether a path is empty.
Definition: path.h:67
#define LIBPATH_SEPARATOR
Path separator.
Definition: path.h:38
Safer wrapper around a string symbolizing a path.
Definition: path.h:43
size_t len
The length of the raw string.
Definition: path.h:45
const char * path
Original raw string containing the full path.
Definition: path.h:44
A single path component.
Definition: path.h:107
const char * next
Start of the next segment.
Definition: path.h:114
const char * start
Start of the segment.
Definition: path.h:108
const path_t * path
The original path the segment is a part of.
Definition: path.h:110
const char * prev
Start of the previous segment.
Definition: path.h:118
const char * end
End of the segment.
Definition: path.h:109