TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_file_utilities.hpp
1 
9 #ifndef TurtleBrains_FileUtilities_hpp
10 #define TurtleBrains_FileUtilities_hpp
11 
12 #include "tb_types.hpp"
13 #include "tb_defines.hpp"
14 
15 #include <fstream>
16 
17 namespace TurtleBrains
18 {
19  namespace Core
20  {
21  namespace FileUtilities
22  {
23  //Expects the data to be written in Little-Endian Byte Order.
24  void WriteVariableLengthEncoding(tbCore::uint64 length, std::ostream& outputFile);
25 
26  //Expects the data to be read in Little-Endian Byte Order.
27  tbCore::uint64 ReadVariableLengthEncoding(std::istream& inputFile);
28 
29  template<typename Type> Type ReadVariableLength(std::istream& inputFile)
30  {
31  return tbCore::RangedCast<Type, tbCore::uint64>(ReadVariableLengthEncoding(inputFile));
32  }
33 
34  //For all Binary functions below it is assumed that the file stream was opened in Binary mode. Calling these on
35  //files opened in text mode, which is default, will be likely to cause issues and headaches! You've been warned.
36 
37  template <typename Type> void WriteBinary(const Type& object, std::ostream& outputFile)
38  {
39  outputFile.write(reinterpret_cast<const char*>(&object), sizeof(Type));
40  }
41 
42  inline void WriteBinary(const tbCore::tbString& object, std::ostream& outputFile)
43  {
44  const tbCore::uint32 stringLength = object.size();
45  WriteVariableLengthEncoding(stringLength, outputFile);
46  outputFile.write(object.c_str(), stringLength + 1); //+1 to write the null-terminator.
47  }
48 
49  template <typename Type> void ReadBinary(Type& object, std::istream& inputFile)
50  {
51  inputFile.read(reinterpret_cast<char*>(&object), sizeof(Type));
52  }
53 
54  inline void ReadBinary(tbCore::tbString& object, std::istream& inputFile)
55  {
56  const tbCore::uint32 stringLength = ReadVariableLength<tbCore::uint32>(inputFile);
57 
58  object.clear();
59 
60  if (stringLength > 0)
61  {
62  object.resize(stringLength); //null-terminator is added already.
63  inputFile.read(&object.at(0), stringLength);
64  }
65 
66  //We didn't read the null-terminator that was written, I believe the original reason to write the
67  // null-terminator was to be capable of loading into custom strings etc, (doesn't make complete sense
68  // at this point... but, we can't remove it without breaking all previous file formats that ever called
69  // ReadBinary()/WriteBinary() with strings.
70  inputFile.seekg(1, std::ios_base::cur);
71  }
72 
73  template <typename Type> Type ReadBinary(std::istream& inputFile)
74  {
75  Type object;
76  ReadBinary(object, inputFile);
77  return object;
78  }
79 
80  template<> inline tbCore::tbString ReadBinary<tbCore::tbString>(std::istream& inputFile)
81  {
82  tbCore::tbString object;
83  ReadBinary(object, inputFile);
84  return object;
85  }
86 
87 
89 
90 
91  }; /* namespace FileUtilities */
92  }; /* namespace Core */
93 }; /* namespace TurtleBrains */
94 
95 namespace tbCore = TurtleBrains::Core;
96 
97 #endif /* TurtleBrains_FileUtilities_hpp */
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:21
uint64_t uint64
Unsigned integer with a size of 64 bits, Supports values from 0 to (2^64 - 1).
Definition: tb_types.hpp:30
void WriteBinary(const tbCore::DynamicStructure &data, std::ostream &output)
uint32_t uint32
Unsigned integer with a size of 32 bits. Supports values from 0 to 4294967295, (2^32 - 1)...
Definition: tb_types.hpp:28
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:88
std::string tbString
Definition: tb_string.hpp:335