fx.io: expand path joining functionality to non-path strings
This commit is contained in:
@@ -51,12 +51,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(
|
||||
@@ -263,7 +263,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++;
|
||||
@@ -571,7 +571,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,
|
||||
};
|
||||
|
||||
|
||||
@@ -248,11 +248,11 @@ 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_get_cstr(main_file->path), fx_path_get_cstr(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(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);
|
||||
|
||||
@@ -441,7 +441,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(
|
||||
|
||||
+55
-42
@@ -41,7 +41,7 @@ static bool path_is_absolute(const struct fx_path_p *path)
|
||||
return s[0] == '/';
|
||||
}
|
||||
|
||||
static const char *path_ptr(const struct fx_path_p *path)
|
||||
static const char *path_get_cstr(const struct fx_path_p *path)
|
||||
{
|
||||
return fx_string_get_cstr(path->p_pathstr);
|
||||
}
|
||||
@@ -51,7 +51,7 @@ static enum fx_status path_stat(
|
||||
struct fx_file_info *out)
|
||||
{
|
||||
struct stat st;
|
||||
int err = stat(path_ptr(path), &st);
|
||||
int err = stat(path_get_cstr(path), &st);
|
||||
|
||||
if (err != 0) {
|
||||
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
|
||||
@@ -135,8 +135,8 @@ static enum fx_status path_get_directory(
|
||||
size_t dir_path_len = (size_t)(sep - path_cstr);
|
||||
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
|
||||
|
||||
fx_path *dir_path = fx_path_create_from_cstr(
|
||||
fx_string_get_cstr(dir_path_s));
|
||||
fx_path *dir_path
|
||||
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
|
||||
fx_string_unref(dir_path_s);
|
||||
|
||||
*out_dir_path = dir_path;
|
||||
@@ -200,33 +200,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();
|
||||
@@ -259,6 +232,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();
|
||||
@@ -285,15 +301,13 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_path_p *result_p = fx_object_get_private(
|
||||
result,
|
||||
FX_TYPE_PATH);
|
||||
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);
|
||||
struct fx_path_p *path_p
|
||||
= fx_object_get_private(paths[i], FX_TYPE_PATH);
|
||||
append_path(result_p, path_p);
|
||||
}
|
||||
}
|
||||
@@ -366,9 +380,9 @@ 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);
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_get_cstr, path);
|
||||
}
|
||||
|
||||
size_t fx_path_length(const fx_path *path)
|
||||
@@ -400,9 +414,8 @@ static fx_status path_to_string(
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
struct fx_path_p *path = fx_object_get_private(
|
||||
obj->v_object,
|
||||
FX_TYPE_PATH);
|
||||
struct fx_path_p *path
|
||||
= fx_object_get_private(obj->v_object, FX_TYPE_PATH);
|
||||
|
||||
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL);
|
||||
return FX_SUCCESS;
|
||||
|
||||
Reference in New Issue
Block a user