-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.cpp
210 lines (195 loc) · 5.7 KB
/
utilities.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
208
209
210
#include "utilities.h"
#include <fstream>
using json = nlohmann::json;
namespace fs = std::filesystem;
enum StripMode { structure, quote, commentSingle, commentMulti };
/**
* Strip JSON comments from strings.
*
* @param text The text to remove JSON comments from.
*/
void stripJsonComments(std::string & text) {
StripMode mode = structure;
int commentStart, commentLength;
char prev = 'U', current = 'U';
for(int i = 0; i < text.length(); ++i) {
prev = current;
current = text[i];
switch(mode) {
case structure:
if(current == '"') {
mode = quote;
} else if(prev == '/') {
if(current == '/') {
mode = commentSingle;
text[i - 1] = ' ';
text[i] = ' ';
} else if(current == '*') {
mode = commentMulti;
text[i - 1] = ' ';
text[i] = ' ';
}
}
break;
case quote:
if(prev != '\\' && current == '"') {
mode = structure;
}
break;
case commentSingle:
if(current == '\n') {
mode = structure;
} else {
text[i] = ' ';
}
break;
case commentMulti:
if (prev == '*' && current == '/') {
mode = structure;
}
if(current != '\n') {
text[i] = ' ';
}
break;
}
}
}
/**
* Converts newlines in JSON values to breakout newlines.
*
* @param text The text to convert newlines in.
*/
void convertJsonValueNewlinesToBreakout(std::string & text) {
StripMode mode = structure;
char prev = 'U', current = 'U';
for (int i = 0; i < text.size(); i++) {
prev = current;
current = text[i];
switch (mode) {
case structure:
if (current == '"') {
mode = quote;
}
break;
case quote:
//Check if at end of quote.
if (prev != '\\' && current == '"') {
mode = structure;
} else if (current == '\n') {
text.erase(i, 1);
text.insert(i, "\\n");
}
}
}
}
/**
* Converts breakout newlines in JSON values to newlines.
*
* @param text The text to convert breakout newlines in.
*/
void convertNewlineBreakoutsToNewline(std::string & text) {
char prev = 'U', current = 'U';
for (int i = 0; i < text.size(); i++) {
prev = current;
current = text[i];
if (prev == '\\' && current == 'n') {
text.erase(i - 1, 2);
text.insert(i - 1, "\n");
prev = 'U';
current = text[i];
i--;
}
}
}
/**
* Converts quotes in JSON values to breakout quotes.
*
* @param text The text to convert quotes in.
*/
void convertQuoteToBreakoutQuote(std::string & text) {
char prev = 'U', current = 'U';
for (int i = 0; i < text.size(); i++) {
prev = current;
current = text[i];
if (prev != '\\' && current == '\"') {
text.insert(i, "\\");
current = '\\';
}
}
}
/**
* Converts breakout newlines in values to newlines".
*
* @param text The text to have a character replaced in.
* @param x The character to replace.
* @param replacement The string to replace the character with.
* @return The position of the character replaced, -1 if none.
*/
int replaceFirstOfX(std::string & text, const char x, const std::string replacement) {
for (int i = 0; i < text.size(); i++) {
if (text[i] == x) {
text.erase(i, 1);
text.insert(i, replacement);
return i;
}
}
return -1;
}
/**
* Retrieves a Text file from the path.
*
* @param filePath The path the file is at.
* @return The file document as std::string.
*/
const std::string fetchText(fs::path filePath) {
std::ifstream textFile;
std::stringstream textStream;
//TODO: Handle read failures more gracefully.
textFile.open(filePath);
textStream << textFile.rdbuf();
textFile.close();
return textStream.str();
}
/**
* Retrieves a JSON file from the path.
*
* @param filePath The path the file is at.
* @param valuesHaveNewlines If values have actual newlines in them.
* @return The JSON file as nlohmann::json.
*/
const json fetchJson(fs::path filePath, bool valuesHaveNewlines) {
std::string jsonString = fetchText(filePath);
stripJsonComments(jsonString);
if (valuesHaveNewlines) {
convertJsonValueNewlinesToBreakout(jsonString);
}
//TODO: Handle conversion failures more gracefully.
return json::parse(jsonString);
}
/**
* Retrieves a JSON file from the path.
*
* @param filePath The path the file is at.
* @return The JSON file as nlohmann::json.
*/
const json fetchJson(fs::path filePath) {
return fetchJson(filePath, false);
}
/**
* Writes a string steam to a specific path..
*
* @param stream The string stream to write.
* @param filePath The path the file should be written to.
* @return If the file was written.
*/
const bool writeStringStreamToPath(std::stringstream & stream, std::filesystem::path filePath) {
if (fs::exists(filePath.parent_path()) || fs::create_directories(filePath.parent_path())) {
std::ofstream textFile;
//TODO: Handle write failures more gracefully.
textFile.open(filePath);
textFile << stream.rdbuf();
textFile.close();
return true;
}
return false;
}