forked from VolosR/TTGOWeatherStation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs_operations.cpp
74 lines (57 loc) · 1.48 KB
/
fs_operations.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
#include "fs_operations.h"
bool writeFile(String path, String data, const bool binary, const bool overwrite) {
char *mode = FILE_WRITE;
bool create = true;
if (!LittleFS.begin(true))
return false;
if (LittleFS.exists(path)) {
create = false;
if (!overwrite)
mode = FILE_APPEND;
}
File file = LittleFS.open(path, mode, create);
if (!binary && !data.endsWith("\n"))
data += '\n';
create = file.print(data) == data.length();
file.flush();
file.close();
return create;
}
String readFile(String path, const bool binary) {
File file;
String data;
if (!LittleFS.begin(true))
return "";
if (!(file = LittleFS.open(path)))
return "";
if (binary) {
bool success = false;
char* buf;
size_t size;
buf = (char*)malloc(file.size());
size = file.readBytes(buf, file.size());
success = size == file.size();
if (success)
data.concat(buf, size);
free(buf);
}
else
data = file.readString();
file.close();
return data;
}
bool findFile(String path) {
if (!LittleFS.begin(true))
return false;
return LittleFS.exists(path);
}
bool deleteFile(String path){
if (!LittleFS.begin(true))
return false;
return LittleFS.remove(path);
}
bool renameFile(String from, String to) {
if (!LittleFS.begin(true))
return false;
return LittleFS.rename(from, to);
}