-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cpp
More file actions
65 lines (53 loc) · 1.27 KB
/
file.cpp
File metadata and controls
65 lines (53 loc) · 1.27 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
58
59
60
61
62
63
64
65
#include "file.hpp"
#include <map>
#include <fstream>
#include <iostream>
namespace Utility {
static std::map<int, uint8_t> _map {
{ '0', 0x00 },
{ '1', 0x01 },
{ '2', 0x02 },
{ '3', 0x03 },
{ '4', 0x04 },
{ '5', 0x05 },
{ '6', 0x06 },
{ '7', 0x07 },
{ '8', 0x08 },
{ '9', 0x09 },
{ 'a', 0x0A },
{ 'b', 0x0B },
{ 'c', 0x0C },
{ 'd', 0x0D },
{ 'e', 0x0E },
{ 'f', 0x0F }
};
std::vector<std::vector<uint8_t>> ReadKnownFile(const char *filename, const int linesz, const int nlines) {
std::ifstream file(filename, std::ifstream::in);
std::vector<std::vector<uint8_t>> lines;
lines.reserve(nlines);
for (auto i = 0; i < nlines && file.good(); i++) {
std::vector<uint8_t> v;
uint8_t byte;
do {
auto ch = file.get();
if (file.eof()) {
break;
}
if (ch == '\n') {
break;
}
auto ch2 = file.get();
if (file.eof()) {
break;
}
if (ch2 == '\n') {
break;
}
byte = (_map[ch] << 4) | _map[ch2];
v.push_back(byte);
} while (true);
lines.push_back(v);
}
return lines;
}
}