User | Revision | Line number | New contents of line |
el14afma |
0:5ffb234d134c
|
1
|
/* mbed Microcontroller Library
|
el14afma |
0:5ffb234d134c
|
2
|
* Copyright (c) 2006-2012 ARM Limited
|
el14afma |
0:5ffb234d134c
|
3
|
*
|
el14afma |
0:5ffb234d134c
|
4
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
el14afma |
0:5ffb234d134c
|
5
|
* of this software and associated documentation files (the "Software"), to deal
|
el14afma |
0:5ffb234d134c
|
6
|
* in the Software without restriction, including without limitation the rights
|
el14afma |
0:5ffb234d134c
|
7
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
el14afma |
0:5ffb234d134c
|
8
|
* copies of the Software, and to permit persons to whom the Software is
|
el14afma |
0:5ffb234d134c
|
9
|
* furnished to do so, subject to the following conditions:
|
el14afma |
0:5ffb234d134c
|
10
|
*
|
el14afma |
0:5ffb234d134c
|
11
|
* The above copyright notice and this permission notice shall be included in
|
el14afma |
0:5ffb234d134c
|
12
|
* all copies or substantial portions of the Software.
|
el14afma |
0:5ffb234d134c
|
13
|
*
|
el14afma |
0:5ffb234d134c
|
14
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
el14afma |
0:5ffb234d134c
|
15
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
el14afma |
0:5ffb234d134c
|
16
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
el14afma |
0:5ffb234d134c
|
17
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
el14afma |
0:5ffb234d134c
|
18
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
el14afma |
0:5ffb234d134c
|
19
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
el14afma |
0:5ffb234d134c
|
20
|
* SOFTWARE.
|
el14afma |
0:5ffb234d134c
|
21
|
*/
|
el14afma |
0:5ffb234d134c
|
22
|
#include "ff.h"
|
el14afma |
0:5ffb234d134c
|
23
|
#include "ffconf.h"
|
el14afma |
0:5ffb234d134c
|
24
|
#include "mbed_debug.h"
|
el14afma |
0:5ffb234d134c
|
25
|
|
el14afma |
0:5ffb234d134c
|
26
|
#include "FATFileHandle.h"
|
el14afma |
0:5ffb234d134c
|
27
|
|
el14afma |
0:5ffb234d134c
|
28
|
FATFileHandle::FATFileHandle(FIL fh) {
|
el14afma |
0:5ffb234d134c
|
29
|
_fh = fh;
|
el14afma |
0:5ffb234d134c
|
30
|
}
|
el14afma |
0:5ffb234d134c
|
31
|
|
el14afma |
0:5ffb234d134c
|
32
|
int FATFileHandle::close() {
|
el14afma |
0:5ffb234d134c
|
33
|
int retval = f_close(&_fh);
|
el14afma |
0:5ffb234d134c
|
34
|
delete this;
|
el14afma |
0:5ffb234d134c
|
35
|
return retval;
|
el14afma |
0:5ffb234d134c
|
36
|
}
|
el14afma |
0:5ffb234d134c
|
37
|
|
el14afma |
0:5ffb234d134c
|
38
|
ssize_t FATFileHandle::write(const void* buffer, size_t length) {
|
el14afma |
0:5ffb234d134c
|
39
|
UINT n;
|
el14afma |
0:5ffb234d134c
|
40
|
FRESULT res = f_write(&_fh, buffer, length, &n);
|
el14afma |
0:5ffb234d134c
|
41
|
if (res) {
|
el14afma |
0:5ffb234d134c
|
42
|
debug_if(FFS_DBG, "f_write() failed: %d", res);
|
el14afma |
0:5ffb234d134c
|
43
|
return -1;
|
el14afma |
0:5ffb234d134c
|
44
|
}
|
el14afma |
0:5ffb234d134c
|
45
|
return n;
|
el14afma |
0:5ffb234d134c
|
46
|
}
|
el14afma |
0:5ffb234d134c
|
47
|
|
el14afma |
0:5ffb234d134c
|
48
|
ssize_t FATFileHandle::read(void* buffer, size_t length) {
|
el14afma |
0:5ffb234d134c
|
49
|
debug_if(FFS_DBG, "read(%d)\n", length);
|
el14afma |
0:5ffb234d134c
|
50
|
UINT n;
|
el14afma |
0:5ffb234d134c
|
51
|
FRESULT res = f_read(&_fh, buffer, length, &n);
|
el14afma |
0:5ffb234d134c
|
52
|
if (res) {
|
el14afma |
0:5ffb234d134c
|
53
|
debug_if(FFS_DBG, "f_read() failed: %d\n", res);
|
el14afma |
0:5ffb234d134c
|
54
|
return -1;
|
el14afma |
0:5ffb234d134c
|
55
|
}
|
el14afma |
0:5ffb234d134c
|
56
|
return n;
|
el14afma |
0:5ffb234d134c
|
57
|
}
|
el14afma |
0:5ffb234d134c
|
58
|
|
el14afma |
0:5ffb234d134c
|
59
|
int FATFileHandle::isatty() {
|
el14afma |
0:5ffb234d134c
|
60
|
return 0;
|
el14afma |
0:5ffb234d134c
|
61
|
}
|
el14afma |
0:5ffb234d134c
|
62
|
|
el14afma |
0:5ffb234d134c
|
63
|
off_t FATFileHandle::lseek(off_t position, int whence) {
|
el14afma |
0:5ffb234d134c
|
64
|
if (whence == SEEK_END) {
|
el14afma |
0:5ffb234d134c
|
65
|
position += _fh.fsize;
|
el14afma |
0:5ffb234d134c
|
66
|
} else if(whence==SEEK_CUR) {
|
el14afma |
0:5ffb234d134c
|
67
|
position += _fh.fptr;
|
el14afma |
0:5ffb234d134c
|
68
|
}
|
el14afma |
0:5ffb234d134c
|
69
|
FRESULT res = f_lseek(&_fh, position);
|
el14afma |
0:5ffb234d134c
|
70
|
if (res) {
|
el14afma |
0:5ffb234d134c
|
71
|
debug_if(FFS_DBG, "lseek failed: %d\n", res);
|
el14afma |
0:5ffb234d134c
|
72
|
return -1;
|
el14afma |
0:5ffb234d134c
|
73
|
} else {
|
el14afma |
0:5ffb234d134c
|
74
|
debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr);
|
el14afma |
0:5ffb234d134c
|
75
|
return _fh.fptr;
|
el14afma |
0:5ffb234d134c
|
76
|
}
|
el14afma |
0:5ffb234d134c
|
77
|
}
|
el14afma |
0:5ffb234d134c
|
78
|
|
el14afma |
0:5ffb234d134c
|
79
|
int FATFileHandle::fsync() {
|
el14afma |
0:5ffb234d134c
|
80
|
FRESULT res = f_sync(&_fh);
|
el14afma |
0:5ffb234d134c
|
81
|
if (res) {
|
el14afma |
0:5ffb234d134c
|
82
|
debug_if(FFS_DBG, "f_sync() failed: %d\n", res);
|
el14afma |
0:5ffb234d134c
|
83
|
return -1;
|
el14afma |
0:5ffb234d134c
|
84
|
}
|
el14afma |
0:5ffb234d134c
|
85
|
return 0;
|
el14afma |
0:5ffb234d134c
|
86
|
}
|
el14afma |
0:5ffb234d134c
|
87
|
|
el14afma |
0:5ffb234d134c
|
88
|
off_t FATFileHandle::flen() {
|
el14afma |
0:5ffb234d134c
|
89
|
return _fh.fsize;
|
el14afma |
0:5ffb234d134c
|
90
|
}
|