From 36db93748eb090bfb88ef8e6b8a9ac0d6cd235df Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 28 May 2023 11:49:12 +0100 Subject: [PATCH] New utility function: dupwcs. Just like dupstr, but for wchar_t strings. --- misc.h | 1 + utils/CMakeLists.txt | 1 + utils/dupwcs.c | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 utils/dupwcs.c diff --git a/misc.h b/misc.h index c4f0e14c..be9ff9c0 100644 --- a/misc.h +++ b/misc.h @@ -26,6 +26,7 @@ char *host_strrchr(const char *s, int c); char *host_strduptrim(const char *s); char *dupstr(const char *s); +wchar_t *dupwcs(const wchar_t *s); char *dupcat_fn(const char *s1, ...); #define dupcat(...) dupcat_fn(__VA_ARGS__, (const char *)NULL) char *dupprintf(const char *fmt, ...) PRINTF_LIKE(1, 2); diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 3e43d4d7..38882059 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -25,6 +25,7 @@ add_sources_from_current_dir(utils dupcat.c dupprintf.c dupstr.c + dupwcs.c dup_mb_to_wc.c dup_wc_to_mb.c encode_utf8.c diff --git a/utils/dupwcs.c b/utils/dupwcs.c new file mode 100644 index 00000000..11fba330 --- /dev/null +++ b/utils/dupwcs.c @@ -0,0 +1,19 @@ +/* + * Allocate a duplicate of a NUL-terminated wchar_t string. + */ + +#include + +#include "defs.h" +#include "misc.h" + +wchar_t *dupwcs(const wchar_t *s) +{ + wchar_t *p = NULL; + if (s) { + int len = wcslen(s); + p = snewn(len + 1, wchar_t); + wcscpy(p, s); + } + return p; +}