From 6f0ea106004e29507f1b6f11f97cacd23b2d4796 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 20 Nov 2010 13:07:59 -0500 Subject: [PATCH] Adding SD.remove(file) and another example. --- libraries/SD/SD.cpp | 21 +++++++++++++---- libraries/SD/SD.h | 3 +++ libraries/SD/examples/Files/Files.pde | 33 +++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 libraries/SD/examples/Files/Files.pde diff --git a/libraries/SD/SD.cpp b/libraries/SD/SD.cpp index dbe353f29..29b89c201 100644 --- a/libraries/SD/SD.cpp +++ b/libraries/SD/SD.cpp @@ -294,10 +294,9 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent, */ if (isLastComponent) { - SDClass *p_MemoryCard = static_cast(object); - p_MemoryCard->file.open(parentDir, filePathComponent, - p_MemoryCard->fileOpenMode); - p_MemoryCard->c = -1; + SDClass *p_SD = static_cast(object); + p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode); + p_SD->c = -1; // TODO: Return file open result? return false; } @@ -305,6 +304,16 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent, } +boolean callback_remove(SdFile& parentDir, char *filePathComponent, + boolean isLastComponent, void *object) { + if (isLastComponent) { + SdFile::remove(parentDir, filePathComponent); + return false; + } + return true; +} + + /* Implementation of class used to create `SDCard` object. */ @@ -415,4 +424,8 @@ boolean SDClass::mkdir(char *filepath) { return walkPath(filepath, root, callback_makeDirPath); } +void SDClass::remove(char *filepath) { + walkPath(filepath, root, callback_remove); +} + SDClass SD; \ No newline at end of file diff --git a/libraries/SD/SD.h b/libraries/SD/SD.h index 010c608ff..c1cdbfbb9 100644 --- a/libraries/SD/SD.h +++ b/libraries/SD/SD.h @@ -60,6 +60,9 @@ public: // Create the requested directory heirarchy--if intermediate directories // do not exist they will be created. boolean mkdir(char *filepath); + + // Delete the file. + void remove(char *filepath); private: SdFile file; diff --git a/libraries/SD/examples/Files/Files.pde b/libraries/SD/examples/Files/Files.pde new file mode 100644 index 000000000..dbcec71cf --- /dev/null +++ b/libraries/SD/examples/Files/Files.pde @@ -0,0 +1,33 @@ +#include + +File f; + +void setup() +{ + Serial.begin(9600); + Serial.print("Initializing SD card..."); + SD.begin(); + Serial.println("done."); + + if (SD.exists("example.txt")) Serial.println("example.txt exists."); + else Serial.println("example.txt doesn't exist."); + + Serial.println("Creating example.txt..."); + f = SD.open("example.txt", true); + f.close(); + + if (SD.exists("example.txt")) Serial.println("example.txt exists."); + else Serial.println("example.txt doesn't exist."); + + Serial.println("Removing example.txt..."); + SD.remove("example.txt"); + + if (SD.exists("example.txt")) Serial.println("example.txt exists."); + else Serial.println("example.txt doesn't exist."); +} + +void loop() +{ +} + +