-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilereader.cpp
207 lines (181 loc) · 4.98 KB
/
filereader.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <wx/vector.h>
#include <regex>
#include <wx/string.h>
#include <fstream>
#include <streambuf>
#include <sstream>
#include <map>
#include "filereader.h"
const std::map<wxString, long> FileReader::defines = {
{"WAVE_SINE", 0},
{"WAVE_SAWTOOTH", 1},
{"WAVE_TRIANGLE", 2},
{"WAVE_SQUARE_25", 3},
{"WAVE_SQUARE_50", 4},
{"WAVE_SQUARE_75", 5},
{"WAVE_FUZZY_SINE1", 6},
{"WAVE_FUZZY_SINE2", 7},
{"WAVE_FUZZY_SINE3", 8},
{"WAVE_FILTERED_SQUARE", 9},
{"WSIN", 0},
{"WSAW", 1},
{"WTRI", 2},
{"WS25", 3},
{"WS50", 4},
{"WS75", 5},
{"WFS1", 6},
{"WFS2", 7},
{"WFS3", 8},
{"WFSQ", 9},
{"PC_ENV_SPEED", 0},
{"PC_NOISE_PARAMS", 1},
{"PC_WAVE", 2},
{"PC_NOTE_UP", 3},
{"PC_NOTE_DOWN", 4},
{"PC_NOTE_CUT", 5},
{"PC_NOTE_HOLD", 6},
{"PC_ENV_VOL", 7},
{"PC_PITCH", 8},
{"PC_TREMOLO_LEVEL", 9},
{"PC_TREMOLO_RATE", 10},
{"PC_SLIDE", 11},
{"PC_SLIDE_SPEED", 12},
{"PC_LOOP_START", 13},
{"PC_LOOP_END", 14},
{"PATCH_END", 15},
};
const std::regex FileReader::multiline_comments("/\\*(.|[\r\n])*?\\*/");
const std::regex FileReader::singleline_comments("//.*");
const std::regex FileReader::white_space("[\t\n\r]");
const std::regex FileReader::extra_space(" +");
const std::regex FileReader::patch_declaration(
"const char ([a-zA-Z_][a-zA-Z_\\d]*)\\[\\] PROGMEM ?= ?");
const std::regex FileReader::struct_declaration(
"const struct PatchStruct ([a-zA-Z_][a-zA-Z_\\d]*)\\[\\] PROGMEM ?= ?");
long FileReader::string_to_long(const wxString &str) {
if (defines.find(str) != defines.end())
return defines.find(str)->second;
return strtol(str.c_str(), NULL, 0);
}
bool FileReader::read_patch_vals(const wxString &str, wxVector<long> &vals) {
int depth = 0;
std::string vstr;
for (auto c : str) {
if (c == '{') {
depth++;
}
else if (c == '}') {
if (!depth) {
return false;
}
else if (!(--depth)) {
break;
}
}
else if (c != ' ' && depth == 1) {
vstr += c;
}
}
std::stringstream ss(vstr);
std::string item;
vals.clear();
while (std::getline(ss, item, ',')) {
if (!item.empty()) {
vals.push_back(string_to_long(item));
}
}
return !vals.empty();
}
bool FileReader::read_struct_vals(const wxString &str,
wxVector<wxString> &vals) {
int depth = 0;
std::string vstr;
for (auto c : str) {
if (c == '{') {
depth++;
}
else if (c == '}') {
if (!depth) {
return false;
}
else if (!(--depth)) {
break;
}
else {
vstr += ',';
}
}
else if (c != ' ' && depth == 2) {
vstr += c;
}
}
std::stringstream ss(vstr);
std::string item;
vals.clear();
while (std::getline(ss, item, ',')) {
vals.push_back(item);
}
return !vals.empty();
}
std::string FileReader::clean_code(const std::string &code) {
std::string clean_code, t;
/* Remove comments and unecessary white space */
std::regex_replace(std::back_inserter(clean_code), code.begin(), code.end(),
multiline_comments, "");
clean_code.swap(t);
clean_code.clear();
std::regex_replace(std::back_inserter(clean_code), t.begin(), t.end(),
singleline_comments, "");
clean_code.swap(t);
clean_code.clear();
std::regex_replace(std::back_inserter(clean_code), t.begin(), t.end(),
white_space, " ");
clean_code.swap(t);
clean_code.clear();
std::regex_replace(std::back_inserter(clean_code), t.begin(), t.end(),
extra_space, " ");
return clean_code;
}
bool FileReader::read_patches(const std::string &clean_src,
std::multimap<wxString, wxVector<long>> &data) {
std::smatch match;
auto search_start = clean_src.cbegin();
data.clear();
while (std::regex_search(search_start, clean_src.cend(),
match, patch_declaration)) {
search_start += match.position() + match.length();
wxVector<long> vals;
std::string varea(search_start, clean_src.cend());
if (!read_patch_vals(varea, vals))
return false;
data.emplace(match[1].str(), vals);
}
return true;
}
bool FileReader::read_structs(const std::string &clean_src,
std::multimap<wxString, wxVector<wxString>> &data) {
std::smatch match;
auto search_start = clean_src.cbegin();
data.clear();
while (std::regex_search(search_start, clean_src.cend(),
match, struct_declaration)) {
search_start += match.position() + match.length();
wxVector<wxString> vals;
std::string varea(search_start, clean_src.cend());
if (!read_struct_vals(varea, vals))
return false;
data.emplace(match[1].str(), vals);
}
return true;
}
bool FileReader::read_patches_and_structs(const wxString &fn,
std::multimap<wxString, wxVector<long>> &patches,
std::multimap<wxString, wxVector<wxString>> &structs) {
std::ifstream f(fn);
if (!f.is_open())
return false;
std::string src((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
std::string clean_src = clean_code(src);
return read_patches(clean_src, patches) && read_structs(clean_src, structs);
}