forked from lancaster-university/codal-microbit-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicroBitFile.cpp
302 lines (255 loc) · 8.43 KB
/
MicroBitFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
The MIT License (MIT)
Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "MicroBitFile.h"
#include "ErrorNo.h"
using namespace codal;
/**
* Creates an instance of a MicroBitFile and creates a new file if required.
*
* @param fileName the name of the file to create/open.
*
* @param mode One of: READ, WRITE, READ_AND_WRITE. Defaults to READ_AND_WRITE.
*/
MicroBitFile::MicroBitFile(ManagedString fileName, int mode)
{
this->fileName = fileName;
MicroBitFileSystem* fs;
if(MicroBitFileSystem::defaultFileSystem == NULL)
fs = new MicroBitFileSystem();
else
fs = MicroBitFileSystem::defaultFileSystem;
fileHandle = fs->open(fileName.toCharArray(), mode);
}
/**
* Seeks to a position in this MicroBitFile instance from the beginning of the file.
*
* @param offset the number of bytes to seek, relative to the beginning of the file.
*
* @return the new seek position, MICROBIT_NOT_SUPPORTED if the current file handle is invalid,
* MICROBIT_INVALID_PARAMETER if the given offset is negative.
*/
int MicroBitFile::setPosition(int offset)
{
if(fileHandle < 0)
return MICROBIT_NOT_SUPPORTED;
if(offset < 0)
return MICROBIT_INVALID_PARAMETER;
return MicroBitFileSystem::defaultFileSystem->seek(fileHandle, offset, MB_SEEK_SET);
}
/**
* Returns the current position of the seek head for the current file.
*
* @return the new seek position, MICROBIT_NOT_SUPPORTED if the current file handle is invalid.
*/
int MicroBitFile::getPosition()
{
if(fileHandle < 0)
return MICROBIT_NOT_SUPPORTED;
return MicroBitFileSystem::defaultFileSystem->seek(fileHandle, 0, MB_SEEK_CUR);
}
/**
* Writes the given bytes to this MicroBitFile instance at the current position.
*
* @param bytes a pointer to the bytes to write to this file.
*
* @param len the number of bytes to write.
*
* @return The number of bytes written, MICROBIT_NOT_SUPPORTED if the current file
* handle is invalid, MICROBIT_INVALID_PARAMETER if bytes is invalid, or
* len is negative.
*/
int MicroBitFile::write(const char *bytes, int len)
{
if(fileHandle < 0)
return MICROBIT_NOT_SUPPORTED;
return MicroBitFileSystem::defaultFileSystem->write(fileHandle, (uint8_t*)bytes, len);
}
/**
* Writes the given ManagedString to this MicroBitFile instance at the current position.
*
* @param s The ManagedString to write to this file.
*
* @return The number of bytes written, MICROBIT_NOT_SUPPORTED if the current file
* handle is invalid, MICROBIT_INVALID_PARAMETER if bytes is invalid, or
* len is negative.
*/
int MicroBitFile::write(ManagedString s)
{
return write(s.toCharArray(), s.length());
}
/**
* Reads a single character from the file at the current position.
*
* @return the character, or MICROBIT_NOT_SUPPORTED if the current file handle
* is invalid.
*/
int MicroBitFile::read()
{
if(fileHandle < 0)
return MICROBIT_NOT_SUPPORTED;
char c;
int ret = read( &c, 1);
if ( ret > 0)
return c;
return ret < 0 ? ret : MICROBIT_NO_DATA;
}
/**
* Reads from the file into a given buffer.
*
* @param buffer a pointer to the buffer where data will be stored.
*
* @param size the number of bytes that can be safely stored in the buffer.
*
* @return the number of bytes read, or MICROBIT_INVALID_PARAMETER if buffer is
* invalid, or the size given is less than 0.
*/
int MicroBitFile::read(char *buffer, int size)
{
if(fileHandle < 0)
return MICROBIT_NOT_SUPPORTED;
if(size < 0 || buffer == NULL)
return MICROBIT_INVALID_PARAMETER;
return MicroBitFileSystem::defaultFileSystem->read(fileHandle, (uint8_t*)buffer, size);
}
/**
* Reads from the current MicroBitFile into a given buffer.
*
* @param size the number of bytes to be read from the file.
*
* @return a ManagedString containing the requested bytes, oran empty ManagedString
* on error.
*/
ManagedString MicroBitFile::read(int size)
{
char buff[size + 1];
buff[size] = 0;
int ret = read(buff, size);
if(ret < 0)
return ManagedString();
return ManagedString(buff,ret);
}
/**
* Removes this MicroBitFile from the MicroBitFileSystem.
*
* @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the given filename
* does not exist, MICROBIT_CANCELLED if something went wrong.
*/
int MicroBitFile::remove()
{
if ( fileHandle >= 0)
close();
int ret = MicroBitFileSystem::defaultFileSystem->remove(fileName.toCharArray());
if(ret < 0)
return ret;
fileHandle = MICROBIT_NOT_SUPPORTED;
return ret;
}
/**
* Seeks to the end of the file, and appends the given ManagedString to this MicroBitFile instance.
*
* @param bytes a pointer to the bytes to write to this file.
*
* @param len the number of bytes to write.
*
* @return The number of bytes written, MICROBIT_NOT_SUPPORTED if the current file
* handle is invalid or this file was not opened in WRITE mode.
*/
int MicroBitFile::append(const char *bytes, int len)
{
if(fileHandle < 0)
return MICROBIT_NOT_SUPPORTED;
int ret = MicroBitFileSystem::defaultFileSystem->seek(fileHandle, 0, MB_SEEK_END);
if(ret < 0)
return ret;
return write(bytes,len);
}
/**
* Seeks to the end of the file, and appends the given ManagedString to this MicroBitFile instance.
*
* @param s The ManagedString to write to this file.
*
* @return The number of bytes written, MICROBIT_NOT_SUPPORTED if the current file
* handle is invalid or this file was not opened in WRITE mode.
*/
int MicroBitFile::append(ManagedString s)
{
return append(s.toCharArray(), s.length());
}
/**
* Determines if this MicroBitFile instance refers to a valid, open file.
*
* @return true if this file is valid, false otherwise.
*
*/
bool MicroBitFile::isValid()
{
return fileHandle >= 0;
}
/**
* Returns the handle used by this MicroBitFile instance.
*
* @note This member function will also inform the user of any errors encountered
* during the opening of this MicroBitFile. At open, the handle is set
* to the return value from MicroBitFileSystem.open().
*/
int MicroBitFile::getHandle()
{
return fileHandle;
}
/**
* Closes this MicroBitFile instance
*
* @return MICROBIT_OK on success, MICROBIT_NOT_SUPPORTED if the file handle
* is invalid.
*
* @note MicroBitFiles are opened at construction and are implicitly closed at
* destruction. They can be closed explicitly using this member function.
*/
int MicroBitFile::close()
{
if(fileHandle < 0)
return MICROBIT_NOT_SUPPORTED;
int ret = MicroBitFileSystem::defaultFileSystem->close(fileHandle);
if(ret < 0)
return ret;
fileHandle = MICROBIT_NO_RESOURCES;
return ret;
}
/**
* Writes back all state associated with the given file to FLASH memory,
* leaving the file open.
*
* @return MICROBIT_OK on success, MICROBIT_NOT_SUPPORTED if the file system has not
* been initialised or if this file is invalid.
*/
int MicroBitFile::flush()
{
if(fileHandle < 0)
return MICROBIT_NOT_SUPPORTED;
return MicroBitFileSystem::defaultFileSystem->flush(fileHandle);
}
/**
* Destructor for MicroBitFile. Implicitly closes the current file.
*/
MicroBitFile::~MicroBitFile()
{
close();
}