From 04e3ded4c28c0b559620609daaae7b939d776b61 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 15 Jun 2024 11:42:48 -0700 Subject: Add path. --- filesystem/include/path.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 filesystem/include/path.h (limited to 'filesystem/include/path.h') diff --git a/filesystem/include/path.h b/filesystem/include/path.h new file mode 100644 index 0000000..8ad885d --- /dev/null +++ b/filesystem/include/path.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include + +typedef struct path { + char* data; + size_t size; // Does not include null terminator. 0 if data is null. +} path; + +/// Create a new path. +path path_new(const char*); + +/// Free the path's memory. +void path_del(path*); + +/// Return a C string from the path. +static inline const char* path_cstr(path p) { return p.data; } + +/// Return true if the path is empty, false otherwise. +static inline bool path_empty(path p) { + assert((p.data != 0) || (p.size == 0)); // null data -> 0 size + return p.data != 0; +} + +/// Returns the parent directory, or empty path if the given path has no parent. +/// The returned path is a slice of the input path; this function does not +/// allocate. +path path_parent_dir(path); + +/// Concatenate two paths. +path path_concat(path left, path right); + +/// Make a path relative to the parent directory of a file. +bool path_make_relative( + const char* filepath, const char* path, char* relative, + size_t relative_length); -- cgit v1.2.3