From 8177eb2b24eb59af4a87f4df29a0c606a56d8453 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 13 Feb 2025 21:36:53 +0000 Subject: [PATCH] io: store current directory iterator filepath as a b_path --- io-test/tree.c | 2 +- io/include/blue/io/directory.h | 2 +- io/sys/windows/directory.c | 15 +++++++++++++++ io/sys/windows/path.c | 27 +++++++++++++++++---------- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/io-test/tree.c b/io-test/tree.c index bde3f5d..717faad 100644 --- a/io-test/tree.c +++ b/io-test/tree.c @@ -24,7 +24,7 @@ int main(int argc, const char **argv) b_directory_iterator it = {0}; b_directory_iterator_begin(dir, &it, B_DIRECTORY_ITERATE_PARENT_FIRST); while (b_directory_iterator_is_valid(&it)) { - printf("%s\n", it.filepath); + printf("%s\n", b_path_ptr(it.filepath)); b_directory_iterator_next(&it); } diff --git a/io/include/blue/io/directory.h b/io/include/blue/io/directory.h index 6c40751..979b232 100644 --- a/io/include/blue/io/directory.h +++ b/io/include/blue/io/directory.h @@ -21,8 +21,8 @@ typedef struct b_directory_iterator { b_iterator _base; b_directory_iterator_flags flags; b_directory *root; + const b_path *filepath; char *filename; - char *filepath; struct z__b_directory_iterator *_z; } b_directory_iterator; diff --git a/io/sys/windows/directory.c b/io/sys/windows/directory.c index 2fbb02e..6fbb881 100644 --- a/io/sys/windows/directory.c +++ b/io/sys/windows/directory.c @@ -153,8 +153,23 @@ static void cleanup_iterator(struct b_directory_iterator *it) static void update_iterator_data(struct b_directory_iterator *it) { + if (it->filepath) { + b_path_release(B_PATH(it->filepath)); + it->filepath = NULL; + } + struct iteration_state *state = get_iteration_state(it->_z); it->filename = state->data.cFileName; + + struct b_path *filename = b_path_create_from_cstr(it->filename); + + const struct b_path *parts[] = { + state->search_path, + filename, + }; + + it->filepath + = b_path_join(parts, sizeof parts / sizeof parts[0]); } static bool move_into_directory(struct b_directory_iterator *it, const char *dir_name) diff --git a/io/sys/windows/path.c b/io/sys/windows/path.c index 15a60cc..ee5822a 100644 --- a/io/sys/windows/path.c +++ b/io/sys/windows/path.c @@ -166,14 +166,17 @@ struct b_path *b_path_join( struct b_path *b_path_make_absolute(const struct b_path *in) { + return NULL; } struct b_path *b_path_make_relative(const struct b_path *in) { + return NULL; } struct b_path *b_path_make_canonical(const struct b_path *in) { + return NULL; } bool b_path_is_absolute(const struct b_path *path) @@ -202,17 +205,28 @@ bool b_path_is_absolute(const struct b_path *path) bool b_path_exists(const struct b_path *path) { - + DWORD attrib = GetFileAttributesA(b_path_ptr(path)); + return attrib != INVALID_FILE_ATTRIBUTES; } bool b_path_is_file(const struct b_path *path) { + DWORD attrib = GetFileAttributesA(b_path_ptr(path)); + if (attrib == INVALID_FILE_ATTRIBUTES) { + return false; + } + return (attrib & FILE_ATTRIBUTE_NORMAL) != 0; } bool b_path_is_directory(const struct b_path *path) { + DWORD attrib = GetFileAttributesA(b_path_ptr(path)); + if (attrib == INVALID_FILE_ATTRIBUTES) { + return false; + } + return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; } const char *b_path_ptr(const struct b_path *path) @@ -220,17 +234,10 @@ const char *b_path_ptr(const struct b_path *path) return b_string_ptr(path->pathstr); } -struct b_path *b_path_retain(struct b_path *path) -{ -} - -void b_path_release(struct b_path *path) -{ -} - void path_release(struct b_object* obj) { - b_path_release((struct b_path *)obj); + struct b_path *path = B_PATH(obj); + b_string_release(path->pathstr); } void path_to_string(struct b_object* obj, struct b_stringstream* out)