20 return FileError::FILE_NOT_FOUND;
23 sreamOut = std::ifstream((filePath / fileName).fileSystemPath(),
25 ? std::ios::in | std::ios::binary
29 return (sreamOut.eof() || sreamOut.fail())
30 ? FileError::FILE_READ_ERROR
36 std::ifstream streamIn;
37 const FileError ret =
readFile(filePath, fileName, fileType, streamIn);
38 if ( ret != FileError::NONE)
43 std::stringstream buffer;
44 buffer << streamIn.rdbuf();
45 contentOut = buffer.str();
47 return contentOut.empty() ? FileError::FILE_EMPTY : FileError::NONE;
52 std::ifstream streamIn;
53 const FileError ret =
readFile(filePath, fileName, fileType, streamIn);
54 if ( ret != FileError::NONE)
59 std::stringstream buffer;
60 buffer << streamIn.rdbuf();
61 contentOut = buffer.str();
63 return contentOut.empty() ? FileError::FILE_EMPTY : FileError::NONE;
68 if (contentOut ==
nullptr || sizeInOut == 0u)
70 return FileError::FILE_TARGET_BUFFER_ERROR;
73 std::ifstream streamIn;
74 const FileError ret =
readFile(filePath, fileName, fileType, streamIn);
75 if ( ret != FileError::NONE)
80 streamIn.seekg(0, std::ios::end);
81 const size_t fileSize =
to_size(streamIn.tellg());
85 return FileError::FILE_EMPTY;
88 std::memset(contentOut, 0, sizeInOut *
sizeof(
Byte));
89 sizeInOut = std::min(sizeInOut, fileSize);
90 streamIn.read(
reinterpret_cast<char*
>(contentOut), sizeInOut);
92 return FileError::NONE;
97 if (!filePath.
empty() && content !=
nullptr && length > 0)
101 return FileError::FILE_NOT_FOUND;
104 std::ofstream outputFile((filePath / fileName).fileSystemPath(),
106 ? std::ios::out | std::ios::binary
109 outputFile.write(content, length);
113 return FileError::FILE_WRITE_ERROR;
116 return FileError::NONE;
119 return FileError::FILE_NOT_FOUND;
130 string ret { input };
131 ret.erase(std::remove(std::begin(ret), std::end(ret),
'\"'), std::end(ret));
139 input.fileSystemPath().filename().generic_string().c_str(),
140 ResourcePath( input.fileSystemPath().parent_path().generic_string().c_str() )
147 const bool ret = is_directory(filePath.fileSystemPath(), ec);
148 return ec ? false : ret;
159 const bool ret = create_directories(path.fileSystemPath(), ec);
162 return FileError::FILE_CREATE_ERROR;
167 return pathExists(path) ? FileError::NONE : FileError::FILE_CREATE_ERROR;
171 return FileError::NONE;
179 if (std::filesystem::remove_all(path.fileSystemPath(), ec) == 0)
186 return FileError::FILE_DELETE_ERROR;
190 return FileError::NONE;
196 const bool result = is_regular_file(filePathAndName.fileSystemPath(), ec);
197 return ec ? false : result;
203 const bool result = std::filesystem::is_empty(filePathAndName.fileSystemPath(), ec);
204 return ec ? false : result;
211 return FileError::FILE_NOT_FOUND;
215 const auto timeStamp = std::filesystem::last_write_time(filePathAndName.fileSystemPath(), ec).time_since_epoch();
218 return FileError::FILE_READ_ERROR;
221 timeOutSec =
to_U64(std::chrono::duration_cast<std::chrono::seconds>(timeStamp).count());
223 return FileError::NONE;
228 return std::count_if( std::filesystem::directory_iterator( path.fileSystemPath() ),
229 std::filesystem::directory_iterator{},
230 [](
const std::filesystem::path& p){ return std::filesystem::is_regular_file( p ); });
235 if (overwriteExisting &&
fileExists(filePathAndName))
237 return std::ofstream(filePathAndName.
string().c_str(), std::fstream::in | std::fstream::trunc).good();
245 return std::ifstream(filePathAndName.
string().c_str(), std::fstream::in).good();
250 if (fileName.empty() || !
fileExists(filePath, fileName))
252 return FileError::FILE_NOT_FOUND;
267 return ret ? FileError::NONE : FileError::FILE_OPEN_ERROR;
272 if ( fileName.empty() )
274 return FileError::FILE_NOT_FOUND;
280 return FileError::FILE_NOT_FOUND;
284 if (std::filesystem::remove(fullPath.fileSystemPath(), ec))
286 return FileError::NONE;
289 return FileError::FILE_DELETE_ERROR;
292FileError
copyFile(
const ResourcePath& sourcePath,
const std::string_view sourceName,
const ResourcePath& targetPath,
const std::string_view targetName,
const bool overwrite)
294 if (sourceName.empty() || targetName.empty())
296 return FileError::FILE_NOT_FOUND;
303 return FileError::FILE_NOT_FOUND;
310 return FileError::FILE_OVERWRITE_ERROR;
314 if (copy_file(source.fileSystemPath(),
315 target.fileSystemPath(),
316 std::filesystem::copy_options::overwrite_existing,
319 return FileError::NONE;
322 return FileError::FILE_COPY_ERROR;
329 return FileError::FILE_NOT_FOUND;
334 return FileError::FILE_OVERWRITE_ERROR;
338 if ( !std::filesystem::exists( targetPath.fileSystemPath(), ec ) )
340 std::filesystem::create_directories( targetPath.fileSystemPath(), ec );
345 std::filesystem::copy(sourcePath.fileSystemPath(),
346 targetPath.fileSystemPath(),
347 (overwrite ? std::filesystem::copy_options::overwrite_existing : std::filesystem::copy_options::none) |
348 (recursively ? std::filesystem::copy_options::recursive : std::filesystem::copy_options::none),
352 return ec ? FileError::FILE_COPY_ERROR: FileError::NONE;
357 const std::filesystem::path file_name(fileName);
359 const std::filesystem::recursive_directory_iterator end;
360 const auto it = std::find_if(std::filesystem::recursive_directory_iterator( filePath.fileSystemPath()),
362 [&file_name](
const std::filesystem::directory_entry& e)
364 const bool ret = e.path().filename() == file_name;
369 return FileError::FILE_NOT_FOUND;
372 foundPath = it->path().string();
373 return FileError::NONE;
383 return string { fileName.fileSystemPath().extension().
string().c_str() };
406bool hasExtension(
const std::string_view filePath,
const std::string_view extensionNoDot)
409 if (extensionNoDot.empty())
411 return targetExt.empty();
414 if (targetExt.empty())
428 for (
const auto& p : std::filesystem::directory_iterator(filePath.fileSystemPath()))
432 if (is_regular_file(p.status()))
434 const auto extensionString = p.path().extension().string().substr( 1 );
442 if (std::filesystem::remove(p.path()))
457 catch ([[maybe_unused]]
const std::exception &ex)
472 for (
const auto& p : std::filesystem::directory_iterator( filePath.fileSystemPath() ))
476 if (is_regular_file(p.status()))
478 const auto extensionString = p.path().extension().string().substr( 1 );
482 const U64 timeOutSec =
to_U64(std::chrono::duration_cast<std::chrono::seconds>(p.last_write_time().time_since_epoch()).count());
484 listInOut.emplace_back(FileEntry
487 ._lastWriteTime = timeOutSec
500 catch ([[maybe_unused]]
const std::exception& ex)
512 auto currentPath = std::filesystem::current_path();
513 currentPath.append(argv0);
516 std::filesystem::path p(canonical(currentPath, ec));
518 return p.make_preferred().string().c_str();
bool CompareIgnoreCase(const char *a, const char *b) noexcept
Handle console commands that start with a forward slash.
FileError removeDirectory(const ResourcePath &path)
FileError openFile(const std::string_view cmd, const ResourcePath &filePath, const std::string_view fileName)
std::lock_guard< mutex > LockGuard
ResourcePath getTopLevelFolderName(const ResourcePath &filePath)
bool hasExtension(const ResourcePath &filePath, const std::string_view extensionNoDot)
FileError writeFile(const ResourcePath &filePath, const std::string_view fileName, const char *content, const size_t length, const FileType fileType)
FileError readFile(const ResourcePath &filePath, std::string_view fileName, FileType fileType, std::ifstream &sreamOut)
constexpr U64 to_U64(const T value)
size_t numberOfFilesInDirectory(const ResourcePath &path)
bool CallSystemCmd(std::string_view cmd, std::string_view args)
ResourcePath getWorkingDirectory()
bool deleteAllFiles(const ResourcePath &filePath, const char *extension, const char *extensionToSkip)
string getExtension(const std::string_view fileName)
FileError createDirectory(const ResourcePath &path)
FileError copyFile(const ResourcePath &sourcePath, const std::string_view sourceName, const ResourcePath &targetPath, const std::string_view targetName, const bool overwrite)
const SysInfo & const_sysInfo() noexcept
bool fileIsEmpty(const ResourcePath &filePathAndName)
bool getAllFilesInDirectory(const ResourcePath &filePath, FileList &listInOut, const char *extensionNoDot)
FileError findFile(const ResourcePath &filePath, const std::string_view fileName, string &foundPath)
string stripExtension(const std::string_view fileName) noexcept
constexpr size_t to_size(const T value)
FileError copyDirectory(const ResourcePath &sourcePath, const ResourcePath &targetPath, bool recursively, bool overwrite)
FileError deleteFile(const ResourcePath &filePath, const std::string_view fileName)
bool fileExists(const ResourcePath &filePathAndName)
bool pathExists(const ResourcePath &filePath)
string extractFilePathAndName(char *argv0)
FileError fileLastWriteTime(const ResourcePath &filePathAndName, U64 &timeOutSec)
FileNameAndPath splitPathToNameAndLocation(const ResourcePath &input)
bool createFile(const ResourcePath &filePathAndName, const bool overwriteExisting)
string stripQuotes(const std::string_view input)
StringReturnType< N > string() const noexcept
bool empty() const noexcept
ResourcePath _workingDirectory