-
Notifications
You must be signed in to change notification settings - Fork 0
/
refParser.cpp
78 lines (55 loc) · 1.82 KB
/
refParser.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
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
66
67
68
// this file implements a parser for reading in user supplied reference
// hash data to compare against.
//
// (C) Markus Dittrich, 2015
#include <iostream>
#include <fstream>
#include <vector>
#include "refParser.hpp"
using VecString = std::vector<std::string>;
static bool insert_line(const std::string& line, ReferenceMap& info);
static VecString split(const std::string& s, const std::string& delim);
// load_reference_data parses a reference data set at filePath expected to be
// in phantom style output format. It returns an unordered map from filepaths
// to hash values
ReferenceMap load_reference_data(const std::string& filePath) {
std::ifstream refFile(filePath);
if (!refFile) {
return ReferenceMap();
}
std::string line;
ReferenceMap outMap;
while (getline(refFile, line)) {
if (!insert_line(line, outMap)) {
return ReferenceMap();
}
}
return outMap;
}
// insert_line parses a single line of a reference hash file and inserts it into
// the reference map data structure. The line is expected to be in csv format
// of the form: <hash type>, <file path>, <file hash>
bool insert_line(const std::string& line, ReferenceMap& map) {
auto result = split(line, ", ");
if (result.size() != 3) {
return false;
}
map[result[1]] = result[2];
return true;
}
// split is a helper function splitting string s into substring at each instance
// of any character contained in the delimiter string
VecString split(const std::string& s, const std::string& delim) {
size_t j = 0;
std::vector<std::string> out;
while ((j = s.find_first_not_of(delim, j)) != std::string::npos) {
size_t i = s.find_first_of(delim, j);
if (i == std::string::npos) {
out.push_back(s.substr(j, s.length()-j));
break;
}
out.push_back(s.substr(j,i-j));
j = i;
}
return out;
}