From 391bb80bb9ac8dbdd08db87734beed0a0d3bb7e9 Mon Sep 17 00:00:00 2001
From: Cameron Cawley <[EMAIL REDACTED]>
Date: Fri, 5 Mar 2021 16:53:06 +0000
Subject: [PATCH] Replace duplicate functions and lstrlen/lstrcat with SDL
string functions
---
src/SDL_log.c | 4 +-
src/audio/wasapi/SDL_wasapi.c | 44 ++--------------------
src/core/windows/SDL_windows.h | 2 +
src/filesystem/windows/SDL_sysfilesystem.c | 12 +++---
src/video/winrt/SDL_winrtgamebar.cpp | 2 +-
src/video/winrt/SDL_winrtvideo.cpp | 2 +-
6 files changed, 16 insertions(+), 50 deletions(-)
diff --git a/src/SDL_log.c b/src/SDL_log.c
index 7dfe1ebbc..9a5d1c480 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -376,7 +376,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
#if !defined(HAVE_STDIO_H) && !defined(__WINRT__)
/* Screen output to stderr, if console was attached. */
if (consoleAttached == 1) {
- if (!WriteConsole(stderrHandle, tstr, lstrlen(tstr), &charsWritten, NULL)) {
+ if (!WriteConsole(stderrHandle, tstr, SDL_tcslen(tstr), &charsWritten, NULL)) {
OutputDebugString(TEXT("Error calling WriteConsole\r\n"));
if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
OutputDebugString(TEXT("Insufficient heap memory to write message\r\n"));
@@ -384,7 +384,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
}
} else if (consoleAttached == 2) {
- if (!WriteFile(stderrHandle, output, lstrlenA(output), &charsWritten, NULL)) {
+ if (!WriteFile(stderrHandle, output, SDL_strlen(output), &charsWritten, NULL)) {
OutputDebugString(TEXT("Error calling WriteFile\r\n"));
}
}
diff --git a/src/audio/wasapi/SDL_wasapi.c b/src/audio/wasapi/SDL_wasapi.c
index 3f86c801b..e74f80c9c 100644
--- a/src/audio/wasapi/SDL_wasapi.c
+++ b/src/audio/wasapi/SDL_wasapi.c
@@ -58,42 +58,6 @@ static const IID SDL_IID_IAudioCaptureClient = { 0xc8adbd64, 0xe71e, 0x48a0,{ 0x
static const GUID SDL_KSDATAFORMAT_SUBTYPE_PCM = { 0x00000001, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
static const GUID SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { 0x00000003, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
-static SDL_bool
-WStrEqual(const WCHAR *a, const WCHAR *b)
-{
- while (*a) {
- if (*a != *b) {
- return SDL_FALSE;
- }
- a++;
- b++;
- }
- return *b == 0;
-}
-
-static size_t
-WStrLen(const WCHAR *wstr)
-{
- size_t retval = 0;
- if (wstr) {
- while (*(wstr++)) {
- retval++;
- }
- }
- return retval;
-}
-
-static WCHAR *
-WStrDupe(const WCHAR *wstr)
-{
- const size_t len = (WStrLen(wstr) + 1) * sizeof (WCHAR);
- WCHAR *retval = (WCHAR *) SDL_malloc(len);
- if (retval) {
- SDL_memcpy(retval, wstr, len);
- }
- return retval;
-}
-
void
WASAPI_RemoveDevice(const SDL_bool iscapture, LPCWSTR devid)
@@ -103,7 +67,7 @@ WASAPI_RemoveDevice(const SDL_bool iscapture, LPCWSTR devid)
DevIdList *prev = NULL;
for (i = deviceid_list; i; i = next) {
next = i->next;
- if (WStrEqual(i->str, devid)) {
+ if (SDL_wcscmp(i->str, devid) == 0) {
if (prev) {
prev->next = next;
} else {
@@ -153,7 +117,7 @@ WASAPI_AddDevice(const SDL_bool iscapture, const char *devname, WAVEFORMATEXTENS
/* see if we already have this one. */
for (devidlist = deviceid_list; devidlist; devidlist = devidlist->next) {
- if (WStrEqual(devidlist->str, devid)) {
+ if (SDL_wcscmp(devidlist->str, devid) == 0) {
return; /* we already have this. */
}
}
@@ -163,7 +127,7 @@ WASAPI_AddDevice(const SDL_bool iscapture, const char *devname, WAVEFORMATEXTENS
return; /* oh well. */
}
- devid = WStrDupe(devid);
+ devid = SDL_wcsdup(devid);
if (!devid) {
SDL_free(devidlist);
return; /* oh well. */
@@ -690,7 +654,7 @@ WASAPI_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
if (!devid) { /* is default device? */
this->hidden->default_device_generation = SDL_AtomicGet(iscapture ? &WASAPI_DefaultCaptureGeneration : &WASAPI_DefaultPlaybackGeneration);
} else {
- this->hidden->devid = WStrDupe(devid);
+ this->hidden->devid = SDL_wcsdup(devid);
if (!this->hidden->devid) {
return SDL_OutOfMemory();
}
diff --git a/src/core/windows/SDL_windows.h b/src/core/windows/SDL_windows.h
index 035538cd6..1d5e06b0d 100644
--- a/src/core/windows/SDL_windows.h
+++ b/src/core/windows/SDL_windows.h
@@ -46,10 +46,12 @@
#if UNICODE
#define WIN_StringToUTF8 WIN_StringToUTF8W
#define WIN_UTF8ToString WIN_UTF8ToStringW
+#define SDL_tcslen SDL_wcslen
#define SDL_tcsstr SDL_wcsstr
#else
#define WIN_StringToUTF8 WIN_StringToUTF8A
#define WIN_UTF8ToString WIN_UTF8ToStringA
+#define SDL_tcslen SDL_strlen
#define SDL_tcsstr SDL_strstr
#endif
diff --git a/src/filesystem/windows/SDL_sysfilesystem.c b/src/filesystem/windows/SDL_sysfilesystem.c
index a1e2874fe..5e223d7cc 100644
--- a/src/filesystem/windows/SDL_sysfilesystem.c
+++ b/src/filesystem/windows/SDL_sysfilesystem.c
@@ -143,7 +143,7 @@ SDL_GetPrefPath(const char *org, const char *app)
return NULL;
}
- new_wpath_len = lstrlenW(worg) + lstrlenW(wapp) + lstrlenW(path) + 3;
+ new_wpath_len = SDL_wcslen(worg) + SDL_wcslen(wapp) + SDL_wcslen(path) + 3;
if ((new_wpath_len + 1) > MAX_PATH) {
SDL_free(worg);
@@ -153,8 +153,8 @@ SDL_GetPrefPath(const char *org, const char *app)
}
if (*worg) {
- lstrcatW(path, L"\\");
- lstrcatW(path, worg);
+ SDL_wcslcat(path, L"\\", SDL_arraysize(path));
+ SDL_wcslcat(path, worg, SDL_arraysize(path));
}
SDL_free(worg);
@@ -167,8 +167,8 @@ SDL_GetPrefPath(const char *org, const char *app)
}
}
- lstrcatW(path, L"\\");
- lstrcatW(path, wapp);
+ SDL_wcslcat(path, L"\\", SDL_arraysize(path));
+ SDL_wcslcat(path, wapp, SDL_arraysize(path));
SDL_free(wapp);
api_result = CreateDirectoryW(path, NULL);
@@ -179,7 +179,7 @@ SDL_GetPrefPath(const char *org, const char *app)
}
}
- lstrcatW(path, L"\\");
+ SDL_wcslcat(path, L"\\", SDL_arraysize(path));
retval = WIN_StringToUTF8W(path);
diff --git a/src/video/winrt/SDL_winrtgamebar.cpp b/src/video/winrt/SDL_winrtgamebar.cpp
index 8c925b1ca..4d1acc1d6 100644
--- a/src/video/winrt/SDL_winrtgamebar.cpp
+++ b/src/video/winrt/SDL_winrtgamebar.cpp
@@ -89,7 +89,7 @@ WINRT_GetGameBar()
IGameBarStatics_ *pGameBar = NULL;
HRESULT hr;
- hr = ::WindowsCreateString(wClassName, (UINT32)wcslen(wClassName), &hClassName);
+ hr = ::WindowsCreateString(wClassName, (UINT32)SDL_wcslen(wClassName), &hClassName);
if (FAILED(hr)) {
goto done;
}
diff --git a/src/video/winrt/SDL_winrtvideo.cpp b/src/video/winrt/SDL_winrtvideo.cpp
index a731ecff6..322940aea 100644
--- a/src/video/winrt/SDL_winrtvideo.cpp
+++ b/src/video/winrt/SDL_winrtvideo.cpp
@@ -782,7 +782,7 @@ WINRT_CreateDisplayRequest(_THIS)
ABI::Windows::System::Display::IDisplayRequest * pDisplayRequest = nullptr;
HRESULT hr;
- hr = ::WindowsCreateString(wClassName, (UINT32)wcslen(wClassName), &hClassName);
+ hr = ::WindowsCreateString(wClassName, (UINT32)SDL_wcslen(wClassName), &hClassName);
if (FAILED(hr)) {
goto done;
}