summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2024-11-24 13:19:54 -0500
committerDavid Robillard <d@drobilla.net>2024-11-24 20:03:48 -0500
commit8366ebd3a153b25273176c75f4623d7a592144c5 (patch)
treed953a42ba47d85fe3b6095ab93aa91cce9111e4b /src
parent8ef53b017d085a36bab7580111b8169ae07a0666 (diff)
downloadlilv-8366ebd3a153b25273176c75f4623d7a592144c5.tar.gz
lilv-8366ebd3a153b25273176c75f4623d7a592144c5.tar.bz2
lilv-8366ebd3a153b25273176c75f4623d7a592144c5.zip
Use zix_expand_environment_strings()
The removed test was the last that needed access to private code, so also remove all of the build complication around ensuring there's a static library to test (avoiding a double build in many cases).
Diffstat (limited to 'src')
-rw-r--r--src/util.c78
-rw-r--r--src/world.c5
2 files changed, 3 insertions, 80 deletions
diff --git a/src/util.c b/src/util.c
index c0047d9..278f0de 100644
--- a/src/util.c
+++ b/src/util.c
@@ -12,7 +12,6 @@
#include <sys/stat.h>
-#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
@@ -133,83 +132,6 @@ lilv_get_lang(void)
return lang;
}
-#ifndef _WIN32
-
-/** Append suffix to dst, update dst_len, and return the realloc'd result. */
-static char*
-strappend(char* dst, size_t* dst_len, const char* suffix, size_t suffix_len)
-{
- dst = (char*)realloc(dst, *dst_len + suffix_len + 1);
- memcpy(dst + *dst_len, suffix, suffix_len);
- dst[(*dst_len += suffix_len)] = '\0';
- return dst;
-}
-
-/** Append the value of the environment variable var to dst. */
-static char*
-append_var(char* dst, size_t* dst_len, const char* var)
-{
- // Get value from environment
- const char* val = getenv(var);
- if (val) { // Value found, append it
- return strappend(dst, dst_len, val, strlen(val));
- }
-
- // No value found, append variable reference as-is
- return strappend(strappend(dst, dst_len, "$", 1), dst_len, var, strlen(var));
-}
-
-#endif
-
-/** Expand variables (e.g. POSIX ~ or $FOO, Windows %FOO%) in `path`. */
-char*
-lilv_expand(const char* path)
-{
-#ifdef _WIN32
- char* out = (char*)malloc(MAX_PATH);
- ExpandEnvironmentStrings(path, out, MAX_PATH);
-#else
- char* out = NULL;
- size_t len = 0;
-
- const char* start = path; // Start of current chunk to copy
- for (const char* s = path; *s;) {
- if (*s == '$') {
- // Hit $ (variable reference, e.g. $VAR_NAME)
- for (const char* t = s + 1;; ++t) {
- if (!*t || (!isupper(*t) && !isdigit(*t) && *t != '_')) {
- // Append preceding chunk
- out = strappend(out, &len, start, s - start);
-
- // Append variable value (or $VAR_NAME if not found)
- char* var = (char*)calloc(t - s, 1);
- memcpy(var, s + 1, t - s - 1);
- out = append_var(out, &len, var);
- free(var);
-
- // Continue after variable reference
- start = s = t;
- break;
- }
- }
- } else if (*s == '~' && (*(s + 1) == '/' || !*(s + 1))) {
- // Hit ~ before slash or end of string (home directory reference)
- out = strappend(out, &len, start, s - start);
- out = append_var(out, &len, "HOME");
- start = ++s;
- } else {
- ++s;
- }
- }
-
- if (*start) {
- out = strappend(out, &len, start, strlen(start));
- }
-#endif
-
- return out;
-}
-
char*
lilv_find_free_path(const char* in_path,
bool (*exists)(const char*, const void*),
diff --git a/src/world.c b/src/world.c
index d4d7f8b..1dfae9c 100644
--- a/src/world.c
+++ b/src/world.c
@@ -1,4 +1,4 @@
-// Copyright 2007-2019 David Robillard <d@drobilla.net>
+// Copyright 2007-2024 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC
#include "lilv_config.h" // IWYU pragma: keep
@@ -7,6 +7,7 @@
#include "lilv/lilv.h"
#include "serd/serd.h"
#include "sord/sord.h"
+#include "zix/environment.h"
#include "zix/filesystem.h"
#include "zix/tree.h"
@@ -966,7 +967,7 @@ load_dir_entry(const char* dir, const char* name, void* data)
static void
lilv_world_load_directory(LilvWorld* world, const char* dir_path)
{
- char* path = lilv_expand(dir_path);
+ char* const path = zix_expand_environment_strings(NULL, dir_path);
if (path) {
zix_dir_for_each(path, world, load_dir_entry);
free(path);