From 0887b98f627500271b5ad8b3c4f6c7b90bc227ee Mon Sep 17 00:00:00 2001 From: Thibaut VIARD Date: Tue, 21 Jun 2011 00:20:43 +0200 Subject: Moving all AVR specific libraries to hardware/avr --- libraries/SD/File.cpp | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 libraries/SD/File.cpp (limited to 'libraries/SD/File.cpp') diff --git a/libraries/SD/File.cpp b/libraries/SD/File.cpp new file mode 100644 index 0000000..9ef8196 --- /dev/null +++ b/libraries/SD/File.cpp @@ -0,0 +1,143 @@ +/* + + 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 + +/* for debugging file open/close leaks + uint8_t nfilecount=0; +*/ + +File::File(SdFile f, 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); + */ + } +} + +File::File(void) { + _file = 0; + _name[0] = 0; + //Serial.print("Created empty file object"); +} + +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()); +} + + +void File::write(uint8_t val) { + if (_file) + _file->write(val); +} + +void File::write(const char *str) { + if (_file) + _file->write(str); +} + +void File::write(const uint8_t *buf, size_t size) { + if (_file) + _file->write(buf, size); +} + +int File::peek() { + if (! _file) + return 0; + + int c = _file->read(); + if (c != -1) _file->seekCur(-1); + return c; +} + +int File::read() { + 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; +} + +int File::available() { + if (! _file) return 0; + return size() - position(); +} + +void File::flush() { + if (_file) + _file->sync(); +} + +boolean File::seek(uint32_t pos) { + if (! _file) return false; + + return _file->seekSet(pos); +} + +uint32_t File::position() { + if (! _file) return -1; + return _file->curPosition(); +} + +uint32_t File::size() { + if (! _file) return 0; + return _file->fileSize(); +} + +void 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() { + if (_file) + return _file->isOpen(); + return false; +} + -- cgit v1.2.3-18-g5258