-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.cpp
33 lines (31 loc) · 1.01 KB
/
common.cpp
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
#include "common.h"
std::string locateFile(const std::string& input, const std::vector<std::string> & directories)
{
std::string file;
const int MAX_DEPTH{10};
bool found{false};
for (auto &dir : directories)
{
file = dir + input;
for (int i = 0; i < MAX_DEPTH && !found; i++)
{
std::ifstream checkFile(file);
found = checkFile.is_open();
if (found) break;
file = "../" + file;
}
if (found) break;
file.clear();
}
assert(!file.empty() && "Could not find a file due to it not existing in the data directory.");
return file;
}
void readPGMFile(const std::string& fileName, uint8_t *buffer, int inH, int inW)
{
std::ifstream infile(fileName, std::ifstream::binary);
assert(infile.is_open() && "Attempting to read from a file that is not open.");
std::string magic, h, w, max;
infile >> magic >> h >> w >> max;
infile.seekg(1, infile.cur);
infile.read(reinterpret_cast<char*>(buffer), inH*inW);
}