jubilant-funicular
IOManager.cpp
1 #include "nta/IOManager.h"
2 #include "nta/Logger.h"
3 
4 namespace nta {
5  bool IOManager::fileExists(crstring path) {
6  return std::ifstream(path.c_str()).good();
7  }
8  void IOManager::readFileToBuffer(crstring filePath, FileBuffer& buffer) {
9  Logger::writeToLog("Reading " + filePath + " to a buffer");
10  std::ifstream file(filePath.c_str(), std::ios::binary);
11  if (file.fail()) {
12  Logger::writeErrorToLog("Failed to open file " + filePath,
13  MISSING_RESOURCE);
14  }
15  file.seekg(0, std::ios::end);
16  unsigned int fileSize = file.tellg();
17  file.seekg(0, std::ios::beg);
18  fileSize -= file.tellg();
19  buffer.resize(fileSize);
20  file.read((char*)&buffer[0], fileSize);
21  file.close();
22  Logger::writeToLog("Read file");
23  }
25  void IOManager::readFileToBuffer(crstring filePath, std::string& buffer) {
26  Logger::writeToLog("Reading " + filePath + " to a buffer");
27  std::ifstream file(filePath.c_str(), std::ios::binary);
28  if (file.fail()) {
29  Logger::writeErrorToLog("Failed to open file " + filePath,
30  MISSING_RESOURCE);
31  }
32  file.seekg(0, std::ios::end);
33  unsigned int fileSize = file.tellg();
34  file.seekg(0, std::ios::beg);
35  fileSize -= file.tellg();
36  buffer.resize(fileSize);
37  file.read((char*)&buffer[0], fileSize);
38  file.close();
39  Logger::writeToLog("Read file");
40  }
41  void IOManager::writeFileFromBuffer(crstring filePath, const FileBuffer& buffer) {
42  Logger::writeToLog("Writing buffer to " + filePath);
43  std::ofstream file(filePath.c_str(), std::ios::binary);
44  file.write((char*)&buffer[0], buffer.size());
45  Logger::writeToLog("Wrote buffer");
46  }
47  void IOManager::writeFileFromBuffer(crstring filePath, crstring buffer) {
48  Logger::writeToLog("Writing buffer to " + filePath);
49  std::ofstream file(filePath.c_str(), std::ios::binary);
50  file.write((char*)&buffer[0], buffer.size());
51  Logger::writeToLog("Wrote buffer");
52  }
53  void IOManager::writeFloatLE(float val, std::ofstream& file) {
54  char bytes[4];
55  for (int i = 0; i < 4; i++) {
56  bytes[i] = ((char*)&val)[i];
57  }
58  file.write(bytes, 4);
59  }
60  void IOManager::writeFloatLE(float val, FileBuffer& buffer) {
61  char bytes[4];
62  for (int i = 0; i < 4; i++) {
63  bytes[i] = ((char*)&val)[i];
64  }
65  buffer.insert(buffer.end(), bytes, bytes+4);
66  }
67  float IOManager::readFloatLE(std::ifstream& file) {
68  char bytes[4];
69  file.read(bytes, 4);
70  float ret;
71  for (int i = 0; i < 4; i++) {
72  ((char*)&ret)[i] = bytes[i];
73  }
74  return ret;
75  }
76  float IOManager::readFloatLE(const FileBuffer& buffer, int pos) {
77  float ret;
78  for (int i = 0; i < 4; i++) {
79  ((char*)&ret)[i] = buffer[i+pos];
80  }
81  return ret;
82  }
83  void IOManager::writeIntLE(int val, std::ofstream& file) {
84  char bytes[4];
85  for (int i = 0; i < 4; i++) {
86  bytes[i] = (val >> (8*i)) & 0xFF;
87  }
88  file.write(bytes, 4);
89  }
90  void IOManager::writeIntLE(int val, FileBuffer& buffer) {
91  char bytes[4];
92  for (int i = 0; i < 4; i++) {
93  bytes[i] = (val >> (8*i)) & 0xFF;
94  }
95  buffer.insert(buffer.end(), bytes, bytes+4);
96  }
97  int IOManager::readIntLE(std::ifstream& file) {
98  char bytes[4];
99  file.read(bytes, 4);
100  int ret = 0;
101  for (int i = 0; i < 4; i++) {
102  ret |= bytes[i] << (i*8);
103  }
104  return ret;
105  }
106  int IOManager::readIntLE(const FileBuffer& buffer, int pos) {
107  int ret = 0;
108  for (int i = 0; i < 4; i++) {
109  ret |= buffer[pos+i] << (i*8);
110  }
111  return ret;
112  }
113  int IOManager::readIntBE(std::ifstream& file) {
114  char bytes[4];
115  file.read(bytes, 4);
116  int ret = 0;
117  for (int i = 0; i < 4; i++) {
118  ret |= bytes[i] << ((3-i)*8);
119  }
120  return ret;
121  }
122  void IOManager::writeShortLE(short val, std::ofstream& file) {
123  char bytes[2];
124  for (int i = 0; i < 2; i++) {
125  bytes[i] = (val >> (8*i)) & 0xFF;
126  }
127  file.write(bytes, 2);
128  }
129  short IOManager::readShortLE(std::ifstream& file) {
130  char bytes[2];
131  file.read(bytes, 2);
132  return bytes[0] | (bytes[1] << 8);
133  }
134 }
nta::IOManager::readFileToBuffer
static void readFileToBuffer(crstring filePath, FileBuffer &buffer)
stores the entire contents of a file in a buffer
Definition: IOManager.cpp:8
nta::Logger::writeToLog
static void writeToLog(crstring entry)
writes an entry in the log
Definition: Logger.cpp:17
nta::IOManager::writeIntLE
static void writeIntLE(int val, std::ofstream &file)
writes/reads an int to/from a file
Definition: IOManager.cpp:83
nta::IOManager::fileExists
static bool fileExists(crstring path)
Checks to see if a file exists.
Definition: IOManager.cpp:5
nta::IOManager::writeFloatLE
static void writeFloatLE(float val, std::ofstream &file)
writes/reads a float to/from a file
Definition: IOManager.cpp:53
nta::IOManager::writeFileFromBuffer
static void writeFileFromBuffer(crstring filePath, const FileBuffer &buffer)
stores the entire contents of a buffer in a file
Definition: IOManager.cpp:41
nta
Definition: Animation2D.h:6
nta::Logger::writeErrorToLog
static Error writeErrorToLog(crstring error, ErrorType type=OTHER)
writes entry in log and then notifies ErrorManager
Definition: Logger.cpp:31
nta::IOManager::writeShortLE
static void writeShortLE(short val, std::ofstream &file)
writes/reads a short to/from a file
Definition: IOManager.cpp:122