root / arduino-1.0 / libraries / SD / File.cpp @ 58d82c77
History | View | Annotate | Download (2.62 KB)
1 | 58d82c77 | Tom Mullins | /*
|
---|---|---|---|
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 | File::~File(void) {
|
||
47 | // Serial.print("Deleted file object");
|
||
48 | } |
||
49 | |||
50 | // returns a pointer to the file name
|
||
51 | char *File::name(void) { |
||
52 | return _name;
|
||
53 | } |
||
54 | |||
55 | // a directory is a special type of file
|
||
56 | boolean File::isDirectory(void) {
|
||
57 | return (_file && _file->isDir());
|
||
58 | } |
||
59 | |||
60 | |||
61 | size_t File::write(uint8_t val) { |
||
62 | return write(&val, 1); |
||
63 | } |
||
64 | |||
65 | size_t File::write(const uint8_t *buf, size_t size) {
|
||
66 | size_t t; |
||
67 | if (!_file) {
|
||
68 | setWriteError(); |
||
69 | return 0; |
||
70 | } |
||
71 | _file->clearWriteError(); |
||
72 | t = _file->write(buf, size); |
||
73 | if (_file->getWriteError()) {
|
||
74 | setWriteError(); |
||
75 | return 0; |
||
76 | } |
||
77 | return t;
|
||
78 | } |
||
79 | |||
80 | int File::peek() {
|
||
81 | if (! _file)
|
||
82 | return 0; |
||
83 | |||
84 | int c = _file->read();
|
||
85 | if (c != -1) _file->seekCur(-1); |
||
86 | return c;
|
||
87 | } |
||
88 | |||
89 | int File::read() {
|
||
90 | if (_file)
|
||
91 | return _file->read();
|
||
92 | return -1; |
||
93 | } |
||
94 | |||
95 | // buffered read for more efficient, high speed reading
|
||
96 | int File::read(void *buf, uint16_t nbyte) { |
||
97 | if (_file)
|
||
98 | return _file->read(buf, nbyte);
|
||
99 | return 0; |
||
100 | } |
||
101 | |||
102 | int File::available() {
|
||
103 | if (! _file) return 0; |
||
104 | |||
105 | uint32_t n = size() - position(); |
||
106 | |||
107 | return n > 0X7FFF ? 0X7FFF : n; |
||
108 | } |
||
109 | |||
110 | void File::flush() {
|
||
111 | if (_file)
|
||
112 | _file->sync(); |
||
113 | } |
||
114 | |||
115 | boolean File::seek(uint32_t pos) { |
||
116 | if (! _file) return false; |
||
117 | |||
118 | return _file->seekSet(pos);
|
||
119 | } |
||
120 | |||
121 | uint32_t File::position() { |
||
122 | if (! _file) return -1; |
||
123 | return _file->curPosition();
|
||
124 | } |
||
125 | |||
126 | uint32_t File::size() { |
||
127 | if (! _file) return 0; |
||
128 | return _file->fileSize();
|
||
129 | } |
||
130 | |||
131 | void File::close() {
|
||
132 | if (_file) {
|
||
133 | _file->close(); |
||
134 | free(_file); |
||
135 | _file = 0;
|
||
136 | |||
137 | /* for debugging file open/close leaks
|
||
138 | nfilecount--;
|
||
139 | Serial.print("Deleted ");
|
||
140 | Serial.println(nfilecount, DEC);
|
||
141 | */
|
||
142 | } |
||
143 | } |
||
144 | |||
145 | File::operator bool() { |
||
146 | if (_file)
|
||
147 | return _file->isOpen();
|
||
148 | return false; |
||
149 | } |