-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileBuffer.hpp
More file actions
57 lines (46 loc) · 1.18 KB
/
Copy pathFileBuffer.hpp
File metadata and controls
57 lines (46 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef __FILEBUFFER_HPP__
#define __FILEBUFFER_HPP__
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
class FileBuffer
{
public:
FileBuffer() = default;
FileBuffer(const std::string& path)
{
load(path);
}
bool load(const std::string& path)
{
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file.is_open())
return false;
m_path = path;
m_size = file.tellg();
file.seekg(0, std::ios::beg);
m_data.resize(m_size);
file.read(m_data.data(), m_size);
file.close();
return true;
}
const char* data() const { return m_data.data(); }
std::size_t size() const { return m_size; }
const std::string& path() const { return m_path; }
bool empty() const { return m_size == 0; }
std::string toString() const
{
return std::string(m_data.data(), m_size);
}
void ds_info() const
{
std::cout << "file: " << m_path
<< ", size: " << m_size << " bytes\n";
}
private:
std::string m_path;
std::vector<char> m_data;
std::size_t m_size = 0;
};
#endif // __FILEBUFFER_HPP__