-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProperties.cpp
More file actions
70 lines (59 loc) · 1.78 KB
/
Properties.cpp
File metadata and controls
70 lines (59 loc) · 1.78 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
#include <Arduino.h>
#include <FS.h>
#include "Properties.h"
bool getValue(String path, String key, String &value) {
File file = SPIFFS.open(path, "r");
if (file) {
String line;
do {
line = file.readStringUntil('\n');
if (line.length() > 0) {
if (line.indexOf('=') > 0) {
if (line.substring(0, line.indexOf('=')).equals(key)) {
value = line.substring(line.indexOf('=') + 1,
line.length());
file.close();
return true;
}
}
}
} while (line.length() > 0);
file.close();
}
return false;
}
bool setValue(String path, String key, String value) {
File original = SPIFFS.open(path, "r");
File config = SPIFFS.open(path + "~", "w+");
if (!config) {
original.close();
return false;
}
bool found = false;
if (original) {
String line;
do {
line = original.readStringUntil('\n');
if (line.length() > 0) {
if (line.indexOf('=') > 0) {
if (line.substring(0, line.indexOf('=')).equals(key)) {
config.printf("%s=%s\n", key.c_str(), value.c_str());
found = true;
} else {
config.printf("%s\n", line.c_str());
}
}
}
} while (line.length() > 0);
}
if (!found) {
config.printf("%s=%s\n", key.c_str(), value.c_str());
}
if (original) {
original.close();
SPIFFS.remove(path);
}
config.close();
SPIFFS.rename(path + "~", path);
return true;
}