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/filesystem.h | 6 ------ filesystem/include/path.h | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 filesystem/include/path.h (limited to 'filesystem/include') diff --git a/filesystem/include/filesystem.h b/filesystem/include/filesystem.h index 3dce87f..1c354b7 100644 --- a/filesystem/include/filesystem.h +++ b/filesystem/include/filesystem.h @@ -3,7 +3,6 @@ */ #pragma once -#include #include #include @@ -12,8 +11,3 @@ size_t get_file_size(FILE* file); /// Read the entire contents of the file into memory. void* read_file(const char* filepath); - -/// Make a path relative to the parent directory of a file. -bool make_relative_path( - const char* filepath, const char* path, char* relative, - size_t relative_length); 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