Skip to content
This repository was archived by the owner on Jul 25, 2019. It is now read-only.

Commit

Permalink
Adjusted multi-file support with files on the heap not stack
Browse files Browse the repository at this point in the history
  • Loading branch information
ladyada committed Feb 26, 2011
1 parent c7a2f61 commit b66f115
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 51 deletions.
94 changes: 68 additions & 26 deletions File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,40 @@
(Because sdfatlib is licensed with this.)
(C) Copyright 2010 SparkFun Electronics
Multi-file support and some extras added by Limor Fried
*/

#include <SD.h>

/* for debugging file open/close leaks
uint8_t nfilecount=0;
*/

File::File(SdFile f, char *n) {
_file = f;
strncpy(_name, n, 12);
_name[12] = 0;
_c = -1;

/*
Serial.print("Created file object \"");
Serial.print(n);
Serial.println("\"");
*/
// oh man you are kidding me, new() doesnt exist? Ok we do it by hand!
_file = (SdFile *)malloc(sizeof(SdFile));
if (_file) {
memcpy(_file, &f, sizeof(SdFile));

strncpy(_name, n, 12);
_name[12] = 0;
_c = -1;

/* for debugging file open/close leaks
nfilecount++;
Serial.print("Created \"");
Serial.print(n);
Serial.print("\": ");
Serial.println(nfilecount, DEC);
*/
}
}

File::File(void) {
_c = -1;
_file = 0;
_name[0] = 0;
//Serial.print("Created empty file object");
}

Expand All @@ -46,68 +59,97 @@ char *File::name(void) {
};

boolean File::isDirectory(void) {
return _file.isDir();
return (_file && _file->isDir());
}

void File::write(uint8_t val) {
_file.write(val);
if (_file)
_file->write(val);
}

void File::write(const char *str) {
_file.write(str);
if (_file)
_file->write(str);
}

void File::write(const uint8_t *buf, size_t size) {
_file.write(buf, size);
if (_file)
_file->write(buf, size);
}

int File::peek() {
if (_c != -1) return _c;
_c = _file.read();
if (!_file)
return 0;

if (_c != -1)
return _c;
_c = _file->read();
return _c;
}

boolean File::seekSet(uint32_t pos) {
return _file.seekSet(pos);
return (_file && _file->seekSet(pos));
}

uint32_t File::size(void) {
return _file.fileSize();
if (_file)
return _file->fileSize();
return 0;
}


int File::read() {
if (!_file)
return 0;

if (_c != -1) {
int tmp = _c;
_c = -1;
return tmp;
}
return _file.read();
return _file->read();
}

int File::read(void *buf, uint16_t nbyte) {
if (!_file)
return 0;

if ((_c != -1) && (nbyte > 0)) {
((char *)buf)[0] = SD.c;
_c = -1;
return _file.read(((char *)buf)+1, nbyte-1);
return _file->read(((char *)buf)+1, nbyte-1);
}
return _file.read(buf, nbyte);
return _file->read(buf, nbyte);
}

int File::available() {
if (_c != -1) return 1;
_c = _file.read();
if (_c != -1)
return 1;
if (! _file)
return 0;
_c = _file->read();
return _c != -1;
}

void File::flush() {
_file.sync();
if (_file)
_file->sync();
}

void File::close() {
_file.close();
if (_file) {
_file->close();
free(_file);
_file = 0;

/* for debugging file open/close leaks
nfilecount--;
Serial.print("Deleted ");
Serial.println(nfilecount, DEC);
*/
}
}

File::operator bool() {
return _file.isOpen();
return (_file && _file->isOpen());
}
56 changes: 32 additions & 24 deletions SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ File SDClass::open(char *filepath, uint8_t mode) {
return File();
}
parentdir.close();

if (mode == FILE_APPEND)
file.seekSet(file.fileSize());
return File(file, filepath);
}

Expand Down Expand Up @@ -197,32 +200,37 @@ boolean SDClass::mkdir(char *filepath) {
A rough equivalent to `mkdir -p`.
*/

/*
int pathidx;
// do the interative search
SdFile parentdir = getParentDir(filepath, &pathidx);
// no more subdirs!
// go thru each subdir
char path[80];
strncpy(path, filepath, 79);
path[79] = 0;
SDfile parent = root;
SDfile child;
filepath += pathidx;
while (path[0]) {
// get rid of leading /'s
if (path[0] == '/') {
path++;
continue;
}
if (! path[0]) {
// it was in the root directory, so leave now
break;
}
if (! filepath[0]) {
// it was the directory itself!
return File(parentdir, "/");
}
// extract just the name of the next subdirectory
char *post = strchr(path, '/');
post[0] = 0;
// Open the file itself
SdFile file;
if (!parentdir.isOpen() || ! file.open(parentdir, filepath, mode)) {
// failed to open the final file itself or one of the subdirs
return File();
// the next subdir
Serial.println(path);
filepath += idx;
}
parentdir.close();
return File(file, filepath);
// return walkPath(filepath, root, callback_makeDirPath);
*/
}

Expand All @@ -246,7 +254,7 @@ File File::openNextFile(uint8_t mode) {
dir_t p;

//Serial.print("\t\treading dir...");
while (_file.readDir(&p) > 0) {
while (_file->readDir(&p) > 0) {

// done if past last used entry
if (p.name[0] == DIR_NAME_FREE) {
Expand All @@ -269,7 +277,7 @@ File File::openNextFile(uint8_t mode) {
// print file name with possible blank fill
SdFile f;
char name[12];
_file.dirName(p, name);
_file->dirName(p, name);
//Serial.print("try to open file ");
//Serial.println(name);

Expand All @@ -288,7 +296,7 @@ File File::openNextFile(uint8_t mode) {

void File::rewindDirectory(void) {
if (isDirectory())
_file.rewind();
_file->rewind();
}

SDClass SD;
2 changes: 1 addition & 1 deletion SD.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class File : public Stream {
private:
char _name[13];
SdFile _file;
SdFile *_file;
int _c;

public:
Expand Down

0 comments on commit b66f115

Please sign in to comment.