Initial commit
This commit is contained in:
174
Ecran Lilygot-T-RGB/Code tests/SDCard/FileHandler.cpp
Normal file
174
Ecran Lilygot-T-RGB/Code tests/SDCard/FileHandler.cpp
Normal file
@@ -0,0 +1,174 @@
|
||||
#include "FileHandler.h"
|
||||
|
||||
|
||||
FileHandler::FileHandler() {
|
||||
// Initialisez vos membres de classe ici si nécessaire
|
||||
}
|
||||
|
||||
|
||||
FileInfo* FileHandler::listDir(fs::FS &fs, const char *dirname, uint8_t levels, int &fileCount) {
|
||||
Serial.printf("Listing directory: %s\n", dirname);
|
||||
File root = fs.open(dirname);
|
||||
if (!root) {
|
||||
Serial.println("Failed to open directory");
|
||||
fileCount = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!root.isDirectory()) {
|
||||
Serial.println("Not a directory");
|
||||
fileCount = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
File file = root.openNextFile();
|
||||
FileInfo* fileInfo = nullptr;
|
||||
int count = 0;
|
||||
|
||||
while (file) {
|
||||
FileInfo* newFileInfo = (FileInfo*)malloc(sizeof(FileInfo));
|
||||
newFileInfo->name = file.name();
|
||||
newFileInfo->size = file.size();
|
||||
|
||||
FileInfo* temp = (FileInfo*)realloc(fileInfo, (count + 1) * sizeof(FileInfo));
|
||||
if (temp == nullptr) {
|
||||
// Échec de réallocation, libérer la mémoire précédemment allouée
|
||||
freeFileInfo(fileInfo);
|
||||
fileCount = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
fileInfo = temp;
|
||||
fileInfo[count] = *newFileInfo;
|
||||
free(newFileInfo);
|
||||
|
||||
count++;
|
||||
|
||||
if (file.isDirectory() && levels) {
|
||||
FileInfo* subDirFiles = listDir(fs, file.path(), levels - 1, fileCount);
|
||||
if (subDirFiles) {
|
||||
for (int i = 0; i < fileCount; i++) {
|
||||
FileInfo* newFileInfo = (FileInfo*)malloc(sizeof(FileInfo));
|
||||
newFileInfo->name = subDirFiles[i].name;
|
||||
newFileInfo->size = subDirFiles[i].size;
|
||||
|
||||
FileInfo* temp = (FileInfo*)realloc(fileInfo, (count + 1) * sizeof(FileInfo));
|
||||
if (temp == nullptr) {
|
||||
// Échec de réallocation, libérer la mémoire précédemment allouée
|
||||
freeFileInfo(fileInfo);
|
||||
fileCount = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
fileInfo = temp;
|
||||
fileInfo[count] = *newFileInfo;
|
||||
free(newFileInfo);
|
||||
|
||||
count++;
|
||||
}
|
||||
freeFileInfo(subDirFiles);
|
||||
}
|
||||
}
|
||||
|
||||
file = root.openNextFile();
|
||||
}
|
||||
|
||||
fileCount = count;
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
void FileHandler::freeFileInfo(FileInfo* fileInfo) {
|
||||
if (fileInfo) {
|
||||
free(fileInfo);
|
||||
}
|
||||
}
|
||||
|
||||
bool FileHandler::createDir(fs::FS &fs, const char *path) {
|
||||
Serial.printf("Creating Dir: %s\n", path);
|
||||
if (fs.mkdir(path)) {
|
||||
Serial.println("Dir created");
|
||||
return true;
|
||||
} else {
|
||||
Serial.println("mkdir failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileHandler::removeDir(fs::FS &fs, const char *path) {
|
||||
Serial.printf("Removing Dir: %s\n", path);
|
||||
if (fs.rmdir(path)) {
|
||||
Serial.println("Dir removed");
|
||||
return true;
|
||||
} else {
|
||||
Serial.println("rmdir failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileHandler::readFile(fs::FS &fs, const char *path) {
|
||||
Serial.printf("Reading file: %s\n", path);
|
||||
File file = fs.open(path);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for reading");
|
||||
return false;
|
||||
}
|
||||
Serial.print("Read from file: ");
|
||||
while (file.available()) {
|
||||
Serial.write(file.read());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileHandler::writeFile(fs::FS &fs, const char *path, const char *message) {
|
||||
Serial.printf("Writing file: %s\n", path);
|
||||
File file = fs.open(path, FILE_WRITE);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for writing");
|
||||
return false;
|
||||
}
|
||||
if (file.print(message)) {
|
||||
Serial.println("File written");
|
||||
return true;
|
||||
} else {
|
||||
Serial.println("Write failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileHandler::appendFile(fs::FS &fs, const char *path, const char *message) {
|
||||
Serial.printf("Appending to file: %s\n", path);
|
||||
File file = fs.open(path, FILE_APPEND);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for appending");
|
||||
return false;
|
||||
}
|
||||
if (file.print(message)) {
|
||||
Serial.println("Message appended");
|
||||
return true;
|
||||
} else {
|
||||
Serial.println("Append failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileHandler::renameFile(fs::FS &fs, const char *path1, const char *path2) {
|
||||
Serial.printf("Renaming file %s to %s\n", path1, path2);
|
||||
if (fs.rename(path1, path2)) {
|
||||
Serial.println("File renamed");
|
||||
return true;
|
||||
} else {
|
||||
Serial.println("Rename failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileHandler::deleteFile(fs::FS &fs, const char *path) {
|
||||
Serial.printf("Deleting file: %s\n", path);
|
||||
if (fs.remove(path)) {
|
||||
Serial.println("File deleted");
|
||||
return true;
|
||||
} else {
|
||||
Serial.println("Delete failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user