From 5f660a4a4e42233820ce9a804cf70685afb5c454 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 19 Oct 2025 21:00:26 +0100 Subject: [PATCH] io: path: convert to new object system --- io/include/blue/io/path.h | 36 +-- io/sys/darwin/path.c | 501 ++++++++++++++++++++++---------------- 2 files changed, 309 insertions(+), 228 deletions(-) diff --git a/io/include/blue/io/path.h b/io/include/blue/io/path.h index 2d70fb5..aa15651 100644 --- a/io/include/blue/io/path.h +++ b/io/include/blue/io/path.h @@ -1,20 +1,29 @@ -#ifndef BLUELIB_IO_PATH_H_ -#define BLUELIB_IO_PATH_H_ +#ifndef BLUE_IO_PATH_H_ +#define BLUE_IO_PATH_H_ +#include #include #include -#include +#include #include -#define B_PATH(p) ((b_path *)p) +B_DECLS_BEGIN; -#define B_RV_PATH(cstr) B_PATH(B_RV(b_path_create_from_cstr(cstr))) +#define B_TYPE_PATH (b_path_get_type()) + +B_DECLARE_TYPE(b_path); + +B_TYPE_CLASS_DECLARATION_BEGIN(b_path) +B_TYPE_CLASS_DECLARATION_END(b_path) + +#define B_RV_PATH(cstr) B_RV(b_path_create_from_cstr(cstr)) struct b_file_info; -typedef struct b_path b_path; +BLUE_API b_type b_path_get_type(void); + +B_TYPE_DEFAULT_CONSTRUCTOR(b_path, B_TYPE_PATH); -BLUE_API b_path *b_path_create(); BLUE_API b_path *b_path_create_root(); BLUE_API b_path *b_path_create_cwd(); BLUE_API b_path *b_path_create_from_cstr(const char *path); @@ -35,20 +44,11 @@ BLUE_API b_status b_path_unlink(const b_path *path); BLUE_API enum b_status b_path_get_directory( const b_path *path, b_path **out_dir_path); -BLUE_API enum b_status b_path_get_filename( - const b_path *path, struct b_string *out_name); +BLUE_API enum b_status b_path_get_filename(const b_path *path, b_string *out_name); BLUE_API const char *b_path_ptr(const b_path *path); BLUE_API size_t b_path_length(const b_path *path); -static inline b_path *b_path_retain(b_path *path) -{ - return B_PATH(b_retain(B_DSREF(path))); -} - -static inline void b_path_release(b_path *path) -{ - b_release(B_DSREF(path)); -} +B_DECLS_END; #endif diff --git a/io/sys/darwin/path.c b/io/sys/darwin/path.c index d10a47d..2046c95 100644 --- a/io/sys/darwin/path.c +++ b/io/sys/darwin/path.c @@ -12,218 +12,44 @@ #include #include -struct b_path { - struct b_dsref base; - struct b_string *pathstr; +/*** PRIVATE DATA *************************************************************/ + +struct b_path_p { + b_string *p_pathstr; }; -static void path_release(struct b_dsref *obj); -static void path_to_string(const struct b_dsref *obj, struct b_stream *out); +/*** PRIVATE FUNCTIONS ********************************************************/ -static struct b_dsref_type path_type = { - .t_name = "corelib::path", - .t_flags = B_DSREF_FUNDAMENTAL, - .t_id = B_DSREF_TYPE_PATH, - .t_instance_size = sizeof(struct b_path), - .t_release = path_release, - .t_to_string = path_to_string, -}; - -struct b_path *b_path_create() -{ - struct b_path *path - = (struct b_path *)b_dsref_type_instantiate(&path_type); - if (!path) { - return NULL; - } - - path->pathstr = b_string_create(); - if (!path->pathstr) { - b_path_release(path); - return NULL; - } - - return path; -} - -struct b_path *b_path_create_root() -{ - const char *system_drive = "/"; - size_t system_drive_len = strlen(system_drive); - - struct b_path *path = b_path_create(); - if (!path) { - return NULL; - } - - b_string_append_cstr(path->pathstr, system_drive); - if (system_drive[system_drive_len - 1] != '\\') { - b_string_append_c(path->pathstr, '\\'); - } - - return path; -} - -struct b_path *b_path_create_cwd() -{ - const long buf_len = 2048; - char *buf = malloc(buf_len); - if (!buf) { - return NULL; - } - - if (!getcwd(buf, buf_len)) { - free(buf); - return NULL; - } - - struct b_path *path = b_path_create(); - if (!path) { - free(buf); - return NULL; - } - - b_string_append_cstr(path->pathstr, buf); - free(buf); - - return path; -} - -struct b_path *b_path_create_from_cstr(const char *cstr) -{ - struct b_path *path = b_path_create(); - if (!path) { - return NULL; - } - - char prev = 0; - - for (size_t i = 0; cstr[i]; i++) { - char c = cstr[i]; - if (c == '\\') { - c = '/'; - } - - if (prev == '/' && c == '/') { - continue; - } - - b_string_append_c(path->pathstr, c); - prev = c; - } - - while (b_string_back(path->pathstr) == '/') { - b_string_pop_back(path->pathstr); - } - - return path; -} - -struct b_path *b_path_duplicate(const struct b_path *path) -{ - struct b_path *new_path = b_path_create(); - if (!path) { - return NULL; - } - - new_path->pathstr = b_string_duplicate(path->pathstr); - if (!new_path->pathstr) { - b_path_release(new_path); - return NULL; - } - - return new_path; -} - -static void append_path(struct b_path *dest, const struct b_path *src) -{ - if (b_path_is_absolute(src)) { - b_string_clear(dest->pathstr); - b_string_append_s(dest->pathstr, src->pathstr); - return; - } - - if (b_string_get_size(dest->pathstr, B_STRLEN_NORMAL) > 0 - && b_string_back(dest->pathstr) != '/' - && b_string_front(src->pathstr) != '/') { - char s[] = {'/', 0}; - b_string_append_cstr(dest->pathstr, s); - } - - b_string_append_s(dest->pathstr, src->pathstr); -} - -struct b_path *b_path_join(const b_path *paths[], size_t nr_paths) -{ - struct b_path *result = b_path_create(); - if (!result) { - return NULL; - } - - for (size_t i = 0; i < nr_paths; i++) { - if (paths[i]) { - append_path(result, paths[i]); - } - } - - return result; -} - -struct b_path *b_path_make_absolute(const struct b_path *in) +static b_path *path_make_absolute(const struct b_path_p *in) { return NULL; } -struct b_path *b_path_make_relative(const struct b_path *in) +static b_path *path_make_relative(const struct b_path_p *in) { return NULL; } -struct b_path *b_path_make_canonical(const struct b_path *in) +static b_path *path_make_canonical(const struct b_path_p *in) { return NULL; } -bool b_path_is_absolute(const struct b_path *path) +static bool path_is_absolute(const struct b_path_p *path) { - const char *s = b_string_ptr(path->pathstr); + const char *s = b_string_ptr(path->p_pathstr); return s[0] == '/'; } -bool b_path_exists(const struct b_path *path) +static const char *path_ptr(const struct b_path_p *path) { - b_file_info info; - if (!B_OK(b_path_stat(path, &info))) { - return false; - } - - return true; + return b_string_ptr(path->p_pathstr); } -bool b_path_is_file(const struct b_path *path) -{ - b_file_info info; - if (!B_OK(b_path_stat(path, &info))) { - return false; - } - - return (info.attrib & B_FILE_ATTRIB_NORMAL) != 0; -} - -bool b_path_is_directory(const struct b_path *path) -{ - b_file_info info; - if (!B_OK(b_path_stat(path, &info))) { - return false; - } - - return (info.attrib & B_FILE_ATTRIB_DIRECTORY) != 0; -} - -enum b_status b_path_stat(const b_path *path, struct b_file_info *out) +static enum b_status path_stat(const struct b_path_p *path, struct b_file_info *out) { struct stat st; - int err = stat(b_path_ptr(path), &st); + int err = stat(path_ptr(path), &st); if (err != 0) { return b_status_from_errno(errno, B_ERR_IO_FAILURE); @@ -234,16 +60,64 @@ enum b_status b_path_stat(const b_path *path, struct b_file_info *out) return b_file_info_from_stat(&st, out); } -enum b_status b_path_unlink(const b_path *path) +static bool path_exists(const struct b_path_p *path) { - int err = remove(b_string_ptr(path->pathstr)); + b_file_info info; + if (!B_OK(path_stat(path, &info))) { + return false; + } + + return true; +} + +static bool path_is_file(const struct b_path_p *path) +{ + b_file_info info; + if (!B_OK(path_stat(path, &info))) { + return false; + } + + return (info.attrib & B_FILE_ATTRIB_NORMAL) != 0; +} + +static bool path_is_directory(const struct b_path_p *path) +{ + b_file_info info; + if (!B_OK(path_stat(path, &info))) { + return false; + } + + return (info.attrib & B_FILE_ATTRIB_DIRECTORY) != 0; +} + +static void append_path(struct b_path_p *dest, const struct b_path_p *src) +{ + if (path_is_absolute(src)) { + b_string_clear(dest->p_pathstr); + b_string_append_s(dest->p_pathstr, src->p_pathstr); + return; + } + + if (b_string_get_size(dest->p_pathstr, B_STRLEN_NORMAL) > 0 + && b_string_back(dest->p_pathstr) != '/' + && b_string_front(src->p_pathstr) != '/') { + char s[] = {'/', 0}; + b_string_append_cstr(dest->p_pathstr, s); + } + + b_string_append_s(dest->p_pathstr, src->p_pathstr); +} + +static enum b_status path_unlink(const struct b_path_p *path) +{ + int err = remove(b_string_ptr(path->p_pathstr)); return err == 0 ? B_SUCCESS : b_status_from_errno(errno, B_ERR_IO_FAILURE); } -enum b_status b_path_get_directory( - const struct b_path *path, struct b_path **out_dir_path) +static enum b_status path_get_directory( + const struct b_path_p *path, b_path **out_dir_path) { - struct b_string *path_str = path->pathstr; + b_string *path_str = path->p_pathstr; long len = b_string_get_size(path_str, B_STRLEN_NORMAL); const char *path_cstr = b_string_ptr(path_str); @@ -255,21 +129,20 @@ enum b_status b_path_get_directory( } size_t dir_path_len = (size_t)(sep - path_cstr); - struct b_string *dir_path_s = b_string_substr(path_str, 0, dir_path_len); + b_string *dir_path_s = b_string_substr(path_str, 0, dir_path_len); - struct b_path *dir_path - = b_path_create_from_cstr(b_string_ptr(dir_path_s)); - b_string_release(dir_path_s); + b_path *dir_path = b_path_create_from_cstr(b_string_ptr(dir_path_s)); + b_string_unref(dir_path_s); *out_dir_path = dir_path; return B_SUCCESS; } -enum b_status b_path_get_filename( - const struct b_path *path, struct b_string *out_name) +static enum b_status path_get_filename( + const struct b_path_p *path, b_string *out_name) { - struct b_string *path_str = path->pathstr; + b_string *path_str = path->p_pathstr; long len = b_string_get_size(path_str, B_STRLEN_NORMAL); char *sep = strrchr(b_string_ptr(path_str), '/'); @@ -294,26 +167,234 @@ enum b_status b_path_get_filename( return B_SUCCESS; } -const char *b_path_ptr(const struct b_path *path) +static size_t path_length(const struct b_path_p *path) { - return b_string_ptr(path->pathstr); + return b_string_get_size(path->p_pathstr, B_STRLEN_NORMAL); } -size_t b_path_length(const struct b_path *path) +/*** PUBLIC FUNCTIONS *********************************************************/ + +b_path *b_path_create_root() { - return b_string_get_size(path->pathstr, B_STRLEN_NORMAL); + const char *system_drive = "/"; + size_t system_drive_len = strlen(system_drive); + + b_path *path = b_path_create(); + if (!path) { + return NULL; + } + + struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH); + + b_string_append_cstr(p->p_pathstr, system_drive); + if (system_drive[system_drive_len - 1] != '\\') { + b_string_append_c(p->p_pathstr, '\\'); + } + + return path; } -void path_release(struct b_dsref *obj) +b_path *b_path_create_cwd() { - struct b_path *path = (struct b_path *)obj; + const long buf_len = 2048; + char *buf = malloc(buf_len); + if (!buf) { + return NULL; + } - b_string_release(path->pathstr); + if (!getcwd(buf, buf_len)) { + free(buf); + return NULL; + } + + b_path *path = b_path_create(); + if (!path) { + free(buf); + return NULL; + } + + struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH); + + b_string_append_cstr(p->p_pathstr, buf); + free(buf); + + return path; } -void path_to_string(const struct b_dsref *obj, struct b_stream *out) +b_path *b_path_create_from_cstr(const char *cstr) { - struct b_path *path = (struct b_path *)obj; + b_path *path = b_path_create(); + if (!path) { + return NULL; + } - b_stream_write_string(out, b_string_ptr(path->pathstr), NULL); + struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH); + + char prev = 0; + + for (size_t i = 0; cstr[i]; i++) { + char c = cstr[i]; + if (c == '\\') { + c = '/'; + } + + if (prev == '/' && c == '/') { + continue; + } + + b_string_append_c(p->p_pathstr, c); + prev = c; + } + + while (b_string_back(p->p_pathstr) == '/') { + b_string_pop_back(p->p_pathstr); + } + + return path; } + +b_path *b_path_duplicate(const b_path *path) +{ + b_path *new_path = b_path_create(); + if (!path) { + return NULL; + } + + struct b_path_p *old_p = b_object_get_private(path, B_TYPE_PATH); + struct b_path_p *new_p = b_object_get_private(new_path, B_TYPE_PATH); + + new_p->p_pathstr = b_string_duplicate(old_p->p_pathstr); + if (!new_p->p_pathstr) { + b_path_unref(new_path); + return NULL; + } + + return new_path; +} + +b_path *b_path_join(const b_path *paths[], size_t nr_paths) +{ + b_path *result = b_path_create(); + if (!result) { + return NULL; + } + + struct b_path_p *result_p = b_object_get_private(result, B_TYPE_PATH); + + for (size_t i = 0; i < nr_paths; i++) { + if (paths[i]) { + struct b_path_p *path_p + = b_object_get_private(paths[i], B_TYPE_PATH); + append_path(result_p, path_p); + } + } + + return result; +} + +b_path *b_path_make_absolute(const b_path *in) +{ + B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_absolute, in); +} + +b_path *b_path_make_relative(const b_path *in) +{ + B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_relative, in); +} + +b_path *b_path_make_canonical(const b_path *in) +{ + B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_canonical, in); +} + +bool b_path_is_absolute(const b_path *path) +{ + B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_absolute, path); +} + +bool b_path_exists(const b_path *path) +{ + B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_exists, path); +} + +bool b_path_is_file(const b_path *path) +{ + B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_file, path); +} + +bool b_path_is_directory(const b_path *path) +{ + B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_directory, path); +} + +enum b_status b_path_stat(const b_path *path, struct b_file_info *out) +{ + B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_stat, path, out); +} + +enum b_status b_path_unlink(const b_path *path) +{ + B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_unlink, path); +} + +enum b_status b_path_get_directory(const b_path *path, b_path **out_dir_path) +{ + B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_get_directory, path, out_dir_path); +} + +enum b_status b_path_get_filename(const b_path *path, b_string *out_name) +{ + B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_get_filename, path, out_name); +} + +const char *b_path_ptr(const b_path *path) +{ + B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_ptr, path); +} + +size_t b_path_length(const b_path *path) +{ + B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_length, path); +} + +/*** VIRTUAL FUNCTIONS ********************************************************/ + +static void path_init(b_object *obj, void *priv) +{ + struct b_path_p *path = priv; + + path->p_pathstr = b_string_create(); + if (!path->p_pathstr) { + /* TODO return error */ + } +} + +void path_fini(b_object *obj, void *priv) +{ + struct b_path_p *path = priv; + + b_string_unref(path->p_pathstr); +} + +void path_to_string(const b_object *obj, struct b_stream *out) +{ + struct b_path_p *path = b_object_get_private(obj, B_TYPE_PATH); + + b_stream_write_string(out, b_string_ptr(path->p_pathstr), NULL); +} + +/*** CLASS DEFINITION *********************************************************/ + +B_TYPE_CLASS_DEFINITION_BEGIN(b_path) + B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) + B_INTERFACE_ENTRY(to_string) = path_to_string; + B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) +B_TYPE_CLASS_DEFINITION_END(b_path) + +B_TYPE_DEFINITION_BEGIN(b_path) + B_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4); + B_TYPE_CLASS(b_path_class); + B_TYPE_INSTANCE_PRIVATE(struct b_path_p); + B_TYPE_INSTANCE_INIT(path_init); + B_TYPE_INSTANCE_FINI(path_fini); +B_TYPE_DEFINITION_END(b_path)