Changeset - r27456:9d0c8630d512
[Not reviewed]
master
0 7 0
Rubidium - 12 months ago 2023-05-30 20:38:55
rubidium@openttd.org
Codechange: let FiosGetDiskFreeSpace only return disk space and split FiosGetCurrentPath off
7 files changed with 27 insertions and 50 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -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;
 
}
 

	
src/fios.cpp
Show inline comments
 
@@ -39,7 +39,6 @@ extern bool FiosIsRoot(const std::string
 
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
 
}
 

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

	
 
/**
src/fios.h
Show inline comments
 
@@ -110,7 +110,8 @@ void FiosGetHeightmapList(SaveLoadOperat
 

	
 
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);
src/fios_gui.cpp
Show inline comments
 
@@ -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;
 
			}
src/os/os2/os2.cpp
Show inline comments
 
@@ -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;
 
	}
 

	
 
	return false;
 
#else
 
	uint64 free = 0;
 
#elif defined(HAS_STATVFS)
 
	struct statvfs s;
 

	
 
#ifdef 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;
 
#endif
 
	return std::nullopt;
 
}
 

	
 
bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb)
src/os/unix/unix.cpp
Show inline comments
 
@@ -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)
src/os/windows/win32.cpp
Show inline comments
 
@@ -213,16 +213,17 @@ bool FiosIsHiddenFile(const struct diren
 
	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()
0 comments (0 inline, 0 general)