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 #ifndef MBED_FILEHANDLE_H
17 #define MBED_FILEHANDLE_H
19 typedef int FILEHANDLE;
23 #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
28 # include <sys/types.h>
33 /** An OO equivalent of the internal FILEHANDLE variable
34 * and associated _sys_* functions.
36 * FileHandle is an abstract class, needing at least sys_write and
37 * sys_read to be implmented for a simple interactive device.
39 * No one ever directly tals to/instanciates a FileHandle - it gets
40 * created by FileSystem, and wrapped up by stdio.
45 /** Write the contents of a buffer to the file
47 * @param buffer the buffer to write from
48 * @param length the number of characters to write
51 * The number of characters written (possibly 0) on success, -1 on error.
53 virtual ssize_t write(const void* buffer, size_t length) = 0;
58 * Zero on success, -1 on error.
60 virtual int close() = 0;
63 * Reads the contents of the file into a buffer
65 * @param buffer the buffer to read in to
66 * @param length the number of characters to read
69 * The number of characters read (zero at end of file) on success, -1 on error.
71 virtual ssize_t read(void* buffer, size_t length) = 0;
73 /** Check if the handle is for a interactive terminal device.
74 * If so, line buffered behaviour is used by default
77 * 1 if it is a terminal,
80 virtual int isatty() = 0;
82 /** Move the file position to a given offset from a given location.
84 * @param offset The offset from whence to move to
85 * @param whence SEEK_SET for the start of the file, SEEK_CUR for the
86 * current file position, or SEEK_END for the end of the file.
89 * new file position on success,
90 * -1 on failure or unsupported
92 virtual off_t lseek(off_t offset, int whence) = 0;
94 /** Flush any buffers associated with the FileHandle, ensuring it
95 * is up to date on disk
98 * 0 on success or un-needed,
101 virtual int fsync() = 0;
103 virtual off_t flen() {
104 /* remember our current position */
105 off_t pos = lseek(0, SEEK_CUR);
106 if(pos == -1) return -1;
107 /* seek to the end to get the file length */
108 off_t res = lseek(0, SEEK_END);
109 /* return to our old position */
110 lseek(pos, SEEK_SET);
114 virtual ~FileHandle();