diff --git a/include/private/vkd3d_string.h b/include/private/vkd3d_string.h new file mode 100644 index 00000000..16b15028 --- /dev/null +++ b/include/private/vkd3d_string.h @@ -0,0 +1,37 @@ +/* + * Copyright 2021 Hans-Kristian Arntzen for Valve Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef __VKD3D_STRING_H +#define __VKD3D_STRING_H + +#include "vkd3d_common.h" +#include + +/* Various string utilities. */ + +WCHAR *vkd3d_dup_entry_point(const char *str); +WCHAR *vkd3d_dup_entry_point_n(const char *str, size_t len); +WCHAR *vkd3d_dup_demangled_entry_point(const char *str); + +bool vkd3d_export_strequal(const WCHAR *a, const WCHAR *b); + +char *vkd3d_strdup(const char *str); +WCHAR *vkd3d_wstrdup(const WCHAR *str); +WCHAR *vkd3d_wstrdup_n(const WCHAR *str, size_t n); + +#endif /* __VKD3D_STRING_H */ \ No newline at end of file diff --git a/libs/vkd3d-common/meson.build b/libs/vkd3d-common/meson.build index 2fca0afd..44a5db6e 100644 --- a/libs/vkd3d-common/meson.build +++ b/libs/vkd3d-common/meson.build @@ -2,7 +2,8 @@ vkd3d_common_src = [ 'debug.c', 'memory.c', 'utf8.c', - 'profiling.c' + 'profiling.c', + 'string.c', ] vkd3d_common_lib = static_library('vkd3d_common', vkd3d_common_src, vkd3d_header_files, diff --git a/libs/vkd3d-common/string.c b/libs/vkd3d-common/string.c new file mode 100644 index 00000000..a1ef4491 --- /dev/null +++ b/libs/vkd3d-common/string.c @@ -0,0 +1,106 @@ +/* + * Copyright 2021 Hans-Kristian Arntzen for Valve Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#define VKD3D_DBG_CHANNEL VKD3D_DBG_CHANNEL_API + +#include "vkd3d_string.h" +#include "vkd3d_memory.h" + +char *vkd3d_strdup(const char *str) +{ + /* strdup() is actually not standard. */ + char *duped; + size_t len; + + len = strlen(str) + 1; + + duped = vkd3d_malloc(len); + if (duped) + memcpy(duped, str, len); + return duped; +} + +WCHAR *vkd3d_wstrdup(const WCHAR *str) +{ + WCHAR *duped; + size_t len; + + len = vkd3d_wcslen(str, sizeof(WCHAR)) + 1; + + duped = vkd3d_malloc(len * sizeof(WCHAR)); + if (duped) + memcpy(duped, str, len * sizeof(WCHAR)); + return duped; +} + +bool vkd3d_export_strequal(const WCHAR *a, const WCHAR *b) +{ + if (!a || !b) + return false; + + while (*a != '\0' && *b != '\0') + { + if (*a != *b) + return false; + a++; + b++; + } + return *a == *b; +} + +WCHAR *vkd3d_dup_entry_point(const char *str) +{ + return vkd3d_dup_entry_point_n(str, strlen(str)); +} + +WCHAR *vkd3d_dup_entry_point_n(const char *str, size_t len) +{ + WCHAR *duped; + size_t i; + + duped = vkd3d_malloc((len + 1) * sizeof(WCHAR)); + if (!duped) + return NULL; + + for (i = 0; i < len; i++) + duped[i] = (unsigned char)str[i]; + duped[len] = 0; + return duped; +} + +static bool is_valid_identifier_character(char v) +{ + return (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || v == '_'; +} + +WCHAR *vkd3d_dup_demangled_entry_point(const char *entry) +{ + const char *end_entry; + + while (*entry != '\0' && !is_valid_identifier_character(*entry)) + entry++; + + end_entry = entry; + while (*end_entry != '\0' && is_valid_identifier_character(*end_entry)) + end_entry++; + + if (entry == end_entry) + return NULL; + + return vkd3d_dup_entry_point_n(entry, end_entry - entry); +}