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
+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);
}