-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtkread.cpp
79 lines (65 loc) · 1.65 KB
/
gtkread.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
69
70
71
72
73
74
75
76
77
78
79
#include "gtkread.h"
#include <fstream>
#include <cassert>
#include <iostream>
gtkread::gtkread(char *filename) {
m_filename = filename;
}
gtkread::~gtkread() {
}
void gtkread::file_load() {
assert(m_filename.length() > 0);
#ifdef _MSC_VER
FILE *file = nullptr;
fopen_s(&file, m_filename.c_str(), "rb");
#else
FILE *file = fopen(m_filename.c_str(), "rb");
#endif
if (file == nullptr)
return;
fseek(file, 0, SEEK_END);
size_t file_size = ftell(file);
fseek(file, 0, SEEK_SET);
char *buf = new char[file_size];
fread(buf, 1, file_size, file);
fclose(file);
size_t start = 0;
size_t curr;
for (curr = 0; curr < file_size; curr++) {
if (buf[curr] == '\x0d' || buf[curr] == '\x0a') {
if (start != ~0 && curr - start > 0) {
m_file_contents.push_back(std::string(buf + start, curr - start));
}
start = ~0;
} else if (start == ~0) {
start = curr;
}
}
if (start != ~0) {
m_file_contents.push_back(std::string(buf + start, curr - start));
}
delete[] buf;
}
size_t gtkread::size() {
return m_file_contents.size();
}
void gtkread::parse_nodes() {
size_t nodeBegin = 0;
size_t headerIndex = ~0;
for (size_t i=0; i < m_file_contents.size(); i++) {
std::string line = m_file_contents[i];
if (line.find('{') != std::string::npos) {
nodeBegin = i+1;
if (i > 0 && m_file_contents[i-1].substr(0, 2) == "//") {
headerIndex = i-1;
}
continue;
}
if (line.find('}') != std::string::npos) {
m_nodes.push_back(node(m_file_contents, headerIndex, nodeBegin, i-1));
}
}
}
std::vector<node>& gtkread::get_nodes() {
return m_nodes;
}