diff options
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 53 |
1 files changed, 21 insertions, 32 deletions
@@ -1,37 +1,26 @@ -/* - Copyright 2007-2019 David Robillard <d@drobilla.net> - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#include "filesystem.h" +// Copyright 2007-2019 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + #include "lilv_internal.h" #include "lilv/lilv.h" #include "serd/serd.h" +#include "zix/allocator.h" +#include "zix/filesystem.h" +#include "zix/path.h" +#include "zix/string_view.h" #include <sys/stat.h> -#include <sys/types.h> #include <ctype.h> #include <errno.h> #include <stdarg.h> #include <stdbool.h> -#include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> // IWYU pragma: keep void lilv_free(void* ptr) @@ -47,7 +36,7 @@ lilv_strjoin(const char* first, ...) memcpy(result, first, len); - va_list args; + va_list args; // NOLINT(cppcoreguidelines-init-variables) va_start(args, first); while (1) { const char* const s = va_arg(args, const char*); @@ -127,10 +116,9 @@ lilv_get_lang(void) lang[i] = '-'; // Convert _ to - } else if (env_lang[i] >= 'A' && env_lang[i] <= 'Z') { lang[i] = env_lang[i] + ('a' - 'A'); // Convert to lowercase - } else if (env_lang[i] >= 'a' && env_lang[i] <= 'z') { - lang[i] = env_lang[i]; // Lowercase letter, copy verbatim - } else if (env_lang[i] >= '0' && env_lang[i] <= '9') { - lang[i] = env_lang[i]; // Digit, copy verbatim + } else if ((env_lang[i] >= 'a' && env_lang[i] <= 'z') || + (env_lang[i] >= '0' && env_lang[i] <= '9')) { + lang[i] = env_lang[i]; // Lowercase letter or digit, copy verbatim } else if (env_lang[i] == '\0' || env_lang[i] == '.') { // End, or start of suffix (e.g. en_CA.utf-8), finished lang[i] = '\0'; @@ -231,7 +219,7 @@ lilv_find_free_path(const char* in_path, char* path = (char*)malloc(in_path_len + 7); memcpy(path, in_path, in_path_len + 1); - for (unsigned i = 2; i < 1000000u; ++i) { + for (unsigned i = 2U; i < 1000000U; ++i) { if (!exists(path, user_data)) { return path; } @@ -251,13 +239,13 @@ static void update_latest(const char* path, const char* name, void* data) { Latest* latest = (Latest*)data; - char* entry_path = lilv_path_join(path, name); + char* entry_path = zix_path_join(NULL, path, name); unsigned num = 0; if (sscanf(entry_path, latest->pattern, &num) == 1) { struct stat st; if (!stat(entry_path, &st)) { if (st.st_mtime >= latest->time) { - free(latest->latest); + zix_free(NULL, latest->latest); latest->latest = entry_path; } } else { @@ -265,7 +253,7 @@ update_latest(const char* path, const char* name, void* data) } } if (entry_path != latest->latest) { - free(entry_path); + zix_free(NULL, entry_path); } } @@ -273,8 +261,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)) { @@ -283,9 +272,9 @@ lilv_get_latest_copy(const char* path, const char* copy_path) LILV_ERRORF("stat(%s) (%s)\n", path, strerror(errno)); } - lilv_dir_for_each(copy_dir, &latest, update_latest); + zix_dir_for_each(copy_dir, &latest, update_latest); free(latest.pattern); - free(copy_dir); + zix_free(NULL, copy_dir); return latest.latest; } |