From 442daf58da862acc6a1f25415cd0eb34977d844b Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Wed, 10 Apr 2024 20:12:33 +0200 Subject: [PATCH] Codechange: replace lengthof with std::size in Windows specific code --- src/fileio.cpp | 2 +- src/music/dmusic.cpp | 4 ++-- src/network/core/http_winhttp.cpp | 8 ++++---- src/network/core/os_abstraction.cpp | 2 +- src/os/windows/library_loader_win.cpp | 2 +- src/os/windows/win32.cpp | 16 ++++++++-------- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/fileio.cpp b/src/fileio.cpp index fc892ed242..db2db572b0 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -203,7 +203,7 @@ static FILE *FioFOpenFileSp(const std::string &filename, const char *mode, Searc * a string, but a variable, it 'renames' the variable, * so make that variable to makes it compile happily */ wchar_t Lmode[5]; - MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode, lengthof(Lmode)); + MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode, static_cast(std::size(Lmode))); #endif FILE *f = nullptr; std::string buf; diff --git a/src/music/dmusic.cpp b/src/music/dmusic.cpp index 8d0919ceb3..c7c2d97642 100644 --- a/src/music/dmusic.cpp +++ b/src/music/dmusic.cpp @@ -870,7 +870,7 @@ static const char *LoadDefaultDLSFile(const char *user_dls) DWORD buf_size = sizeof(dls_path); // Buffer size as to be given in bytes! if (SUCCEEDED(RegQueryValueEx(hkDM, L"GMFilePath", nullptr, nullptr, (LPBYTE)dls_path, &buf_size))) { wchar_t expand_path[MAX_PATH * 2]; - ExpandEnvironmentStrings(dls_path, expand_path, lengthof(expand_path)); + ExpandEnvironmentStrings(dls_path, expand_path, static_cast(std::size(expand_path))); if (!dls_file.LoadFile(expand_path)) Debug(driver, 1, "Failed to load default GM DLS file from registry"); } RegCloseKey(hkDM); @@ -880,7 +880,7 @@ static const char *LoadDefaultDLSFile(const char *user_dls) if (dls_file.instruments.empty()) { static const wchar_t *DLS_GM_FILE = L"%windir%\\System32\\drivers\\gm.dls"; wchar_t path[MAX_PATH]; - ExpandEnvironmentStrings(DLS_GM_FILE, path, lengthof(path)); + ExpandEnvironmentStrings(DLS_GM_FILE, path, static_cast(std::size(path))); if (!dls_file.LoadFile(path)) return "Can't load GM DLS collection"; } diff --git a/src/network/core/http_winhttp.cpp b/src/network/core/http_winhttp.cpp index 0efcbe69df..ccba25ee62 100644 --- a/src/network/core/http_winhttp.cpp +++ b/src/network/core/http_winhttp.cpp @@ -78,7 +78,7 @@ static std::string GetLastErrorAsString() DWORD error_code = GetLastError(); if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, GetModuleHandle(L"winhttp.dll"), error_code, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, lengthof(buffer), nullptr) == 0) { + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, static_cast(std::size(buffer)), nullptr) == 0) { return fmt::format("unknown error {}", error_code); } @@ -222,11 +222,11 @@ void NetworkHTTPRequest::Connect() /* Convert the URL to its components. */ url_components.dwStructSize = sizeof(url_components); url_components.lpszScheme = scheme; - url_components.dwSchemeLength = lengthof(scheme); + url_components.dwSchemeLength = static_cast(std::size(scheme)); url_components.lpszHostName = hostname; - url_components.dwHostNameLength = lengthof(hostname); + url_components.dwHostNameLength = static_cast(std::size(hostname)); url_components.lpszUrlPath = url_path; - url_components.dwUrlPathLength = lengthof(url_path); + url_components.dwUrlPathLength = static_cast(std::size(url_path)); WinHttpCrackUrl(this->uri.c_str(), 0, 0, &url_components); /* Create the HTTP connection. */ diff --git a/src/network/core/os_abstraction.cpp b/src/network/core/os_abstraction.cpp index 97647a4165..023fe5b116 100644 --- a/src/network/core/os_abstraction.cpp +++ b/src/network/core/os_abstraction.cpp @@ -83,7 +83,7 @@ const std::string &NetworkError::AsString() const #if defined(_WIN32) wchar_t buffer[512]; if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, this->error, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, lengthof(buffer), nullptr) == 0) { + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, static_cast(std::size(buffer)), nullptr) == 0) { this->message.assign(fmt::format("Unknown error {}", this->error)); } else { this->message.assign(FS2OTTD(buffer)); diff --git a/src/os/windows/library_loader_win.cpp b/src/os/windows/library_loader_win.cpp index eea8f061f9..608b9349c9 100644 --- a/src/os/windows/library_loader_win.cpp +++ b/src/os/windows/library_loader_win.cpp @@ -22,7 +22,7 @@ static std::string GetLoadError() wchar_t buffer[512]; if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, error_code, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, lengthof(buffer), nullptr) == 0) { + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, static_cast(std::size(buffer)), nullptr) == 0) { return fmt::format("Unknown error {}", error_code); } diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index e5ddba94c0..18655c3d42 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -180,7 +180,7 @@ void FiosGetDrives(FileList &file_list) wchar_t drives[256]; const wchar_t *s; - GetLogicalDriveStrings(lengthof(drives), drives); + GetLogicalDriveStrings(static_cast(std::size(drives)), drives); for (s = drives; *s != '\0';) { FiosItem *fios = &file_list.emplace_back(); fios->type = FIOS_TYPE_DRIVE; @@ -400,7 +400,7 @@ void DetermineBasePaths(const char *exe) /* Use the folder of the config file as working directory. */ wchar_t config_dir[MAX_PATH]; wcsncpy(path, convert_to_fs(_config_file, path, lengthof(path)), lengthof(path)); - if (!GetFullPathName(path, lengthof(config_dir), config_dir, nullptr)) { + if (!GetFullPathName(path, static_cast(std::size(config_dir)), config_dir, nullptr)) { Debug(misc, 0, "GetFullPathName failed ({})", GetLastError()); _searchpaths[SP_WORKING_DIR].clear(); } else { @@ -412,13 +412,13 @@ void DetermineBasePaths(const char *exe) } } - if (!GetModuleFileName(nullptr, path, lengthof(path))) { + if (!GetModuleFileName(nullptr, path, static_cast(std::size(path)))) { Debug(misc, 0, "GetModuleFileName failed ({})", GetLastError()); _searchpaths[SP_BINARY_DIR].clear(); } else { wchar_t exec_dir[MAX_PATH]; - wcsncpy(path, convert_to_fs(exe, path, lengthof(path)), lengthof(path)); - if (!GetFullPathName(path, lengthof(exec_dir), exec_dir, nullptr)) { + wcsncpy(path, convert_to_fs(exe, path, std::size(path)), std::size(path)); + if (!GetFullPathName(path, static_cast(std::size(exec_dir)), exec_dir, nullptr)) { Debug(misc, 0, "GetFullPathName failed ({})", GetLastError()); _searchpaths[SP_BINARY_DIR].clear(); } else { @@ -530,8 +530,8 @@ const char *GetCurrentLocale(const char *) const LCID userUiLocale = MAKELCID(userUiLang, SORT_DEFAULT); char lang[9], country[9]; - if (GetLocaleInfoA(userUiLocale, LOCALE_SISO639LANGNAME, lang, lengthof(lang)) == 0 || - GetLocaleInfoA(userUiLocale, LOCALE_SISO3166CTRYNAME, country, lengthof(country)) == 0) { + if (GetLocaleInfoA(userUiLocale, LOCALE_SISO639LANGNAME, lang, static_cast(std::size(lang))) == 0 || + GetLocaleInfoA(userUiLocale, LOCALE_SISO3166CTRYNAME, country, static_cast(std::size(country))) == 0) { /* Unable to retrieve the locale. */ return nullptr; } @@ -557,7 +557,7 @@ void Win32SetCurrentLocaleName(std::string iso_code) } } - MultiByteToWideChar(CP_UTF8, 0, iso_code.c_str(), -1, _cur_iso_locale, lengthof(_cur_iso_locale)); + MultiByteToWideChar(CP_UTF8, 0, iso_code.c_str(), -1, _cur_iso_locale, static_cast(std::size(_cur_iso_locale))); } int OTTDStringCompare(std::string_view s1, std::string_view s2)