Skip to content

Commit

Permalink
fix dir listing in subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
newdigate committed Dec 28, 2023
1 parent 68a933c commit 9ba5a81
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/InMemoryFile.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "SD.h"

InMemoryFile::InMemoryFile(const char *name, char *data, uint32_t size, uint8_t mode) {
InMemoryFile::InMemoryFile(const char *name, char *data, uint32_t size, uint8_t mode) : AbstractFile(name) {
_fileName = name;
_data = data;
_size = size;
_position = 0;
_isOpen = true;
}

InMemoryFile::InMemoryFile(void) {
InMemoryFile::InMemoryFile(void) : AbstractFile("n/a") {
_size = -1;
_position = 0;
_isOpen = true;
Expand Down
25 changes: 15 additions & 10 deletions src/LinuxFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@
#include <string>
#include <unistd.h>

LinuxFile::LinuxFile(const char *name, uint8_t mode, SDClass &sd) : _sd(sd) {
std::string actualFileName = sd.getSDCardFolderPath() + std::string("/") + std::string(name);
LinuxFile::LinuxFile(const char *name, const char *path, uint8_t mode, SDClass &sd) : AbstractFile(name), _sd(sd) {
std::string actualFileName;
if (strlen(path)==0) {
actualFileName = sd.getSDCardFolderPath() + std::string("/") + std::string(name);
} else
actualFileName = sd.getSDCardFolderPath() + std::string("/") + std::string(path) + std::string("/") + std::string(name);

localFileName = new char[actualFileName.length() + 1] {0};
memcpy((char*)localFileName, actualFileName.c_str(), actualFileName.length());

size_t last_slash_idx = actualFileName.rfind('/');
if (std::string::npos != last_slash_idx)
{
std::string temppath = actualFileName.substr(0, last_slash_idx);
path = new char[temppath.length() + 1] {0};
memcpy(path, temppath.c_str(), temppath.length());
localPath = new char[temppath.length() + 1] {0};
memcpy(localPath, temppath.c_str(), temppath.length());
}
_path = path;

// cout << actualFileName;
if (!is_directory(localFileName) ) {
Expand Down Expand Up @@ -163,7 +169,7 @@ File LinuxFile::openNextFile(void) {
struct dirent *entry;

if (!dp)
dp = opendir(isCurrentFileADirectory? localFileName : path);
dp = opendir(isCurrentFileADirectory? localFileName : localPath);

if (dp == NULL) {
perror("opendir: Path does not exist or could not be read.");
Expand All @@ -172,14 +178,13 @@ File LinuxFile::openNextFile(void) {

if (isCurrentFileADirectory){
while (true) {
bool isCurrentFileADirectory = false;
entry = readdir(dp);

if (entry != NULL) {
//cout << entry->d_name << std::endl;
char *nextFileName = new char[strlen(entry->d_name) + 1] {0};
memcpy(nextFileName, entry->d_name, strlen(entry->d_name));
File f = File(new LinuxFile(nextFileName, O_READ, this->_sd));
auto l = _sd.getSDCardFolderPath().length();
std::string lfnString = std::string( localFileName );
std::string final = lfnString.substr(l+1, lfnString.length()-l-1);
File f = File(new LinuxFile(entry->d_name, final.c_str(), O_READ, this->_sd));
return f;
} else
break;
Expand Down
19 changes: 15 additions & 4 deletions src/SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,20 @@ File SDClass::open(const char *filepath, uint8_t mode) {
AbstractFile *result;
if (_useMockData) {
result = new InMemoryFile(filepath, _fileData, _fileSize, mode );
} else
result = new LinuxFile(filepath, mode, *this);
} else {
std::string pathString = std::string(filepath);
std::string fileNameString = std::string(filepath);
size_t last_slash_idx = pathString.rfind('/');
char *path = "";
if (std::string::npos != last_slash_idx)
{
fileNameString = fileNameString.substr(last_slash_idx+1, strlen(filepath)-last_slash_idx-1);
std::string temppath = pathString.substr(0, last_slash_idx);
path = new char[temppath.length() + 1] {0};
memcpy(path, temppath.c_str(), temppath.length());
}
result = new LinuxFile(fileNameString.c_str(), path, mode, *this);
}
return File(result);
}

Expand Down Expand Up @@ -491,6 +503,5 @@ bool SDClass::remove(const char *filepath) {
SDClass SD;




AbstractFile::AbstractFile(const char *fileName) : _fileName(fileName) {}
};
7 changes: 5 additions & 2 deletions src/SD.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace SDLib {
bool _isDirectory;
const char *_fileName;

explicit AbstractFile(const char *fileName);

virtual bool isDirectory() = 0;
int read() override = 0;
virtual int read(void *buf, uint16_t nbyte) = 0;
Expand Down Expand Up @@ -102,11 +104,12 @@ class InMemoryFile : public AbstractFile {
class LinuxFile : public AbstractFile {
private:
const char * localFileName;
char * path;
char * localPath;
const char * _path;
std::fstream mockFile = std::fstream();
DIR *dp = NULL;
public:
LinuxFile(const char *name, uint8_t mode = O_READ, SDClass &sd = SD);
LinuxFile(const char *name, const char *path, uint8_t mode = O_READ, SDClass &sd = SD);
LinuxFile(SDClass &sd = SD);

static std::streampos fileSize( const char* filePath );
Expand Down

0 comments on commit 9ba5a81

Please sign in to comment.