From 8366ebd3a153b25273176c75f4623d7a592144c5 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 24 Nov 2024 13:19:54 -0500 Subject: 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). --- src/util.c | 78 -------------------------------------------------------------- 1 file changed, 78 deletions(-) (limited to 'src/util.c') 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 -#include #include #include #include @@ -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*), -- cgit v1.2.1