Codechange: replace lengthof with std::size in Windows specific code

This commit is contained in:
rubidium42 2024-04-10 20:12:33 +02:00 committed by rubidium42
parent 6bc4a62c27
commit 442daf58da
6 changed files with 17 additions and 17 deletions

View File

@ -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<int>(std::size(Lmode)));
#endif
FILE *f = nullptr;
std::string buf;

View File

@ -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<DWORD>(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<DWORD>(std::size(path)));
if (!dls_file.LoadFile(path)) return "Can't load GM DLS collection";
}

View File

@ -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<DWORD>(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<DWORD>(std::size(scheme));
url_components.lpszHostName = hostname;
url_components.dwHostNameLength = lengthof(hostname);
url_components.dwHostNameLength = static_cast<DWORD>(std::size(hostname));
url_components.lpszUrlPath = url_path;
url_components.dwUrlPathLength = lengthof(url_path);
url_components.dwUrlPathLength = static_cast<DWORD>(std::size(url_path));
WinHttpCrackUrl(this->uri.c_str(), 0, 0, &url_components);
/* Create the HTTP connection. */

View File

@ -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<DWORD>(std::size(buffer)), nullptr) == 0) {
this->message.assign(fmt::format("Unknown error {}", this->error));
} else {
this->message.assign(FS2OTTD(buffer));

View File

@ -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<DWORD>(std::size(buffer)), nullptr) == 0) {
return fmt::format("Unknown error {}", error_code);
}

View File

@ -180,7 +180,7 @@ void FiosGetDrives(FileList &file_list)
wchar_t drives[256];
const wchar_t *s;
GetLogicalDriveStrings(lengthof(drives), drives);
GetLogicalDriveStrings(static_cast<DWORD>(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<DWORD>(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<DWORD>(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<DWORD>(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<int>(std::size(lang))) == 0 ||
GetLocaleInfoA(userUiLocale, LOCALE_SISO3166CTRYNAME, country, static_cast<int>(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<int>(std::size(_cur_iso_locale)));
}
int OTTDStringCompare(std::string_view s1, std::string_view s2)