2010-11-20 04:30:40 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
SD - a slightly more friendly wrapper for sdfatlib
|
|
|
|
|
|
|
|
This library aims to expose a subset of SD card functionality
|
|
|
|
in the form of a higher level "wrapper" object.
|
|
|
|
|
|
|
|
License: GNU General Public License V3
|
|
|
|
(Because sdfatlib is licensed with this.)
|
|
|
|
|
|
|
|
(C) Copyright 2010 SparkFun Electronics
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <SD.h>
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
/* for debugging file open/close leaks
|
|
|
|
uint8_t nfilecount=0;
|
|
|
|
*/
|
|
|
|
|
|
|
|
File::File(SdFile f, const char *n) {
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
/* for debugging file open/close leaks
|
|
|
|
nfilecount++;
|
|
|
|
Serial.print("Created \"");
|
|
|
|
Serial.print(n);
|
|
|
|
Serial.print("\": ");
|
|
|
|
Serial.println(nfilecount, DEC);
|
|
|
|
*/
|
|
|
|
}
|
2010-11-20 04:30:40 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
File::File(void) {
|
|
|
|
_file = 0;
|
|
|
|
_name[0] = 0;
|
|
|
|
//Serial.print("Created empty file object");
|
2010-11-20 04:30:40 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 17:45:13 +02:00
|
|
|
File::~File(void) {
|
|
|
|
// Serial.print("Deleted file object");
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns a pointer to the file name
|
|
|
|
char *File::name(void) {
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// a directory is a special type of file
|
|
|
|
boolean File::isDirectory(void) {
|
|
|
|
return (_file && _file->isDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t File::write(uint8_t val) {
|
|
|
|
return write(&val, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t File::write(const uint8_t *buf, size_t size) {
|
|
|
|
size_t t;
|
|
|
|
if (!_file) {
|
|
|
|
setWriteError();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
_file->clearWriteError();
|
|
|
|
t = _file->write(buf, size);
|
|
|
|
if (_file->getWriteError()) {
|
|
|
|
setWriteError();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return t;
|
2010-11-20 04:30:40 +01:00
|
|
|
}
|
|
|
|
|
2010-11-20 17:45:05 +01:00
|
|
|
int File::peek() {
|
2011-10-27 17:45:13 +02:00
|
|
|
if (! _file)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int c = _file->read();
|
|
|
|
if (c != -1) _file->seekCur(-1);
|
2010-12-22 23:47:50 +01:00
|
|
|
return c;
|
2010-11-20 17:45:05 +01:00
|
|
|
}
|
|
|
|
|
2010-11-20 04:30:40 +01:00
|
|
|
int File::read() {
|
2011-10-27 17:45:13 +02:00
|
|
|
if (_file)
|
|
|
|
return _file->read();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// buffered read for more efficient, high speed reading
|
|
|
|
int File::read(void *buf, uint16_t nbyte) {
|
|
|
|
if (_file)
|
|
|
|
return _file->read(buf, nbyte);
|
|
|
|
return 0;
|
2010-11-20 04:30:40 +01:00
|
|
|
}
|
|
|
|
|
2010-11-20 17:45:05 +01:00
|
|
|
int File::available() {
|
2011-10-27 17:45:13 +02:00
|
|
|
if (! _file) return 0;
|
|
|
|
|
|
|
|
uint32_t n = size() - position();
|
|
|
|
|
|
|
|
return n > 0X7FFF ? 0X7FFF : n;
|
2010-11-20 17:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void File::flush() {
|
2011-10-27 17:45:13 +02:00
|
|
|
if (_file)
|
|
|
|
_file->sync();
|
2010-11-20 17:45:05 +01:00
|
|
|
}
|
|
|
|
|
2010-12-22 22:11:54 +01:00
|
|
|
boolean File::seek(uint32_t pos) {
|
2011-10-27 17:45:13 +02:00
|
|
|
if (! _file) return false;
|
|
|
|
|
|
|
|
return _file->seekSet(pos);
|
2010-12-22 22:11:54 +01:00
|
|
|
}
|
|
|
|
|
2010-12-22 22:52:09 +01:00
|
|
|
uint32_t File::position() {
|
2011-10-27 17:45:13 +02:00
|
|
|
if (! _file) return -1;
|
|
|
|
return _file->curPosition();
|
2010-12-22 22:52:09 +01:00
|
|
|
}
|
|
|
|
|
2010-12-22 22:11:54 +01:00
|
|
|
uint32_t File::size() {
|
2011-10-27 17:45:13 +02:00
|
|
|
if (! _file) return 0;
|
|
|
|
return _file->fileSize();
|
2010-12-22 22:11:54 +01:00
|
|
|
}
|
|
|
|
|
2010-11-20 04:30:40 +01:00
|
|
|
void File::close() {
|
2011-10-27 17:45:13 +02:00
|
|
|
if (_file) {
|
|
|
|
_file->close();
|
|
|
|
free(_file);
|
|
|
|
_file = 0;
|
|
|
|
|
|
|
|
/* for debugging file open/close leaks
|
|
|
|
nfilecount--;
|
|
|
|
Serial.print("Deleted ");
|
|
|
|
Serial.println(nfilecount, DEC);
|
|
|
|
*/
|
|
|
|
}
|
2010-11-20 04:30:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
File::operator bool() {
|
2011-10-27 17:45:13 +02:00
|
|
|
if (_file)
|
|
|
|
return _file->isOpen();
|
|
|
|
return false;
|
2010-11-20 04:30:40 +01:00
|
|
|
}
|
2011-10-27 17:45:13 +02:00
|
|
|
|