Remove helper function FileExists

Use std::filesystem at caller site instead.

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
This commit is contained in:
Ralph Sennhauser
2026-06-20 10:35:23 +02:00
parent 5ca4c93f55
commit 181a29597d
7 changed files with 16 additions and 28 deletions
-14
View File
@@ -48,20 +48,6 @@ bool DirectoryExists(const OsPath& path)
}
bool FileExists(const OsPath& pathname)
{
try
{
return std::filesystem::is_regular_file(pathname.string());
}
catch (std::filesystem::filesystem_error& err)
{
debug_printf("FileExists: failed to check if file '%s' exists, reason: %s\n", pathname.string8().c_str(), err.what());
}
return false;
}
Status GetFileInfo(const OsPath& pathname, CFileInfo* pPtrInfo)
{
try
-1
View File
@@ -36,7 +36,6 @@
#include <vector>
bool DirectoryExists(const OsPath& path);
bool FileExists(const OsPath& pathname);
// (bundling size and mtime avoids a second expensive call to stat())
class CFileInfo
+3 -2
View File
@@ -98,6 +98,7 @@ that of Atlas depending on commandline parameters.
#include <cstdlib>
#include <ctime>
#include <exception>
#include <filesystem>
#include <js/RootingAPI.h>
#include <js/TypeDecls.h>
#include <js/Value.h>
@@ -549,7 +550,7 @@ static void RunGameOrAtlas(const std::span<const char* const> argv)
if (isVisualReplay || isNonVisualReplay)
{
if (!FileExists(replayFile))
if (!std::filesystem::is_regular_file(replayFile.string()))
{
debug_printf("ERROR: The requested replay file '%s' does not exist!\n", replayFile.string8().c_str());
return;
@@ -570,7 +571,7 @@ static void RunGameOrAtlas(const std::span<const char* const> argv)
debug_printf("Skipping file '%s' which does not have a mod file extension.\n", modPath.string8().c_str());
continue;
}
if (!FileExists(modPath))
if (!std::filesystem::is_regular_file(modPath.string()))
{
debug_printf("ERROR: The mod file '%s' does not exist!\n", modPath.string8().c_str());
continue;
+2 -1
View File
@@ -105,6 +105,7 @@
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <filesystem>
#include <fmt/format.h>
#include <fstream>
#include <js/CallArgs.h>
@@ -877,7 +878,7 @@ bool Autostart(const CmdLineArgs& args)
bool AutostartVisualReplay(const std::string& replayFile)
{
if (!FileExists(OsPath(replayFile)))
if (!std::filesystem::is_regular_file(replayFile))
return false;
g_Game = new CGame(false);
+1 -1
View File
@@ -187,7 +187,7 @@ Paths::Paths(const CmdLineArgs& args)
}
// make sure it's valid
if(!FileExists(pathname))
if(!std::filesystem::is_regular_file(pathname.string()))
{
LOGERROR("Cannot find executable (expected at '%s')", pathname.string8());
WARN_IF_ERR(StatusFromErrno());
+3 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -34,6 +34,7 @@
#include "ps/Pyrogenesis.h"
#include <ctime>
#include <filesystem>
#include <iomanip>
#include <memory>
#include <sstream>
@@ -107,7 +108,7 @@ OsPath createDateIndexSubdirectory(const OsPath& parentDir)
sprintf_s(directory, ARRAY_SIZE(directory), "%04d-%02d-%02d_%04d", now->tm_year+1900, now->tm_mon+1, now->tm_mday, ++i);
path = parentDir / CStr(directory);
if (DirectoryExists(path) || FileExists(path))
if (DirectoryExists(path) || std::filesystem::is_regular_file(path.string()))
continue;
if (CreateDirectories(path, 0700, ++tries > maxTries) == INFO::OK)
+7 -7
View File
@@ -84,7 +84,7 @@ bool VisualReplay::StartVisualReplay(const OsPath& directory)
const OsPath replayFile = VisualReplay::GetDirectoryPath() / directory / L"commands.txt";
if (!FileExists(replayFile))
if (!std::filesystem::is_regular_file(replayFile.string()))
return false;
g_Game = new CGame(false);
@@ -93,7 +93,7 @@ bool VisualReplay::StartVisualReplay(const OsPath& directory)
bool VisualReplay::ReadCacheFile(const ScriptInterface& scriptInterface, JS::MutableHandleObject cachedReplaysObject)
{
if (!FileExists(GetCacheFilePath()))
if (!std::filesystem::is_regular_file(GetCacheFilePath().string()))
return false;
std::ifstream cacheStream(OsString(GetCacheFilePath()));
@@ -192,7 +192,7 @@ JS::HandleObject VisualReplay::ReloadReplayCache(const ScriptInterface& scriptIn
{
if (compareFiles)
{
if (!FileExists(replayFile))
if (!std::filesystem::is_regular_file(replayFile.string()))
continue;
CFileInfo fileInfo;
GetFileInfo(replayFile, &fileInfo);
@@ -208,7 +208,7 @@ JS::HandleObject VisualReplay::ReloadReplayCache(const ScriptInterface& scriptIn
JS::RootedValue replayData(rq.cx, LoadReplayData(scriptInterface, directory));
if (replayData.isNull())
{
if (!FileExists(replayFile))
if (!std::filesystem::is_regular_file(replayFile.string()))
continue;
CFileInfo fileInfo;
GetFileInfo(replayFile, &fileInfo);
@@ -363,7 +363,7 @@ JS::Value VisualReplay::LoadReplayData(const ScriptInterface& scriptInterface, c
// The directory argument must not be constant, otherwise concatenating will fail
const OsPath replayFile = GetDirectoryPath() / directory / L"commands.txt";
if (!FileExists(replayFile))
if (!std::filesystem::is_regular_file(replayFile.string()))
return JS::NullValue();
// Get file size and modification date
@@ -461,7 +461,7 @@ JS::Value VisualReplay::GetReplayAttributes(const ScriptInterface& scriptInterfa
// Return empty object if file doesn't exist
const OsPath replayFile = GetDirectoryPath() / directoryName / L"commands.txt";
if (!FileExists(replayFile))
if (!std::filesystem::is_regular_file(replayFile.string()))
return attribs;
// Open file
@@ -500,7 +500,7 @@ bool VisualReplay::HasReplayMetadata(const OsPath& directoryName)
{
const OsPath filePath(GetDirectoryPath() / directoryName / L"metadata.json");
if (!FileExists(filePath))
if (!std::filesystem::is_regular_file(filePath.string()))
return false;
CFileInfo fileInfo;