BARE2D
IOManager_impl.tcc
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Logger.hpp"
4 
5 namespace BARE2D {
6 
7  template<typename T>
8  bool IOManager::readFileToBuffer(std::string& filepath, std::vector<T>& buf, std::ios_base::openmode mode/** = std::ios::binary*/) {
9  std::ifstream file(filepath, mode);
10 
11  if(file.fail()) {
12  Logger::getInstance()->log("Failed to load file to buffer: " + filepath);
13  return false;
14  }
15 
16  // Seek to the end to get the file size
17  file.seekg(0, std::ios::end);
18  unsigned int fileSize = (unsigned int)file.tellg();
19 
20  // Go back to the start
21  file.seekg(0, std::ios::beg);
22  fileSize -= file.tellg();
23 
24  // Now read fileSize bytes
25  buf.resize(fileSize);
26  file.read((char*)&(buf[0]), fileSize);
27 
28  // Close it up and return! Job done.
29  file.close();
30 
31  return true;
32  }
33 
34 }