#pragma once #include "CoreTypes.h" #include "TypeTraits/TypeTraits.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 bool LoadFileToString(TString& Result, FStringView Path, FileSystem::EEncoding Encoding = FileSystem::EEncoding::Default, bool bVerify = false); template REDCRAFTUTILITY_API bool LoadFileToString (FString&, FStringView, FileSystem::EEncoding, bool); template REDCRAFTUTILITY_API bool LoadFileToString (FWString&, FStringView, FileSystem::EEncoding, bool); template REDCRAFTUTILITY_API bool LoadFileToString (FU8String&, FStringView, FileSystem::EEncoding, bool); template REDCRAFTUTILITY_API bool LoadFileToString(FU16String&, FStringView, FileSystem::EEncoding, bool); template REDCRAFTUTILITY_API bool LoadFileToString(FU32String&, FStringView, FileSystem::EEncoding, bool); /** * 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 bool SaveStringToFile(TStringView String, FStringView Path, FileSystem::EEncoding Encoding = FileSystem::EEncoding::Default, bool bWithBOM = true); template REDCRAFTUTILITY_API bool SaveStringToFile (FStringView, FStringView, FileSystem::EEncoding, bool); template REDCRAFTUTILITY_API bool SaveStringToFile (FWStringView, FStringView, FileSystem::EEncoding, bool); template REDCRAFTUTILITY_API bool SaveStringToFile (FU8StringView, FStringView, FileSystem::EEncoding, bool); template REDCRAFTUTILITY_API bool SaveStringToFile(FU16StringView, FStringView, FileSystem::EEncoding, bool); template REDCRAFTUTILITY_API bool SaveStringToFile(FU32StringView, FStringView, FileSystem::EEncoding, bool); /** * 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; } NAMESPACE_END(FileSystem) NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END