mirror of https://github.com/OpenTTD/OpenTTD
Codechange: let FiosGetDiskFreeSpace only return disk space and split FiosGetCurrentPath off
parent
f2e704b9a7
commit
324c43eeb2
|
@ -514,8 +514,6 @@ DEF_CONSOLE_CMD(ConChangeDirectory)
|
||||||
|
|
||||||
DEF_CONSOLE_CMD(ConPrintWorkingDirectory)
|
DEF_CONSOLE_CMD(ConPrintWorkingDirectory)
|
||||||
{
|
{
|
||||||
const char *path;
|
|
||||||
|
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
IConsolePrint(CC_HELP, "Print out the current working directory. Usage: 'pwd'.");
|
IConsolePrint(CC_HELP, "Print out the current working directory. Usage: 'pwd'.");
|
||||||
return true;
|
return true;
|
||||||
|
@ -525,8 +523,7 @@ DEF_CONSOLE_CMD(ConPrintWorkingDirectory)
|
||||||
_console_file_list.ValidateFileList(true);
|
_console_file_list.ValidateFileList(true);
|
||||||
_console_file_list.InvalidateFileList();
|
_console_file_list.InvalidateFileList();
|
||||||
|
|
||||||
FiosGetDescText(&path, nullptr);
|
IConsolePrint(CC_DEFAULT, FiosGetCurrentPath());
|
||||||
IConsolePrint(CC_DEFAULT, path);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
src/fios.cpp
12
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 FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb);
|
||||||
extern bool FiosIsHiddenFile(const struct dirent *ent);
|
extern bool FiosIsHiddenFile(const struct dirent *ent);
|
||||||
extern void FiosGetDrives(FileList &file_list);
|
extern void FiosGetDrives(FileList &file_list);
|
||||||
extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
|
|
||||||
|
|
||||||
/* get the name of an oldstyle savegame */
|
/* get the name of an oldstyle savegame */
|
||||||
extern void GetOldSaveGameName(const std::string &file, char *title, const char *last);
|
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
|
* Get the current path/working directory.
|
||||||
* 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)
|
|
||||||
*/
|
*/
|
||||||
StringID FiosGetDescText(const char **path, uint64 *total_free)
|
std::string FiosGetCurrentPath()
|
||||||
{
|
{
|
||||||
*path = _fios_path->c_str();
|
return *_fios_path;
|
||||||
return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -110,7 +110,8 @@ void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list);
|
||||||
|
|
||||||
bool FiosBrowseTo(const FiosItem *item);
|
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);
|
bool FiosDelete(const char *name);
|
||||||
std::string FiosMakeHeightmapName(const char *name);
|
std::string FiosMakeHeightmapName(const char *name);
|
||||||
std::string FiosMakeSavegameName(const char *name);
|
std::string FiosMakeSavegameName(const char *name);
|
||||||
|
|
|
@ -420,19 +420,19 @@ public:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WID_SL_BACKGROUND: {
|
case WID_SL_BACKGROUND: {
|
||||||
static const char *path = nullptr;
|
static std::string path;
|
||||||
static StringID str = STR_ERROR_UNABLE_TO_READ_DRIVE;
|
static std::optional<uint64_t> free_space = std::nullopt;
|
||||||
static uint64 tot = 0;
|
|
||||||
|
|
||||||
if (_fios_path_changed) {
|
if (_fios_path_changed) {
|
||||||
str = FiosGetDescText(&path, &tot);
|
path = FiosGetCurrentPath();
|
||||||
|
free_space = FiosGetDiskFreeSpace(path);
|
||||||
_fios_path_changed = false;
|
_fios_path_changed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect ir = r.Shrink(WidgetDimensions::scaled.framerect);
|
Rect ir = r.Shrink(WidgetDimensions::scaled.framerect);
|
||||||
|
|
||||||
if (str != STR_ERROR_UNABLE_TO_READ_DRIVE) SetDParam(0, tot);
|
if (free_space.has_value()) SetDParam(0, free_space.value());
|
||||||
DrawString(ir.left, ir.right, ir.top + FONT_HEIGHT_NORMAL, str);
|
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);
|
DrawString(ir.left, ir.right, ir.top, path, TC_BLACK);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,32 +92,21 @@ void FiosGetDrives(FileList &file_list)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
|
std::optional<uint64_t> FiosGetDiskFreeSpace(const std::string &path)
|
||||||
{
|
{
|
||||||
#ifndef __INNOTEK_LIBC__
|
#ifndef __INNOTEK_LIBC__
|
||||||
struct diskfree_t free;
|
struct diskfree_t free;
|
||||||
char drive = path[0] - 'A' + 1;
|
char drive = path[0] - 'A' + 1;
|
||||||
|
|
||||||
if (tot != nullptr && _getdiskfree(drive, &free) == 0) {
|
if (_getdiskfree(drive, &free) == 0) {
|
||||||
*tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
|
return free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
#elif defined(HAS_STATVFS)
|
||||||
return false;
|
|
||||||
#else
|
|
||||||
uint64 free = 0;
|
|
||||||
|
|
||||||
#ifdef HAS_STATVFS
|
|
||||||
{
|
|
||||||
struct statvfs s;
|
struct statvfs s;
|
||||||
|
|
||||||
if (statvfs(path, &s) != 0) return false;
|
if (statvfs(path.c_str(), &s) == 0) return static_cast<uint64_t>(s.f_frsize) * s.f_bavail;
|
||||||
free = (uint64)s.f_frsize * s.f_bavail;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (tot != nullptr) *tot = free;
|
|
||||||
return true;
|
|
||||||
#endif
|
#endif
|
||||||
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb)
|
bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb)
|
||||||
|
|
|
@ -67,23 +67,18 @@ void FiosGetDrives(FileList &file_list)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
|
std::optional<uint64_t> FiosGetDiskFreeSpace(const std::string &path)
|
||||||
{
|
{
|
||||||
uint64 free = 0;
|
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
struct statfs s;
|
struct statfs s;
|
||||||
|
|
||||||
if (statfs(path, &s) != 0) return false;
|
if (statfs(path.c_str(), &s) == 0) return static_cast<uint64_t>(s.f_bsize) * s.f_bavail;
|
||||||
free = (uint64)s.f_bsize * s.f_bavail;
|
|
||||||
#elif defined(HAS_STATVFS)
|
#elif defined(HAS_STATVFS)
|
||||||
struct statvfs s;
|
struct statvfs s;
|
||||||
|
|
||||||
if (statvfs(path, &s) != 0) return false;
|
if (statvfs(path.c_str(), &s) == 0) return static_cast<uint64_t>(s.f_frsize) * s.f_bavail;
|
||||||
free = (uint64)s.f_frsize * s.f_bavail;
|
|
||||||
#endif
|
#endif
|
||||||
if (tot != nullptr) *tot = free;
|
return std::nullopt;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb)
|
bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb)
|
||||||
|
|
|
@ -213,16 +213,17 @@ bool FiosIsHiddenFile(const struct dirent *ent)
|
||||||
return (ent->dir->fd.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
|
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
|
UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box
|
||||||
|
|
||||||
ULARGE_INTEGER bytes_free;
|
ULARGE_INTEGER bytes_free;
|
||||||
bool retval = GetDiskFreeSpaceEx(OTTD2FS(path).c_str(), &bytes_free, nullptr, nullptr);
|
bool retval = GetDiskFreeSpaceEx(OTTD2FS(path).c_str(), &bytes_free, nullptr, nullptr);
|
||||||
if (retval && tot != nullptr) *tot = bytes_free.QuadPart;
|
|
||||||
|
|
||||||
SetErrorMode(sem); // reset previous setting
|
SetErrorMode(sem); // reset previous setting
|
||||||
return retval;
|
|
||||||
|
if (retval) return bytes_free.QuadPart;
|
||||||
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateConsole()
|
void CreateConsole()
|
||||||
|
|
Loading…
Reference in New Issue