|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
-
-
-
-
- #include "SD.h"
- #ifndef __SD_t3_H__
-
-
- #define MAX_COMPONENT_LEN 12
- #define PATH_COMPONENT_BUFFER_LEN MAX_COMPONENT_LEN+1
-
- bool getNextPathComponent(const char *path, unsigned int *p_offset,
- char *buffer) {
-
-
-
-
-
-
- int bufferOffset = 0;
-
- int offset = *p_offset;
-
-
- if (path[offset] == '/') {
- offset++;
- }
-
-
- while (bufferOffset < MAX_COMPONENT_LEN
- && (path[offset] != '/')
- && (path[offset] != '\0')) {
- buffer[bufferOffset++] = path[offset++];
- }
-
- buffer[bufferOffset] = '\0';
-
-
-
- if (path[offset] == '/') {
- offset++;
- }
-
- *p_offset = offset;
-
- return (path[offset] != '\0');
- }
-
-
-
- boolean walkPath(const char *filepath, SdFile& parentDir,
- boolean (*callback)(SdFile& parentDir,
- char *filePathComponent,
- boolean isLastComponent,
- void *object),
- void *object = NULL) {
-
-
-
-
- SdFile subfile1;
- SdFile subfile2;
-
- char buffer[PATH_COMPONENT_BUFFER_LEN];
-
- unsigned int offset = 0;
-
- SdFile *p_parent;
- SdFile *p_child;
-
- SdFile *p_tmp_sdfile;
-
- p_child = &subfile1;
-
- p_parent = &parentDir;
-
- while (true) {
-
- boolean moreComponents = getNextPathComponent(filepath, &offset, buffer);
-
- boolean shouldContinue = callback((*p_parent), buffer, !moreComponents, object);
-
- if (!shouldContinue) {
-
-
-
- if (p_parent != &parentDir) {
- (*p_parent).close();
- }
- return false;
- }
-
- if (!moreComponents) {
- break;
- }
-
- boolean exists = (*p_child).open(*p_parent, buffer, O_RDONLY);
-
-
-
- if (p_parent != &parentDir) {
- (*p_parent).close();
- }
-
-
- if (exists) {
-
-
- if (p_parent == &parentDir) {
- p_parent = &subfile2;
- }
-
- p_tmp_sdfile = p_parent;
- p_parent = p_child;
- p_child = p_tmp_sdfile;
- } else {
- return false;
- }
- }
-
- if (p_parent != &parentDir) {
- (*p_parent).close();
- }
-
- return true;
- }
-
-
-
-
-
- boolean callback_pathExists(SdFile& parentDir, char *filePathComponent,
- boolean isLastComponent, void *object) {
-
-
- SdFile child;
-
- boolean exists = child.open(parentDir, filePathComponent, O_RDONLY);
-
- if (exists) {
- child.close();
- }
-
- return exists;
- }
-
-
-
- boolean callback_makeDirPath(SdFile& parentDir, char *filePathComponent,
- boolean isLastComponent, void *object) {
-
-
- boolean result = false;
- SdFile child;
-
- result = callback_pathExists(parentDir, filePathComponent, isLastComponent, object);
- if (!result) {
- result = child.makeDir(parentDir, filePathComponent);
- }
-
- return result;
- }
-
-
-
-
-
-
-
- boolean callback_remove(SdFile& parentDir, char *filePathComponent,
- boolean isLastComponent, void *object) {
- if (isLastComponent) {
- return SdFile::remove(parentDir, filePathComponent);
- }
- return true;
- }
-
- boolean callback_rmdir(SdFile& parentDir, char *filePathComponent,
- boolean isLastComponent, void *object) {
- if (isLastComponent) {
- SdFile f;
- if (!f.open(parentDir, filePathComponent, O_READ)) return false;
- return f.rmDir();
- }
- return true;
- }
-
-
-
-
-
-
-
- boolean SDClass::begin(uint8_t csPin) {
-
-
- return card.init(SPI_HALF_SPEED, csPin) &&
- volume.init(card) &&
- root.openRoot(volume);
- }
-
-
-
-
- SdFile SDClass::getParentDir(const char *filepath, int *index) {
-
- SdFile d1;
- SdFile d2;
-
- d1.openRoot(volume);
-
-
- SdFile *parent = &d1;
- SdFile *subdir = &d2;
-
- const char *origpath = filepath;
-
- while (strchr(filepath, '/')) {
-
-
- if (filepath[0] == '/') {
- filepath++;
- continue;
- }
-
- if (! strchr(filepath, '/')) {
-
- break;
- }
-
-
- uint8_t idx = strchr(filepath, '/') - filepath;
- if (idx > 12)
- idx = 12;
- char subdirname[13];
- strncpy(subdirname, filepath, idx);
- subdirname[idx] = 0;
-
-
- subdir->close();
- if (! subdir->open(parent, subdirname, O_READ)) {
-
- return SdFile();
- }
-
- filepath += idx;
-
-
- parent->close();
-
-
- SdFile *t = parent;
- parent = subdir;
- subdir = t;
- }
-
- *index = (int)(filepath - origpath);
-
- return *parent;
- }
-
-
- File SDClass::open(const char *filepath, uint8_t mode) {
-
-
-
- int pathidx;
-
-
- SdFile parentdir = getParentDir(filepath, &pathidx);
-
-
- filepath += pathidx;
-
- if (! filepath[0]) {
-
- return File(parentdir, "/");
- }
-
-
- SdFile file;
-
-
- if (!parentdir.isOpen())
- return File();
-
- if ( ! file.open(parentdir, filepath, mode)) {
- return File();
- }
-
- parentdir.close();
-
- if (mode & (O_APPEND | O_WRITE))
- file.seekSet(file.fileSize());
- return File(file, filepath);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- boolean SDClass::exists(const char *filepath) {
-
-
- return walkPath(filepath, root, callback_pathExists);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- boolean SDClass::mkdir(const char *filepath) {
-
-
- return walkPath(filepath, root, callback_makeDirPath);
- }
-
- boolean SDClass::rmdir(const char *filepath) {
-
-
- return walkPath(filepath, root, callback_rmdir);
- }
-
- boolean SDClass::remove(const char *filepath) {
- return walkPath(filepath, root, callback_remove);
- }
-
-
-
- File File::openNextFile(uint8_t mode) {
- dir_t p;
-
-
- while (_file->readDir(&p) > 0) {
-
-
- if (p.name[0] == DIR_NAME_FREE) {
-
- return File();
- }
-
-
- if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') {
-
- continue;
- }
-
-
- if (!DIR_IS_FILE_OR_SUBDIR(&p)) {
-
- continue;
- }
-
-
- SdFile f;
- char name[13];
- _file->dirName(p, name);
-
-
-
- if (f.open(_file, name, mode)) {
-
- return File(f, name);
- } else {
-
- return File();
- }
- }
-
-
- return File();
- }
-
- void File::rewindDirectory(void) {
- if (isDirectory())
- _file->rewind();
- }
-
- SDClass SD;
- #endif
|