-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.cpp
66 lines (60 loc) · 1.88 KB
/
parse.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
#include <iostream>
#include <fstream>
#include <json/json.h>
#include <jsoncpp.cpp>
#include "bin.h"
#include "parse.h"
using namespace std;
obj *host_objs;
uint32_t host_num_objs;
uint32_t host_bin_size;
uint32_t host_total_obj_size = 0; //pseudo-constant
bool parse(char *infile) {
ifstream f(infile, std::ifstream::binary);
if (f.fail()) {
return false;
}
Json::Value obj_data;
f >> obj_data;
host_bin_size = obj_data["bin_size"].asUInt();
host_num_objs = obj_data["num_objs"].asUInt();
// Initialize object array and put one obj in each bin
host_objs = new obj[host_num_objs];
auto obj_array = obj_data["objs"];
for(uint32_t i = 0; i < host_num_objs; i++){
#ifdef TAGGING
host_objs[i].tag = i;
#endif
host_objs[i].size = obj_array[i].asUInt();
host_total_obj_size += obj_array[i].asUInt();
}
return true;
}
bool dump(char *outfile) {
Json::Value obj_data;
obj_data["bin_size"] = host_bin_size;
obj_data["num_objs"] = host_num_objs;
obj_data["num_bins"] = host_num_bins;
obj_data["objs"] = Json::Value(Json::arrayValue);
obj_data["bins"] = Json::Value(Json::arrayValue);
for(uint32_t i = 0; i < host_num_objs; i++){
obj_data["objs"][i] = host_objs[i].size;
}
for(uint32_t i = 0; i < (uint32_t) host_num_bins; i++) {
obj_data["bins"][i] = Json::Value(Json::arrayValue);
for (uint32_t j = 0; j < bins_out[i].obj_list.size(); j++) {
obj_data["bins"][i][j] = bins_out[i].obj_list[j].size;
}
}
if (outfile==NULL) { //print results to stdout
cout << "num_objs: " << host_num_objs << endl;
cout << "num_bins: " << host_num_bins << endl;
} else { //print to file
filebuf fb;
fb.open(outfile, ios::out);
ostream f(&fb);
f << obj_data;
}
delete[] host_objs;
return true;
}