diff options
author | David Robillard <d@drobilla.net> | 2024-11-15 09:23:33 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2024-11-17 14:40:27 -0500 |
commit | 9e71c9372d25b699d63a8110ab26a3239ca48cd6 (patch) | |
tree | 3415c03bd253bb990d98ee823bb21b5ffa63989e /src | |
parent | 9e74b423c87f50c6f50cc40cece057e439ddca57 (diff) | |
download | jalv-9e71c9372d25b699d63a8110ab26a3239ca48cd6.tar.gz jalv-9e71c9372d25b699d63a8110ab26a3239ca48cd6.tar.bz2 jalv-9e71c9372d25b699d63a8110ab26a3239ca48cd6.zip |
Move string utilities to a separate compilation unit
These don't really have anything to do with logging. Also replace a call to
the non-standard strdup() in the process.
Diffstat (limited to 'src')
-rw-r--r-- | src/control.c | 4 | ||||
-rw-r--r-- | src/jack.c | 1 | ||||
-rw-r--r-- | src/jalv.c | 1 | ||||
-rw-r--r-- | src/jalv_console.c | 1 | ||||
-rw-r--r-- | src/log.c | 25 | ||||
-rw-r--r-- | src/log.h | 8 | ||||
-rw-r--r-- | src/state.c | 1 | ||||
-rw-r--r-- | src/string_utils.c | 30 | ||||
-rw-r--r-- | src/string_utils.h | 21 |
9 files changed, 57 insertions, 35 deletions
diff --git a/src/control.c b/src/control.c index d663078..451eac5 100644 --- a/src/control.c +++ b/src/control.c @@ -4,6 +4,7 @@ #include "control.h" #include "log.h" +#include "string_utils.h" #include "lilv/lilv.h" #include "lv2/atom/atom.h" @@ -13,7 +14,6 @@ #include <stdbool.h> #include <stdint.h> #include <stdlib.h> -#include <string.h> /// Order scale points by value static int @@ -89,7 +89,7 @@ new_port_control(LilvWorld* const world, id->points[np].value = lilv_node_as_float(lilv_scale_point_get_value(p)); id->points[np].label = - strdup(lilv_node_as_string(lilv_scale_point_get_label(p))); + jalv_strdup(lilv_node_as_string(lilv_scale_point_get_label(p))); ++np; } // TODO: Non-float scale points? @@ -9,6 +9,7 @@ #include "log.h" #include "lv2_evbuf.h" #include "port.h" +#include "string_utils.h" #include "types.h" #include "lilv/lilv.h" @@ -11,6 +11,7 @@ #include "nodes.h" #include "port.h" #include "state.h" +#include "string_utils.h" #include "types.h" #include "urids.h" #include "worker.h" diff --git a/src/jalv_console.c b/src/jalv_console.c index bda894c..2ca69b3 100644 --- a/src/jalv_console.c +++ b/src/jalv_console.c @@ -9,6 +9,7 @@ #include "options.h" #include "port.h" #include "state.h" +#include "string_utils.h" #include "types.h" #include "lilv/lilv.h" @@ -18,8 +18,6 @@ #include <stdarg.h> #include <stdbool.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> void jalv_print_control(const Jalv* const jalv, @@ -30,29 +28,6 @@ jalv_print_control(const Jalv* const jalv, jalv_log(JALV_LOG_INFO, "%s = %f\n", lilv_node_as_string(sym), value); } -char* -jalv_strdup(const char* const str) -{ - const size_t len = strlen(str); - char* copy = (char*)malloc(len + 1); - memcpy(copy, str, len + 1); - return copy; -} - -char* -jalv_strjoin(const char* const a, const char* const b) -{ - const size_t a_len = strlen(a); - const size_t b_len = strlen(b); - char* const out = (char*)malloc(a_len + b_len + 1); - - memcpy(out, a, a_len); - memcpy(out + a_len, b, b_len); - out[a_len + b_len] = '\0'; - - return out; -} - JALV_LOG_FUNC(2, 0) static int jalv_vlog(const JalvLogLevel level, const char* const fmt, va_list ap) @@ -42,14 +42,6 @@ typedef struct { void jalv_print_control(const Jalv* jalv, const struct Port* port, float value); -/// Return a newly allocated copy of a string -char* -jalv_strdup(const char* str); - -/// Return a newly allocated concatenation of two strings -char* -jalv_strjoin(const char* a, const char* b); - /// Print a log message to stderr with a GCC-like prefix and color JALV_LOG_FUNC(2, 3) int diff --git a/src/state.c b/src/state.c index 26c5c20..5bc781c 100644 --- a/src/state.c +++ b/src/state.c @@ -6,6 +6,7 @@ #include "jalv_internal.h" #include "log.h" #include "port.h" +#include "string_utils.h" #include "lilv/lilv.h" #include "lv2/core/lv2.h" diff --git a/src/string_utils.c b/src/string_utils.c new file mode 100644 index 0000000..a690eb1 --- /dev/null +++ b/src/string_utils.c @@ -0,0 +1,30 @@ +// Copyright 2007-2022 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "string_utils.h" + +#include <stdlib.h> +#include <string.h> + +char* +jalv_strdup(const char* const str) +{ + const size_t len = strlen(str); + char* copy = (char*)malloc(len + 1); + memcpy(copy, str, len + 1); + return copy; +} + +char* +jalv_strjoin(const char* const a, const char* const b) +{ + const size_t a_len = strlen(a); + const size_t b_len = strlen(b); + char* const out = (char*)malloc(a_len + b_len + 1); + + memcpy(out, a, a_len); + memcpy(out + a_len, b, b_len); + out[a_len + b_len] = '\0'; + + return out; +} diff --git a/src/string_utils.h b/src/string_utils.h new file mode 100644 index 0000000..53f09fd --- /dev/null +++ b/src/string_utils.h @@ -0,0 +1,21 @@ +// Copyright 2007-2022 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#ifndef JALV_STRING_UTILS_H +#define JALV_STRING_UTILS_H + +#include "attributes.h" + +JALV_BEGIN_DECLS + +/// Return a newly allocated copy of a string +char* +jalv_strdup(const char* str); + +/// Return a newly allocated concatenation of two strings +char* +jalv_strjoin(const char* a, const char* b); + +JALV_END_DECLS + +#endif // JALV_STRING_UTILS_H |