Codechange: let FiosGetDiskFreeSpace only return disk space and split FiosGetCurrentPath off

This commit is contained in:
Rubidium 2023-05-30 22:38:55 +02:00 committed by rubidium42
parent f2e704b9a7
commit 324c43eeb2
7 changed files with 27 additions and 50 deletions

View File

@ -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;
}

View File

@ -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;
}
/**

View File

@ -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<uint64_t> FiosGetDiskFreeSpace(const std::string &path);
bool FiosDelete(const char *name);
std::string FiosMakeHeightmapName(const char *name);
std::string FiosMakeSavegameName(const char *name);

View File

@ -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<uint64_t> 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;
}

View File

@ -92,32 +92,21 @@ void FiosGetDrives(FileList &file_list)
#endif
}
bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
std::optional<uint64_t> 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<uint64_t>(s.f_frsize) * s.f_bavail;
#endif
return std::nullopt;
}
bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb)

View File

@ -67,23 +67,18 @@ void FiosGetDrives(FileList &file_list)
return;
}
bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
std::optional<uint64_t> 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<uint64_t>(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<uint64_t>(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)

View File

@ -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<uint64_t> 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()