9 #ifndef TurtleBrains_FileUtilities_hpp
10 #define TurtleBrains_FileUtilities_hpp
12 #include <turtle_brains/core/tb_defines.hpp>
13 #include <turtle_brains/core/tb_types.hpp>
14 #include <turtle_brains/core/tb_string.hpp>
18 #include <type_traits>
22 class DynamicStructure;
25 namespace TurtleBrains::Core::FileUtilities
28 void WriteVariableLengthEncoding(
uint64 length, OutputFile& outputFile);
33 template<
typename Type> Type ReadVariableLength(std::istream& inputFile)
35 return tbCore::RangedCast<Type, tbCore::uint64>(ReadVariableLengthEncoding(inputFile));
48 template<
typename Type>
void WriteBinary(
const Type&
object, OutputFile& outputFile)
55 tb_static_error_if((std::is_same<Type, DynamicStructure>::value),
"This is the wrong WriteBinary, use tbCore::WriteBinary(object, file);");
56 outputFile.write(
reinterpret_cast<const char*
>(&
object),
sizeof(Type));
62 template<>
void WriteBinary<String>(
const String&
object, OutputFile& outputFile);
67 inline void WriteBinary(
const void*
object,
size_t size, OutputFile& outputFile)
69 outputFile.write(
reinterpret_cast<const char*
>(
object), size);
81 template<
typename Type>
void ReadBinary(Type&
object, InputFile& inputFile)
89 tb_static_error_if((std::is_same<Type, DynamicStructure>::value),
"This is the wrong ReadBinary, use object = tbCore::ReadBinary(file);");
90 inputFile.read(
reinterpret_cast<char*
>(&
object),
sizeof(Type));
103 template<>
void ReadBinary<String>(
String&
object, InputFile& inputFile);
115 template<
typename Type> Type ReadBinary(InputFile& inputFile)
118 ReadBinary(
object, inputFile);
129 inline void ReadBinary(
void*
object,
size_t size, InputFile& inputFile)
131 inputFile.read(
reinterpret_cast<char*
>(
object), size);
134 std::vector<tbCore::byte> LoadBinaryFileContents(
const String& filePath);
136 String LoadFileContentsToString(
const String& filePath,
bool trimTrailingWhitespace =
false);
137 bool SaveStringContentsToFile(
const String& filePath,
const String& stringContents);
#define tb_static_error_if(errorTest, message)
Definition: tb_error.hpp:51
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:125
std::string String
Definition: tb_string.hpp:302
std::uint32_t uint32
Unsigned integer with a size of 32 bits. Supports values from 0 to 4294967295, (2^32 - 1).
Definition: tb_types.hpp:27
std::uint64_t uint64
Unsigned integer with a size of 64 bits, Supports values from 0 to (2^64 - 1).
Definition: tb_types.hpp:29