Files
IoTManager/src/Utils/FileUtils.cpp
Dmitry Borisenko 5eb3c6d3a3 first
2021-01-04 00:39:35 +01:00

183 lines
4.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "Utils/FileUtils.h"
#include "FileSystem.h"
#include "Utils/StringUtils.h"
#include "Utils\SerialPrint.h"
const String filepath(const String& filename) {
return filename.startsWith("/") ? filename : "/" + filename;
}
bool fileSystemInit() {
if (!FileFS.begin()) {
SerialPrint("E", F("FS"), F("FS Init ERROR, may be FS was not flashed"));
return false;
}
SerialPrint("I", F("FS"), F("FS Init completed"));
return true;
}
void removeFile(const String& filename) {
String path = filepath(filename);
if (FileFS.exists(path)) {
if (!FileFS.remove(path)) {
SerialPrint("I", "Files", "remove " + path);
}
} else {
SerialPrint("E", "Files", "not exist" + path);
}
}
File seekFile(const String& filename, size_t position) {
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
SerialPrint("[E]", "Files", "open " + path);
}
// поставим курсор в начало файла
file.seek(position, SeekSet);
return file;
}
const String readFileString(const String& filename, const String& to_find) {
String path = filepath(filename);
String res = "failed";
auto file = FileFS.open(path, "r");
if (!file) {
return "failed";
}
if (file.find(to_find.c_str())) {
res = file.readStringUntil('\n');
}
file.close();
return res;
}
const String addFileLn(const String& filename, const String& str) {
String path = filepath(filename);
auto file = FileFS.open(path, "a");
if (!file) {
return "failed";
}
file.println(str);
file.close();
return "sucсess";
}
const String addFile(const String& filename, const String& str) {
String path = filepath(filename);
auto file = FileFS.open(path, "a");
if (!file) {
return "failed";
}
file.print(str);
file.close();
return "sucсess";
}
bool copyFile(const String& src, const String& dst, bool overwrite) {
String srcPath = filepath(src);
String dstPath = filepath(dst);
SerialPrint("I", "Files", "copy " + srcPath + " to " + dstPath);
if (!FileFS.exists(srcPath)) {
SerialPrint("[E]", "Files", "not exist: " + srcPath);
return false;
}
if (FileFS.exists(dstPath)) {
if (!overwrite) {
SerialPrint("[E]", "Files", "already exist: " + dstPath);
return false;
}
FileFS.remove(dstPath);
}
auto srcFile = FileFS.open(srcPath, "r");
auto dstFile = FileFS.open(dstPath, "w");
uint8_t buf[512];
while (srcFile.available()) {
size_t len = srcFile.read(buf, 512);
dstFile.write(buf, len);
}
srcFile.close();
dstFile.close();
return true;
}
const String writeFile(const String& filename, const String& str) {
String path = filepath(filename);
auto file = FileFS.open(path, "w");
if (!file) {
return "failed";
}
file.print(str);
file.close();
return "sucсess";
}
const String readFile(const String& filename, size_t max_size) {
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
return "failed";
}
size_t size = file.size();
if (size > max_size) {
file.close();
return "large";
}
String temp = file.readString();
file.close();
return temp;
}
size_t countLines(const String filename) {
size_t cnt = -1;
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
return cnt;
}
file.seek(0, SeekSet);
size_t size = file.size();
size_t psn;
do {
cnt++;
file.readStringUntil('\n');
psn = file.position();
} while (psn < size);
file.close();
return cnt;
}
size_t getFileSize(const String filename) {
size_t size = -1;
String filepath(filename);
auto file = FileFS.open(filepath, "r");
if (!file) {
return size;
}
size = file.size();
file.close();
return size;
}
const String getFSSizeInfo() {
String res;
#ifdef ESP8266
FSInfo info;
if (FileFS.info(info)) {
res = prettyBytes(info.usedBytes) + " of " + prettyBytes(info.totalBytes);
} else {
res = "error";
}
#else
res = prettyBytes(FileFS.usedBytes()) + " of " + prettyBytes(FileFS.totalBytes());
#endif
return res;
}
const String getConfigFile(uint8_t preset, ConfigType_t type) {
char buf[64];
sprintf(buf, "/conf/%s%03d.txt", (type == CT_CONFIG) ? "c" : "s", preset);
return String(buf);
}