Replace duplicate functions and lstrlen/lstrcat with SDL string functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
diff --git a/src/SDL_log.c b/src/SDL_log.c
index 7dfe1eb..9a5d1c4 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 3f86c80..e74f80c 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 035538c..1d5e06b 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 a1e2874..5e223d7 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 8c925b1..4d1acc1 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 a731ecf..322940a 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;
}