-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.hpp
More file actions
176 lines (159 loc) · 4.98 KB
/
Copy pathtasks.hpp
File metadata and controls
176 lines (159 loc) · 4.98 KB
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
#pragma once
#include <algorithm>
#include <cctype>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
struct Task {
int id;
std::string text;
bool done;
};
inline const std::string kTasksFile = "tasks.json";
inline std::string escapeJson(const std::string& s) {
std::string out;
for (char c : s) {
switch (c) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\t': out += "\\t"; break;
default: out += c;
}
}
return out;
}
// Minimal parser tailored to the array-of-{id,text,done} format this program writes.
class JsonParser {
public:
explicit JsonParser(const std::string& s) : s_(s), pos_(0) {}
std::vector<Task> parseTasks() {
std::vector<Task> tasks;
skipWs();
if (pos_ >= s_.size() || s_[pos_] != '[') return tasks;
pos_++;
skipWs();
if (pos_ < s_.size() && s_[pos_] == ']') { pos_++; return tasks; }
while (true) {
skipWs();
tasks.push_back(parseTask());
skipWs();
if (pos_ < s_.size() && s_[pos_] == ',') { pos_++; continue; }
break;
}
skipWs();
if (pos_ < s_.size() && s_[pos_] == ']') pos_++;
return tasks;
}
private:
const std::string& s_;
size_t pos_;
void skipWs() {
while (pos_ < s_.size() && std::isspace(static_cast<unsigned char>(s_[pos_]))) pos_++;
}
void expect(char c) {
if (pos_ < s_.size() && s_[pos_] == c) pos_++;
}
Task parseTask() {
Task t{0, "", false};
expect('{');
skipWs();
if (pos_ < s_.size() && s_[pos_] == '}') { pos_++; return t; }
while (true) {
skipWs();
std::string key = parseString();
skipWs();
expect(':');
skipWs();
if (key == "id") t.id = parseInt();
else if (key == "text") t.text = parseString();
else if (key == "done") t.done = parseBool();
else skipValue();
skipWs();
if (pos_ < s_.size() && s_[pos_] == ',') { pos_++; continue; }
break;
}
skipWs();
expect('}');
return t;
}
std::string parseString() {
std::string out;
if (pos_ >= s_.size() || s_[pos_] != '"') return out;
pos_++;
while (pos_ < s_.size() && s_[pos_] != '"') {
char c = s_[pos_];
if (c == '\\' && pos_ + 1 < s_.size()) {
pos_++;
char e = s_[pos_];
switch (e) {
case 'n': out += '\n'; break;
case 't': out += '\t'; break;
case '"': out += '"'; break;
case '\\': out += '\\'; break;
default: out += e;
}
} else {
out += c;
}
pos_++;
}
if (pos_ < s_.size()) pos_++;
return out;
}
int parseInt() {
size_t start = pos_;
if (pos_ < s_.size() && (s_[pos_] == '-' || s_[pos_] == '+')) pos_++;
while (pos_ < s_.size() && std::isdigit(static_cast<unsigned char>(s_[pos_]))) pos_++;
if (start == pos_) return 0;
return std::stoi(s_.substr(start, pos_ - start));
}
bool parseBool() {
if (s_.compare(pos_, 4, "true") == 0) { pos_ += 4; return true; }
if (s_.compare(pos_, 5, "false") == 0) { pos_ += 5; return false; }
return false;
}
void skipValue() {
skipWs();
if (pos_ >= s_.size()) return;
char c = s_[pos_];
if (c == '"') {
parseString();
} else if (c == '{' || c == '[') {
char open = c, close = (c == '{') ? '}' : ']';
int depth = 0;
do {
if (s_[pos_] == open) depth++;
else if (s_[pos_] == close) depth--;
pos_++;
} while (pos_ < s_.size() && depth > 0);
} else {
while (pos_ < s_.size() && s_[pos_] != ',' && s_[pos_] != '}' && s_[pos_] != ']') pos_++;
}
}
};
inline std::vector<Task> loadTasks(const std::string& path) {
std::ifstream in(path);
if (!in) return {};
std::stringstream buffer;
buffer << in.rdbuf();
return JsonParser(buffer.str()).parseTasks();
}
inline void saveTasks(const std::string& path, const std::vector<Task>& tasks) {
std::ofstream out(path);
out << "[\n";
for (size_t i = 0; i < tasks.size(); i++) {
const Task& t = tasks[i];
out << " {\"id\": " << t.id << ", \"text\": \"" << escapeJson(t.text)
<< "\", \"done\": " << (t.done ? "true" : "false") << "}";
if (i + 1 < tasks.size()) out << ",";
out << "\n";
}
out << "]\n";
}
inline int nextId(const std::vector<Task>& tasks) {
int maxId = 0;
for (const auto& t : tasks) maxId = std::max(maxId, t.id);
return maxId + 1;
}