io: store current directory iterator filepath as a b_path

This commit is contained in:
2025-02-13 21:36:53 +00:00
parent c822be0804
commit 8177eb2b24
4 changed files with 34 additions and 12 deletions

View File

@@ -24,7 +24,7 @@ int main(int argc, const char **argv)
b_directory_iterator it = {0}; b_directory_iterator it = {0};
b_directory_iterator_begin(dir, &it, B_DIRECTORY_ITERATE_PARENT_FIRST); b_directory_iterator_begin(dir, &it, B_DIRECTORY_ITERATE_PARENT_FIRST);
while (b_directory_iterator_is_valid(&it)) { 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); b_directory_iterator_next(&it);
} }

View File

@@ -21,8 +21,8 @@ typedef struct b_directory_iterator {
b_iterator _base; b_iterator _base;
b_directory_iterator_flags flags; b_directory_iterator_flags flags;
b_directory *root; b_directory *root;
const b_path *filepath;
char *filename; char *filename;
char *filepath;
struct z__b_directory_iterator *_z; struct z__b_directory_iterator *_z;
} b_directory_iterator; } b_directory_iterator;

View File

@@ -153,8 +153,23 @@ static void cleanup_iterator(struct b_directory_iterator *it)
static void update_iterator_data(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); struct iteration_state *state = get_iteration_state(it->_z);
it->filename = state->data.cFileName; 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) static bool move_into_directory(struct b_directory_iterator *it, const char *dir_name)

View File

@@ -166,14 +166,17 @@ struct b_path *b_path_join(
struct b_path *b_path_make_absolute(const struct b_path *in) 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) 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) struct b_path *b_path_make_canonical(const struct b_path *in)
{ {
return NULL;
} }
bool b_path_is_absolute(const struct b_path *path) 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) 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) 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) 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) 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); 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) 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) void path_to_string(struct b_object* obj, struct b_stringstream* out)