#pragma once #include "CoreTypes.h" #include "TypeTraits/TypeTraits.h" #include "Templates/Utility.h" #include "Templates/Function.h" #include "Containers/Array.h" #include "Strings/StringView.h" #include "Strings/String.h" NAMESPACE_REDCRAFT_BEGIN NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_BEGIN(FileSystem) /** The encoding of the text file. */ enum class EEncoding : uint8 { Default, Narrow, Wide, UTF8, UTF16BE, UTF16LE, UTF32BE, UTF32LE, }; /** Loads the file at the specified path into the byte array. */ REDCRAFTUTILITY_API bool LoadFileToArray(TArray& Result, FStringView Path); /** Saves the byte array to the file at the specified path. */ REDCRAFTUTILITY_API bool SaveArrayToFile(TArrayView Data, FStringView Path); /** * Loads the file at the specified path into the string. * * @param Result - The string to load the file into. * @param Path - The path to the file to load. * @param Encoding - The encoding of the file. The default value indicates automatic detection. * @param bVerify - Whether to verify the character validity of the file. * * @return true if the file was successfully loaded, false otherwise. */ template REDCRAFTUTILITY_API bool LoadFileToString(TString& Result, FStringView Path, FileSystem::EEncoding Encoding = FileSystem::EEncoding::Default, bool bVerify = false); /** * Saves the string to the file at the specified path. * * @param String - The string to save to the file. * @param Path - The path to the file to save. * @param Encoding - The encoding of the file. The default value indicates the same as the string. * @param bWithBOM - Whether to write the BOM character at the beginning of the file. Not valid for narrow and wide encoding. * * @return true if the file was successfully saved, false otherwise. */ template REDCRAFTUTILITY_API bool SaveStringToFile(TStringView String, FStringView Path, FileSystem::EEncoding Encoding = FileSystem::EEncoding::Default, bool bWithBOM = true); /** * Saves the string to the file at the specified path. * * @param String - The string to save to the file. * @param Path - The path to the file to save. * @param Encoding - The encoding of the file. The default value indicates the same as the string. * @param bWithBOM - Whether to write the BOM character at the beginning of the file. Not valid for narrow and wide encoding. * * @return true if the file was successfully saved, false otherwise. */ template requires (CConvertibleTo || CConvertibleTo || CConvertibleTo || CConvertibleTo || CConvertibleTo) bool SaveStringToFile(T&& String, FStringView Path, FileSystem::EEncoding Encoding = FileSystem::EEncoding::Default, bool bWithBOM = true) { if constexpr (CConvertibleTo) return SaveStringToFile(FStringView (Forward(String)), Path, Encoding, bWithBOM); else if constexpr (CConvertibleTo) return SaveStringToFile(FWStringView (Forward(String)), Path, Encoding, bWithBOM); else if constexpr (CConvertibleTo) return SaveStringToFile(FU8StringView (Forward(String)), Path, Encoding, bWithBOM); else if constexpr (CConvertibleTo) return SaveStringToFile(FU16StringView(Forward(String)), Path, Encoding, bWithBOM); else if constexpr (CConvertibleTo) return SaveStringToFile(FU32StringView(Forward(String)), Path, Encoding, bWithBOM); return false; } REDCRAFTUTILITY_API size_t FileSize(FStringView Path); REDCRAFTUTILITY_API bool Delete(FStringView Path); REDCRAFTUTILITY_API bool Exists(FStringView Path); REDCRAFTUTILITY_API bool Copy(FStringView Destination, FStringView Source); REDCRAFTUTILITY_API bool Rename(FStringView Destination, FStringView Source); REDCRAFTUTILITY_API bool CreateDirectory(FStringView Path, bool bRecursive = false); REDCRAFTUTILITY_API bool DeleteDirectory(FStringView Path, bool bRecursive = false); REDCRAFTUTILITY_API bool ExistsDirectory(FStringView Path); REDCRAFTUTILITY_API bool IterateDirectory(FStringView Path, TFunctionRef Visitor); NAMESPACE_END(FileSystem) NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END