From bd1e2ab33250588b33038ab0e9a57437ed1d6e98 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Thu, 11 Jul 2013 18:14:49 +0200 Subject: [PATCH] File: implemented File.openNextFile() and File.rewindDirectory() --- libraries/Bridge/FileIO.cpp | 42 ++++++++++++++++++++++++++++++++++--- libraries/Bridge/FileIO.h | 3 +++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/libraries/Bridge/FileIO.cpp b/libraries/Bridge/FileIO.cpp index 4339c8e..83c889e 100644 --- a/libraries/Bridge/FileIO.cpp +++ b/libraries/Bridge/FileIO.cpp @@ -18,6 +18,8 @@ #include + + File::File(BridgeClass &b) : mode(255), bridge(b) { // Empty } @@ -27,6 +29,7 @@ File::File(const char *_filename, uint8_t _mode, BridgeClass &b) : mode(_mode), char modes[] = {'r','w','a'}; uint8_t cmd[] = {'F', modes[mode]}; uint8_t res[2]; + dirPosition = 1; bridge.transfer(cmd, 2, (uint8_t*)filename.c_str(), filename.length(), res, 2); if (res[0] != 0) { // res[0] contains error code mode = 255; // In case of error keep the file closed @@ -161,9 +164,42 @@ boolean File::isDirectory() { bridge.transfer(cmd, 1, (uint8_t *)filename.c_str(), filename.length(), res, 1); return res[0]; } -//boolean isDirectory(void) -//File openNextFile(uint8_t mode = O_RDONLY); -//void rewindDirectory(void) + + +File File::openNextFile(uint8_t mode){ + Process awk; + char tmp; + String command; + String filepath; + if (dirPosition == 0xFFFF) return File(); + + command = "ls "; + command += filename; + command += " | awk 'NR=="; + command += dirPosition; + command += "'"; + + awk.runShellCommand(command); + + while(awk.running()); + + command = ""; + + while (awk.available()){ + tmp = awk.read(); + if (tmp!='\n') command += tmp; + } + if (command.length() == 0) + return File(); + dirPosition++; + filepath = filename + "/" + command; + return File(filepath.c_str(),mode); + +} + +void File::rewindDirectory(void){ + dirPosition = 1; +} diff --git a/libraries/Bridge/FileIO.h b/libraries/Bridge/FileIO.h index 0105255..0807287 100644 --- a/libraries/Bridge/FileIO.h +++ b/libraries/Bridge/FileIO.h @@ -55,14 +55,17 @@ private: void doBuffer(); uint8_t buffered; uint8_t readPos; + uint16_t dirPosition; static const int BUFFER_SIZE = 64; uint8_t buffer[BUFFER_SIZE]; + private: BridgeClass &bridge; String filename; uint8_t mode; uint8_t handle; + }; class FileSystemClass {