]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/common/LocalFileSystem.cpp
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "LocalFileSystem.h"
18 #if DEVICE_LOCALFILESYSTEM
20 #include "semihost_api.h"
26 /* Extension to FINFO type defined in RTL.h (in Keil RL) - adds 'create time'. */
28 unsigned char hr
; /* Hours [0..23] */
29 unsigned char min
; /* Minutes [0..59] */
30 unsigned char sec
; /* Seconds [0..59] */
31 unsigned char day
; /* Day [1..31] */
32 unsigned char mon
; /* Month [1..12] */
33 unsigned short year
; /* Year [1980..2107] */
36 typedef struct { /* File Search info record */
37 char name
[32]; /* File name */
38 long size
; /* File size in bytes */
39 int fileID
; /* System File Identification */
40 FTIME create_time
; /* Date & time file was created */
41 FTIME write_time
; /* Date & time of last write */
44 #define RESERVED_FOR_USER_APPLICATIONS (0x100) /* 0x100 - 0x1ff */
45 #define USR_XFFIND (RESERVED_FOR_USER_APPLICATIONS + 0)
47 static int xffind (const char *pattern
, XFINFO
*info
) {
50 param
[0] = (unsigned long)pattern
;
51 param
[1] = (unsigned long)strlen(pattern
);
52 param
[2] = (unsigned long)info
;
53 param
[3] = (unsigned long)sizeof(XFINFO
);
55 return __semihost(USR_XFFIND
, param
);
63 #define OPEN_INVALID -1
65 int posix_to_semihost_open_flags(int flags
) {
66 /* POSIX flags -> semihosting open mode */
71 if (flags
& O_APPEND
) {
73 } else if (flags
& O_TRUNC
) {
78 } else if (flags
& O_WRONLY
) {
80 if (flags
& O_APPEND
) {
85 } else if (flags
== O_RDONLY
) {
90 openmode
= OPEN_INVALID
;
96 FILEHANDLE
local_file_open(const char* name
, int flags
) {
97 int openmode
= posix_to_semihost_open_flags(flags
);
98 if (openmode
== OPEN_INVALID
) {
99 return (FILEHANDLE
)NULL
;
102 FILEHANDLE fh
= semihost_open(name
, openmode
);
104 return (FILEHANDLE
)NULL
;
110 LocalFileHandle::LocalFileHandle(FILEHANDLE fh
) : _fh(fh
), pos(0) {
113 int LocalFileHandle::close() {
114 int retval
= semihost_close(_fh
);
119 ssize_t
LocalFileHandle::write(const void *buffer
, size_t length
) {
120 ssize_t n
= semihost_write(_fh
, (const unsigned char*)buffer
, length
, 0); // number of characters not written
121 n
= length
- n
; // number of characters written
126 ssize_t
LocalFileHandle::read(void *buffer
, size_t length
) {
127 ssize_t n
= semihost_read(_fh
, (unsigned char*)buffer
, length
, 0); // number of characters not read
128 n
= length
- n
; // number of characters read
133 int LocalFileHandle::isatty() {
134 return semihost_istty(_fh
);
137 off_t
LocalFileHandle::lseek(off_t position
, int whence
) {
138 if (whence
== SEEK_CUR
) {
140 } else if (whence
== SEEK_END
) {
141 position
+= semihost_flen(_fh
);
142 } /* otherwise SEEK_SET, so position is fine */
144 /* Always seems to return -1, so just ignore for now. */
145 semihost_seek(_fh
, position
);
150 int LocalFileHandle::fsync() {
151 return semihost_ensure(_fh
);
154 off_t
LocalFileHandle::flen() {
155 return semihost_flen(_fh
);
158 class LocalDirHandle
: public DirHandle
{
161 struct dirent cur_entry
;
164 LocalDirHandle() : cur_entry(), info() {
167 virtual int closedir() {
172 virtual struct dirent
*readdir() {
173 if (xffind("*", &info
)!=0) {
176 memcpy(cur_entry
.d_name
, info
.name
, sizeof(info
.name
));
180 virtual void rewinddir() {
184 virtual off_t
telldir() {
188 virtual void seekdir(off_t offset
) {
189 info
.fileID
= offset
;
193 FileHandle
*LocalFileSystem::open(const char* name
, int flags
) {
194 /* reject filenames with / in them */
195 for (const char *tmp
= name
; *tmp
; tmp
++) {
201 int openmode
= posix_to_semihost_open_flags(flags
);
202 if (openmode
== OPEN_INVALID
) {
206 FILEHANDLE fh
= semihost_open(name
, openmode
);
210 return new LocalFileHandle(fh
);
213 int LocalFileSystem::remove(const char *filename
) {
214 return semihost_remove(filename
);
217 DirHandle
*LocalFileSystem::opendir(const char *name
) {
218 return new LocalDirHandle();