]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/SD/File.cpp
first commit
[arduino] / books / pdummies / Libraries / SD / File.cpp
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, 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 #if ARDUINO >= 100
62
63 size_t File::write(uint8_t val) {
64   if (_file)
65     return _file->write(val);
66   else
67     return 0;
68 }
69
70 size_t File::write(const char *str) {
71   if (_file) 
72     return _file->write(str);
73   else
74     return 0;
75 }
76
77 size_t File::write(const uint8_t *buf, size_t size) {
78   if (_file)
79     return _file->write(buf, size);
80   else
81     return 0;
82 }
83
84 #else
85
86 void File::write(uint8_t val) {
87   if (_file)
88     _file->write(val);
89 }
90
91 void File::write(const char *str) {
92   if (_file) 
93     _file->write(str);
94 }
95
96 void File::write(const uint8_t *buf, size_t size) {
97   if (_file)
98     _file->write(buf, size);
99 }
100
101 #endif // ARDUINO
102
103 int File::peek() {
104   if (! _file) 
105     return 0;
106
107   int c = _file->read();
108   if (c != -1) _file->seekCur(-1);
109   return c;
110 }
111
112 int File::read() {
113   if (_file) 
114     return _file->read();
115   return -1;
116 }
117
118 // buffered read for more efficient, high speed reading
119 int File::read(void *buf, uint16_t nbyte) {
120   if (_file) 
121     return _file->read(buf, nbyte);
122   return 0;
123 }
124
125 int File::available() {
126   if (! _file) return 0;
127
128   uint32_t n = size() - position();
129
130   return n > 0X7FFF ? 0X7FFF : n;
131 }
132
133 void File::flush() {
134   if (_file)
135     _file->sync();
136 }
137
138 boolean File::seek(uint32_t pos) {
139   if (! _file) return false;
140
141   return _file->seekSet(pos);
142 }
143
144 uint32_t File::position() {
145   if (! _file) return -1;
146   return _file->curPosition();
147 }
148
149 uint32_t File::size() {
150   if (! _file) return 0;
151   return _file->fileSize();
152 }
153
154 void File::close() {
155   if (_file) {
156     _file->close();
157     free(_file); 
158     _file = 0;
159
160     /* for debugging file open/close leaks
161     nfilecount--;
162     Serial.print("Deleted ");
163     Serial.println(nfilecount, DEC);
164     */
165   }
166 }
167
168 File::operator bool() {
169   if (_file) 
170     return  _file->isOpen();
171   return false;
172 }
173