-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonImporter.cpp
65 lines (43 loc) · 1.39 KB
/
JsonImporter.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
//
// Created by Matthieu Rudelle on 26/05/16.
//
#include "JsonImporter.h"
#include <fstream>
#include <string>
#include <regex>
#include <math.h>
using namespace std;
#define MAX_BUFFER_LENGTH 1000
//TODO: allow for space between objects
const regex FEATURE_REGEX("\\{\"feature\":\"((?:[^\"]|\\\\\")+)\",\"file\":\"([^\"]+)\",\"line\":\\{\"\\$numberInt\":\"(\\d+)\"\\},\"repoRank\":([\\d\\.]+)\\}");
const float log25567 = log(25567);
JsonImporter::JsonImporter(string filename) {
// TODO: file does not exist + IOException
// open file for reading
input.open(filename);
buffer = new char[MAX_BUFFER_LENGTH];
}
bool JsonImporter::readFeature(string *feature, string *file, float *reporank, int *line) {
if (input.eof()) {
input.close();
return false;
}
input.getline(buffer, MAX_BUFFER_LENGTH);
string featLine(buffer);
smatch matches;
bool match = regex_search(featLine, matches, FEATURE_REGEX);
if (!match) {
*feature = "dummy";
*file = "dummy";
*line = 0;
*reporank = 0.0;
printf("Line not parseable: %s\n", featLine.c_str());
// even if we can't match it we return true as it's not a EOF
return true;
}
*feature = matches.str(1);
*file = matches.str(2);
*line = stoi(matches.str(3));
*reporank = (float) log(stof(matches.str(4)) + 1) / log25567;
return true;
}