summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-11-12 17:54:34 -0500
committerDavid Robillard <d@drobilla.net>2022-11-16 10:22:55 -0500
commitd9feb39a41cf3d0dfc4c797f1a7aee99d8609c16 (patch)
tree86a9f1bdd51c8b5fd5e8e27506ef167a179b6fb5
parentde60d222d9b1ff7931b74ded75cd8e31d77d9bd9 (diff)
downloadlilv-d9feb39a41cf3d0dfc4c797f1a7aee99d8609c16.tar.gz
lilv-d9feb39a41cf3d0dfc4c797f1a7aee99d8609c16.tar.bz2
lilv-d9feb39a41cf3d0dfc4c797f1a7aee99d8609c16.zip
Use zix_path_parent_path()
-rw-r--r--src/filesystem.c46
-rw-r--r--src/filesystem.h8
-rw-r--r--src/state.c11
-rw-r--r--src/util.c9
-rw-r--r--test/test_filesystem.c28
5 files changed, 11 insertions, 91 deletions
diff --git a/src/filesystem.c b/src/filesystem.c
index a690338..945751f 100644
--- a/src/filesystem.c
+++ b/src/filesystem.c
@@ -2,8 +2,6 @@
// SPDX-License-Identifier: ISC
#include "filesystem.h"
-#include "lilv_config.h"
-#include "lilv_internal.h"
#include "zix/allocator.h"
#include "zix/filesystem.h"
@@ -11,24 +9,8 @@
#include <stdbool.h>
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
-static bool
-lilv_is_dir_sep(const char c)
-{
- return c == '/' || c == LILV_DIR_SEP[0];
-}
-
-#ifdef _WIN32
-static inline bool
-is_windows_path(const char* path)
-{
- return (isalpha(path[0]) && (path[1] == ':' || path[1] == '|') &&
- (path[2] == '/' || path[2] == '\\'));
-}
-#endif
-
bool
lilv_path_is_child(const char* path, const char* dir)
{
@@ -41,34 +23,6 @@ lilv_path_is_child(const char* path, const char* dir)
}
char*
-lilv_path_parent(const char* path)
-{
- const char* s = path + strlen(path) - 1; // Last character
-
- // Last non-slash
- for (; s > path && lilv_is_dir_sep(*s); --s) {
- }
-
- // Last internal slash
- for (; s > path && !lilv_is_dir_sep(*s); --s) {
- }
-
- // Skip duplicates
- for (; s > path && lilv_is_dir_sep(*s); --s) {
- }
-
- if (s == path) { // Hit beginning
- return lilv_is_dir_sep(*s) ? lilv_strdup("/") : lilv_strdup(".");
- }
-
- // Pointing to the last character of the result (inclusive)
- char* dirname = (char*)malloc(s - path + 2);
- memcpy(dirname, path, s - path + 1);
- dirname[s - path + 1] = '\0';
- return dirname;
-}
-
-char*
lilv_create_temporary_directory(const char* pattern)
{
char* const tmpdir = zix_temp_directory_path(NULL);
diff --git a/src/filesystem.h b/src/filesystem.h
index 5b355f1..acc8845 100644
--- a/src/filesystem.h
+++ b/src/filesystem.h
@@ -8,14 +8,6 @@ bool
lilv_path_is_child(const char* path, const char* dir);
/**
- Return the path to the directory that contains `path`.
-
- Returns the root path if `path` is the root path.
-*/
-char*
-lilv_path_parent(const char* path);
-
-/**
Create a unique temporary directory.
This is like lilv_create_temporary_directory_in(), except it creates the
diff --git a/src/state.c b/src/state.c
index 8442949..63b26e6 100644
--- a/src/state.c
+++ b/src/state.c
@@ -752,14 +752,15 @@ lilv_state_new_from_file(LilvWorld* world,
(subject) ? subject->node
: sord_node_from_serd_node(world->world, env, &node, NULL, NULL);
- char* dirname = lilv_path_parent(path);
- char* real_path = zix_canonical_path(NULL, dirname);
- char* dir_path = zix_path_join(NULL, real_path, NULL);
- LilvState* state =
+ const ZixStringView dirname = zix_path_parent_path(path);
+ char* const real_path = zix_canonical_path(NULL, dirname.data);
+ char* const dir_path = zix_path_join(NULL, real_path, NULL);
+
+ LilvState* const state =
new_state_from_model(world, map, model, subject_node, dir_path);
+
zix_free(NULL, dir_path);
zix_free(NULL, real_path);
- free(dirname);
serd_node_free(&node);
zix_free(NULL, abs_path);
diff --git a/src/util.c b/src/util.c
index e573a58..dd25e35 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1,7 +1,6 @@
// Copyright 2007-2019 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC
-#include "filesystem.h"
#include "lilv_internal.h"
#include "lilv/lilv.h"
@@ -9,6 +8,7 @@
#include "zix/allocator.h"
#include "zix/filesystem.h"
#include "zix/path.h"
+#include "zix/string_view.h"
#include <sys/stat.h>
@@ -262,8 +262,9 @@ update_latest(const char* path, const char* name, void* data)
char*
lilv_get_latest_copy(const char* path, const char* copy_path)
{
- char* copy_dir = lilv_path_parent(copy_path);
- Latest latest = {lilv_strjoin(copy_path, ".%u", NULL), 0, NULL};
+ char* copy_dir = zix_string_view_copy(NULL, zix_path_parent_path(copy_path));
+
+ Latest latest = {lilv_strjoin(copy_path, ".%u", NULL), 0, NULL};
struct stat st;
if (!stat(path, &st)) {
@@ -275,6 +276,6 @@ lilv_get_latest_copy(const char* path, const char* copy_path)
zix_dir_for_each(copy_dir, &latest, update_latest);
free(latest.pattern);
- free(copy_dir);
+ zix_free(NULL, copy_dir);
return latest.latest;
}
diff --git a/test/test_filesystem.c b/test/test_filesystem.c
index 6f3dc1e..2afbfda 100644
--- a/test/test_filesystem.c
+++ b/test/test_filesystem.c
@@ -6,17 +6,6 @@
#include "../src/filesystem.h"
#include <assert.h>
-#include <stdbool.h>
-#include <stdlib.h>
-#include <string.h>
-
-static bool
-equals(char* string, const char* expected)
-{
- const bool result = !strcmp(string, expected);
- free(string);
- return result;
-}
static void
test_path_is_child(void)
@@ -37,27 +26,10 @@ test_path_is_child(void)
assert(!lilv_path_is_child("/a/b/", "/c/"));
}
-static void
-test_path_parent(void)
-{
- assert(equals(lilv_path_parent("/"), "/"));
- assert(equals(lilv_path_parent("//"), "/"));
- assert(equals(lilv_path_parent("/a"), "/"));
- assert(equals(lilv_path_parent("/a/"), "/"));
- assert(equals(lilv_path_parent("/a///b/"), "/a"));
- assert(equals(lilv_path_parent("/a///b//"), "/a"));
- assert(equals(lilv_path_parent("/a/b"), "/a"));
- assert(equals(lilv_path_parent("/a/b/"), "/a"));
- assert(equals(lilv_path_parent("/a/b/c"), "/a/b"));
- assert(equals(lilv_path_parent("/a/b/c/"), "/a/b"));
- assert(equals(lilv_path_parent("a"), "."));
-}
-
int
main(void)
{
test_path_is_child();
- test_path_parent();
return 0;
}