-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLbaHelper.hpp
41 lines (33 loc) · 1.08 KB
/
LbaHelper.hpp
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
#ifndef SIMPLE_GPT_READER_LBAHELPER_HPP
#define SIMPLE_GPT_READER_LBAHELPER_HPP
#include <string>
#include <fstream>
#include <iostream>
class LbaHelper {
public:
explicit LbaHelper(std::string& devicePath, uint blockSize);
~LbaHelper();
template<typename T> T readFromLba(uint64_t firstLba) {
return readFromLba<T>(firstLba, sizeof(T));
}
template<typename T> T readFromLba(uint64_t firstLba, size_t size) {
deviceInputStream.seekg(firstLba * this->blockSize);
return readFromCurrentPosition<T>(size);
}
template<typename T> T readFromCurrentPosition() {
return readFromCurrentPosition<T>(sizeof(T));
}
template<typename T> T readFromCurrentPosition(size_t size) {
char buffer[size];
if (deviceInputStream.read(buffer, size)) {
return *(T*) &buffer;
} else {
std::cerr << "Could not readFromLba data into buffer." << std::endl;
exit(EXIT_FAILURE);
}
}
private:
std::ifstream deviceInputStream;
uint blockSize;
};
#endif //SIMPLE_GPT_READER_LBAHELPER_HPP