[136] | 1 | /*
|
---|
| 2 |
|
---|
| 3 | SD - a slightly more friendly wrapper for sdfatlib
|
---|
| 4 |
|
---|
| 5 | This library aims to expose a subset of SD card functionality
|
---|
| 6 | in the form of a higher level "wrapper" object.
|
---|
| 7 |
|
---|
| 8 | License: GNU General Public License V3
|
---|
| 9 | (Because sdfatlib is licensed with this.)
|
---|
| 10 |
|
---|
| 11 | (C) Copyright 2010 SparkFun Electronics
|
---|
| 12 |
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | #include <SD.h>
|
---|
| 16 |
|
---|
| 17 | /* for debugging file open/close leaks
|
---|
| 18 | uint8_t nfilecount=0;
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | File::File(SdFile f, const char *n) {
|
---|
| 22 | // oh man you are kidding me, new() doesnt exist? Ok we do it by hand!
|
---|
| 23 | _file = (SdFile *)malloc(sizeof(SdFile));
|
---|
| 24 | if (_file) {
|
---|
| 25 | memcpy(_file, &f, sizeof(SdFile));
|
---|
| 26 |
|
---|
| 27 | strncpy(_name, n, 12);
|
---|
| 28 | _name[12] = 0;
|
---|
| 29 |
|
---|
| 30 | /* for debugging file open/close leaks
|
---|
| 31 | nfilecount++;
|
---|
| 32 | Serial.print("Created \"");
|
---|
| 33 | Serial.print(n);
|
---|
| 34 | Serial.print("\": ");
|
---|
| 35 | Serial.println(nfilecount, DEC);
|
---|
| 36 | */
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | File::File(void) {
|
---|
| 41 | _file = 0;
|
---|
| 42 | _name[0] = 0;
|
---|
| 43 | //Serial.print("Created empty file object");
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | // returns a pointer to the file name
|
---|
| 47 | char *File::name(void) {
|
---|
| 48 | return _name;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | // a directory is a special type of file
|
---|
| 52 | boolean File::isDirectory(void) {
|
---|
| 53 | return (_file && _file->isDir());
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | size_t File::write(uint8_t val) {
|
---|
| 58 | return write(&val, 1);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | size_t File::write(const uint8_t *buf, size_t size) {
|
---|
| 62 | size_t t;
|
---|
| 63 | if (!_file) {
|
---|
| 64 | setWriteError();
|
---|
| 65 | return 0;
|
---|
| 66 | }
|
---|
| 67 | _file->clearWriteError();
|
---|
| 68 | t = _file->write(buf, size);
|
---|
| 69 | if (_file->getWriteError()) {
|
---|
| 70 | setWriteError();
|
---|
| 71 | return 0;
|
---|
| 72 | }
|
---|
| 73 | return t;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | int File::peek() {
|
---|
| 77 | if (! _file)
|
---|
| 78 | return 0;
|
---|
| 79 |
|
---|
| 80 | int c = _file->read();
|
---|
| 81 | if (c != -1) _file->seekCur(-1);
|
---|
| 82 | return c;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | int File::read() {
|
---|
| 86 | if (_file)
|
---|
| 87 | return _file->read();
|
---|
| 88 | return -1;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | // buffered read for more efficient, high speed reading
|
---|
| 92 | int File::read(void *buf, uint16_t nbyte) {
|
---|
| 93 | if (_file)
|
---|
| 94 | return _file->read(buf, nbyte);
|
---|
| 95 | return 0;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | int File::available() {
|
---|
| 99 | if (! _file) return 0;
|
---|
| 100 |
|
---|
| 101 | uint32_t n = size() - position();
|
---|
| 102 |
|
---|
| 103 | return n > 0X7FFF ? 0X7FFF : n;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void File::flush() {
|
---|
| 107 | if (_file)
|
---|
| 108 | _file->sync();
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | boolean File::seek(uint32_t pos) {
|
---|
| 112 | if (! _file) return false;
|
---|
| 113 |
|
---|
| 114 | return _file->seekSet(pos);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | uint32_t File::position() {
|
---|
| 118 | if (! _file) return -1;
|
---|
| 119 | return _file->curPosition();
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | uint32_t File::size() {
|
---|
| 123 | if (! _file) return 0;
|
---|
| 124 | return _file->fileSize();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void File::close() {
|
---|
| 128 | if (_file) {
|
---|
| 129 | _file->close();
|
---|
| 130 | free(_file);
|
---|
| 131 | _file = 0;
|
---|
| 132 |
|
---|
| 133 | /* for debugging file open/close leaks
|
---|
| 134 | nfilecount--;
|
---|
| 135 | Serial.print("Deleted ");
|
---|
| 136 | Serial.println(nfilecount, DEC);
|
---|
| 137 | */
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | File::operator bool() {
|
---|
| 142 | if (_file)
|
---|
| 143 | return _file->isOpen();
|
---|
| 144 | return false;
|
---|
| 145 | }
|
---|
| 146 |
|
---|