From 324c43eeb2654fa689c9e324080d02f62393a142 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Tue, 30 May 2023 22:38:55 +0200 Subject: [PATCH] Codechange: let FiosGetDiskFreeSpace only return disk space and split FiosGetCurrentPath off --- src/console_cmds.cpp | 5 +---- src/fios.cpp | 12 +++--------- src/fios.h | 3 ++- src/fios_gui.cpp | 12 ++++++------ src/os/os2/os2.cpp | 25 +++++++------------------ src/os/unix/unix.cpp | 13 ++++--------- src/os/windows/win32.cpp | 7 ++++--- 7 files changed, 27 insertions(+), 50 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 1df9541384..5cf8b1c958 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -514,8 +514,6 @@ DEF_CONSOLE_CMD(ConChangeDirectory) DEF_CONSOLE_CMD(ConPrintWorkingDirectory) { - const char *path; - if (argc == 0) { IConsolePrint(CC_HELP, "Print out the current working directory. Usage: 'pwd'."); return true; @@ -525,8 +523,7 @@ DEF_CONSOLE_CMD(ConPrintWorkingDirectory) _console_file_list.ValidateFileList(true); _console_file_list.InvalidateFileList(); - FiosGetDescText(&path, nullptr); - IConsolePrint(CC_DEFAULT, path); + IConsolePrint(CC_DEFAULT, FiosGetCurrentPath()); return true; } diff --git a/src/fios.cpp b/src/fios.cpp index 5a9e30dbb6..118c6ed5ed 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -39,7 +39,6 @@ extern bool FiosIsRoot(const std::string &path); extern bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb); extern bool FiosIsHiddenFile(const struct dirent *ent); extern void FiosGetDrives(FileList &file_list); -extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot); /* get the name of an oldstyle savegame */ extern void GetOldSaveGameName(const std::string &file, char *title, const char *last); @@ -128,16 +127,11 @@ const FiosItem *FileList::FindItem(const std::string_view file) } /** - * Get descriptive texts. Returns the path and free space - * left on the device - * @param path string describing the path - * @param total_free total free space in megabytes, optional (can be nullptr) - * @return StringID describing the path (free space or failure) + * Get the current path/working directory. */ -StringID FiosGetDescText(const char **path, uint64 *total_free) +std::string FiosGetCurrentPath() { - *path = _fios_path->c_str(); - return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE; + return *_fios_path; } /** diff --git a/src/fios.h b/src/fios.h index 70986135a2..1470ee2fa7 100644 --- a/src/fios.h +++ b/src/fios.h @@ -110,7 +110,8 @@ void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list); bool FiosBrowseTo(const FiosItem *item); -StringID FiosGetDescText(const char **path, uint64 *total_free); +std::string FiosGetCurrentPath(); +std::optional FiosGetDiskFreeSpace(const std::string &path); bool FiosDelete(const char *name); std::string FiosMakeHeightmapName(const char *name); std::string FiosMakeSavegameName(const char *name); diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index b23d20b3fa..2cde115cf6 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -420,19 +420,19 @@ public: break; case WID_SL_BACKGROUND: { - static const char *path = nullptr; - static StringID str = STR_ERROR_UNABLE_TO_READ_DRIVE; - static uint64 tot = 0; + static std::string path; + static std::optional free_space = std::nullopt; if (_fios_path_changed) { - str = FiosGetDescText(&path, &tot); + path = FiosGetCurrentPath(); + free_space = FiosGetDiskFreeSpace(path); _fios_path_changed = false; } Rect ir = r.Shrink(WidgetDimensions::scaled.framerect); - if (str != STR_ERROR_UNABLE_TO_READ_DRIVE) SetDParam(0, tot); - DrawString(ir.left, ir.right, ir.top + FONT_HEIGHT_NORMAL, str); + if (free_space.has_value()) SetDParam(0, free_space.value()); + DrawString(ir.left, ir.right, ir.top + FONT_HEIGHT_NORMAL, free_space.has_value() ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE); DrawString(ir.left, ir.right, ir.top, path, TC_BLACK); break; } diff --git a/src/os/os2/os2.cpp b/src/os/os2/os2.cpp index bc7ffdca99..3be323e407 100644 --- a/src/os/os2/os2.cpp +++ b/src/os/os2/os2.cpp @@ -92,32 +92,21 @@ void FiosGetDrives(FileList &file_list) #endif } -bool FiosGetDiskFreeSpace(const char *path, uint64 *tot) +std::optional FiosGetDiskFreeSpace(const std::string &path) { #ifndef __INNOTEK_LIBC__ struct diskfree_t free; char drive = path[0] - 'A' + 1; - if (tot != nullptr && _getdiskfree(drive, &free) == 0) { - *tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector; - return true; + if (_getdiskfree(drive, &free) == 0) { + return free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector; } +#elif defined(HAS_STATVFS) + struct statvfs s; - return false; -#else - uint64 free = 0; - -#ifdef HAS_STATVFS - { - struct statvfs s; - - if (statvfs(path, &s) != 0) return false; - free = (uint64)s.f_frsize * s.f_bavail; - } -#endif - if (tot != nullptr) *tot = free; - return true; + if (statvfs(path.c_str(), &s) == 0) return static_cast(s.f_frsize) * s.f_bavail; #endif + return std::nullopt; } bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb) diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index aa3c443e15..2dfce9f21b 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -67,23 +67,18 @@ void FiosGetDrives(FileList &file_list) return; } -bool FiosGetDiskFreeSpace(const char *path, uint64 *tot) +std::optional FiosGetDiskFreeSpace(const std::string &path) { - uint64 free = 0; - #ifdef __APPLE__ struct statfs s; - if (statfs(path, &s) != 0) return false; - free = (uint64)s.f_bsize * s.f_bavail; + if (statfs(path.c_str(), &s) == 0) return static_cast(s.f_bsize) * s.f_bavail; #elif defined(HAS_STATVFS) struct statvfs s; - if (statvfs(path, &s) != 0) return false; - free = (uint64)s.f_frsize * s.f_bavail; + if (statvfs(path.c_str(), &s) == 0) return static_cast(s.f_frsize) * s.f_bavail; #endif - if (tot != nullptr) *tot = free; - return true; + return std::nullopt; } bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb) diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index caf968f31e..a18f693d53 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -213,16 +213,17 @@ bool FiosIsHiddenFile(const struct dirent *ent) return (ent->dir->fd.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0; } -bool FiosGetDiskFreeSpace(const char *path, uint64 *tot) +std::optional FiosGetDiskFreeSpace(const std::string &path) { UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box ULARGE_INTEGER bytes_free; bool retval = GetDiskFreeSpaceEx(OTTD2FS(path).c_str(), &bytes_free, nullptr, nullptr); - if (retval && tot != nullptr) *tot = bytes_free.QuadPart; SetErrorMode(sem); // reset previous setting - return retval; + + if (retval) return bytes_free.QuadPart; + return std::nullopt; } void CreateConsole()