fx.io: expand path joining functionality to non-path strings

This commit is contained in:
2026-06-20 15:25:16 +01:00
parent 32d4d0abf2
commit ee11ff240f
10 changed files with 239 additions and 151 deletions
+35 -29
View File
@@ -53,12 +53,12 @@ static const fx_path *directory_get_rel_path(const struct fx_directory_p *dir)
static const char *directory_get_path_cstr(const struct fx_directory_p *dir)
{
return fx_path_ptr(dir->d_path_abs);
return fx_path_get_cstr(dir->d_path_abs);
}
static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
{
return fx_path_ptr(dir->d_path_rel);
return fx_path_get_cstr(dir->d_path_rel);
}
static fx_result directory_delete(
@@ -90,12 +90,13 @@ static bool directory_path_exists(
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&FX_VALUE_OBJECT(path),
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return false;
}
@@ -110,12 +111,13 @@ static bool directory_path_is_file(
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&FX_VALUE_OBJECT(path),
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return false;
}
@@ -130,12 +132,13 @@ static bool directory_path_is_directory(
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&FX_VALUE_OBJECT(path),
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return false;
}
@@ -151,12 +154,13 @@ static fx_result directory_path_stat(
const fx_path *path,
struct fx_file_info *out)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&FX_VALUE_OBJECT(path),
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY);
}
@@ -171,12 +175,13 @@ static fx_result directory_path_unlink(
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&FX_VALUE_OBJECT(path),
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY);
}
@@ -264,7 +269,7 @@ static fx_result directory_open(
int root_fd = root ? root->d_fd : AT_FDCWD;
const char *path_cstr = fx_path_ptr(path);
const char *path_cstr = fx_path_get_cstr(path);
if (root) {
while (*path_cstr == '/') {
path_cstr++;
@@ -298,15 +303,16 @@ static fx_result directory_open(
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
if (!root) {
cwd = fx_path_create_cwd();
cwd = fx_path_get_system(FX_PATH_CWD);
}
const fx_path *parts[] = {
root ? root->d_path_abs : cwd,
path,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : cwd),
&FX_VALUE_OBJECT(path),
};
fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *new_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!new_path) {
return FX_RESULT_ERR(NO_MEMORY);
}
@@ -570,7 +576,7 @@ fx_iterator *fx_directory_begin(
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
const char *path_list[] = {
fx_path_ptr(it->_p->d_path_abs),
fx_path_get_cstr(it->_p->d_path_abs),
NULL,
};
+25 -18
View File
@@ -86,13 +86,13 @@ static fx_result file_open_shadow(
= fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_string_unref(filename);
const fx_path *parts[] = {
dir,
shadow_filename,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(dir),
&FX_VALUE_OBJECT(shadow_filename),
};
fx_path *shadow_filepath
= fx_path_join(parts, sizeof parts / sizeof parts[0]);
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
fx_path_unref(dir);
fx_path_unref(shadow_filename);
@@ -229,12 +229,14 @@ static enum fx_status file_swap_shadow(
z__fx_io_generate_tmp_filename(tmp_name, sizeof tmp_name);
fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
const fx_path *parts[] = {
dir_path,
tmp_name_p,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(dir_path),
&FX_VALUE_OBJECT(tmp_name_p),
};
tmp_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
tmp_path = fx_path_join_array(
parts,
sizeof parts / sizeof parts[0]);
fx_path_unref(tmp_name_p);
@@ -250,11 +252,15 @@ static enum fx_status file_swap_shadow(
int err;
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
err = rename(
fx_path_ptr(shadow_file->path),
fx_path_ptr(main_file->path));
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
fx_path_get_cstr(main_file->path),
fx_path_get_cstr(tmp_path));
err = rename(
fx_path_get_cstr(shadow_file->path),
fx_path_get_cstr(main_file->path));
err = rename(
fx_path_get_cstr(tmp_path),
fx_path_get_cstr(shadow_file->path));
fx_path_unref(tmp_path);
@@ -424,16 +430,17 @@ fx_result fx_file_open(
if (root) {
root_path = fx_directory_get_path(root);
} else {
root_path = fx_path_create_cwd();
root_path = fx_path_get_system(FX_PATH_CWD);
free_root_path = true;
}
const fx_path *parts[] = {
root_path,
file_path,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(root_path),
&FX_VALUE_OBJECT(file_path),
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (free_root_path) {
fx_path_unref((fx_path *)root_path);
@@ -443,7 +450,7 @@ fx_result fx_file_open(
return FX_RESULT_ERR(NO_MEMORY);
}
int fd = open(fx_path_ptr(abs_path), flags, 0644);
int fd = open(fx_path_get_cstr(abs_path), flags, 0644);
if (fd == -1) {
fx_path_unref(abs_path);
return FX_RESULT_STATUS(
+96 -41
View File
@@ -91,22 +91,22 @@ static bool path_is_directory(const struct fx_path_p *path)
return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0;
}
static void append_path(struct fx_path_p *dest, const struct fx_path_p *src)
static void append_path(struct fx_path_p *dest, const fx_string *src)
{
if (path_is_absolute(src)) {
const char *src_cstr = fx_string_get_cstr(src);
if (src_cstr[0] == '/') {
fx_string_clear(dest->p_pathstr);
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
fx_string_append_s(dest->p_pathstr, src);
return;
}
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
&& fx_string_get_last_char(dest->p_pathstr) != '/'
&& fx_string_get_first_char(src->p_pathstr) != '/') {
char s[] = {'/', 0};
fx_string_append_cstr(dest->p_pathstr, s);
&& fx_string_get_first_char(src) != '/') {
fx_string_append_c(dest->p_pathstr, '/');
}
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
fx_string_append_s(dest->p_pathstr, src);
}
static enum fx_status path_unlink(const struct fx_path_p *path)
@@ -199,33 +199,6 @@ fx_path *fx_path_create_root()
return path;
}
fx_path *fx_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;
}
fx_path *path = fx_path_create();
if (!path) {
free(buf);
return NULL;
}
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
fx_string_append_cstr(p->p_pathstr, buf);
free(buf);
return path;
}
fx_path *fx_path_create_from_cstr(const char *cstr)
{
fx_path *path = fx_path_create();
@@ -258,6 +231,49 @@ fx_path *fx_path_create_from_cstr(const char *cstr)
return path;
}
fx_path *fx_path_get_system(fx_system_path path_type)
{
char path_buf[4096];
const char *path_value = NULL, *default_value = NULL;
switch (path_type) {
case FX_PATH_ROOT:
path_value = "/";
break;
case FX_PATH_CWD:
if (getcwd(path_buf, sizeof path_buf) == NULL) {
path_value = path_buf;
}
break;
case FX_PATH_HOME:
path_value = getenv("HOME");
default_value = "~";
break;
case FX_PATH_CONFIG: {
const char *home = getenv("HOME");
path_value = getenv("XDG_CONFIG_HOME");
snprintf(
path_buf,
sizeof path_buf,
"%s/.config",
home ? home : "~");
default_value = path_buf;
break;
}
default:
return NULL;
}
if (path_value) {
return fx_path_create_from_cstr(path_value);
}
if (default_value) {
return fx_path_create_from_cstr(default_value);
}
return NULL;
}
fx_path *fx_path_duplicate(const fx_path *path)
{
fx_path *new_path = fx_path_create();
@@ -277,7 +293,7 @@ fx_path *fx_path_duplicate(const fx_path *path)
return new_path;
}
fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
fx_path *fx_path_join_array(const fx_value *values[], size_t nr_values)
{
fx_path *result = fx_path_create();
if (!result) {
@@ -287,14 +303,53 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
struct fx_path_p *result_p
= fx_object_get_private(result, FX_TYPE_PATH);
for (size_t i = 0; i < nr_paths; i++) {
if (paths[i]) {
struct fx_path_p *path_p
= fx_object_get_private(paths[i], FX_TYPE_PATH);
append_path(result_p, path_p);
fx_stringstream *tmp = fx_stringstream_create();
fx_string *value_s = fx_string_create();
for (size_t i = 0; i < nr_values; i++) {
if (!values[i]) {
continue;
}
fx_stringstream_reset(tmp);
fx_value_to_string(values[i], tmp, NULL);
fx_string_clear(value_s);
fx_string_append_cstr(value_s, fx_stringstream_get_cstr(tmp));
append_path(result_p, value_s);
}
fx_string_unref(value_s);
fx_stringstream_unref(tmp);
return result;
}
fx_path *fx_path_join_list(size_t count, ...)
{
fx_path *result = fx_path_create();
if (!result) {
return NULL;
}
struct fx_path_p *result_p
= fx_object_get_private(result, FX_TYPE_PATH);
va_list arg;
va_start(arg, count);
fx_stringstream *tmp = fx_stringstream_create();
fx_string *value_s = fx_string_create();
for (size_t i = 0; i < count; i++) {
const fx_value *value = va_arg(arg, const fx_value *);
fx_stringstream_reset(tmp);
fx_value_to_string(value, tmp, NULL);
fx_string_clear(value_s);
fx_string_append_cstr(value_s, fx_stringstream_get_cstr(tmp));
append_path(result_p, value_s);
}
fx_string_unref(value_s);
fx_stringstream_unref(tmp);
return result;
}
@@ -363,7 +418,7 @@ enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
out_name);
}
const char *fx_path_ptr(const fx_path *path)
const char *fx_path_get_cstr(const fx_path *path)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
}