mbed library sources. Supersedes mbed-src. GR-PEACH runs on RAM.
Fork of mbed-dev by
api/FileHandle.h@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 01 15:25:22 2015 +0300
- Revision:
- 0:9b334a45a8ff
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 0:9b334a45a8ff | 3 | * |
bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:9b334a45a8ff | 9 | * |
bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. |
bogdanm | 0:9b334a45a8ff | 15 | */ |
bogdanm | 0:9b334a45a8ff | 16 | #ifndef MBED_FILEHANDLE_H |
bogdanm | 0:9b334a45a8ff | 17 | #define MBED_FILEHANDLE_H |
bogdanm | 0:9b334a45a8ff | 18 | |
bogdanm | 0:9b334a45a8ff | 19 | typedef int FILEHANDLE; |
bogdanm | 0:9b334a45a8ff | 20 | |
bogdanm | 0:9b334a45a8ff | 21 | #include <stdio.h> |
bogdanm | 0:9b334a45a8ff | 22 | |
bogdanm | 0:9b334a45a8ff | 23 | #if defined(__ARMCC_VERSION) || defined(__ICCARM__) |
bogdanm | 0:9b334a45a8ff | 24 | typedef int ssize_t; |
bogdanm | 0:9b334a45a8ff | 25 | typedef long off_t; |
bogdanm | 0:9b334a45a8ff | 26 | |
bogdanm | 0:9b334a45a8ff | 27 | #else |
bogdanm | 0:9b334a45a8ff | 28 | # include <sys/types.h> |
bogdanm | 0:9b334a45a8ff | 29 | #endif |
bogdanm | 0:9b334a45a8ff | 30 | |
bogdanm | 0:9b334a45a8ff | 31 | namespace mbed { |
bogdanm | 0:9b334a45a8ff | 32 | |
bogdanm | 0:9b334a45a8ff | 33 | /** An OO equivalent of the internal FILEHANDLE variable |
bogdanm | 0:9b334a45a8ff | 34 | * and associated _sys_* functions. |
bogdanm | 0:9b334a45a8ff | 35 | * |
bogdanm | 0:9b334a45a8ff | 36 | * FileHandle is an abstract class, needing at least sys_write and |
bogdanm | 0:9b334a45a8ff | 37 | * sys_read to be implmented for a simple interactive device. |
bogdanm | 0:9b334a45a8ff | 38 | * |
bogdanm | 0:9b334a45a8ff | 39 | * No one ever directly tals to/instanciates a FileHandle - it gets |
bogdanm | 0:9b334a45a8ff | 40 | * created by FileSystem, and wrapped up by stdio. |
bogdanm | 0:9b334a45a8ff | 41 | */ |
bogdanm | 0:9b334a45a8ff | 42 | class FileHandle { |
bogdanm | 0:9b334a45a8ff | 43 | |
bogdanm | 0:9b334a45a8ff | 44 | public: |
bogdanm | 0:9b334a45a8ff | 45 | /** Write the contents of a buffer to the file |
bogdanm | 0:9b334a45a8ff | 46 | * |
bogdanm | 0:9b334a45a8ff | 47 | * @param buffer the buffer to write from |
bogdanm | 0:9b334a45a8ff | 48 | * @param length the number of characters to write |
bogdanm | 0:9b334a45a8ff | 49 | * |
bogdanm | 0:9b334a45a8ff | 50 | * @returns |
bogdanm | 0:9b334a45a8ff | 51 | * The number of characters written (possibly 0) on success, -1 on error. |
bogdanm | 0:9b334a45a8ff | 52 | */ |
bogdanm | 0:9b334a45a8ff | 53 | virtual ssize_t write(const void* buffer, size_t length) = 0; |
bogdanm | 0:9b334a45a8ff | 54 | |
bogdanm | 0:9b334a45a8ff | 55 | /** Close the file |
bogdanm | 0:9b334a45a8ff | 56 | * |
bogdanm | 0:9b334a45a8ff | 57 | * @returns |
bogdanm | 0:9b334a45a8ff | 58 | * Zero on success, -1 on error. |
bogdanm | 0:9b334a45a8ff | 59 | */ |
bogdanm | 0:9b334a45a8ff | 60 | virtual int close() = 0; |
bogdanm | 0:9b334a45a8ff | 61 | |
bogdanm | 0:9b334a45a8ff | 62 | /** Function read |
bogdanm | 0:9b334a45a8ff | 63 | * Reads the contents of the file into a buffer |
bogdanm | 0:9b334a45a8ff | 64 | * |
bogdanm | 0:9b334a45a8ff | 65 | * @param buffer the buffer to read in to |
bogdanm | 0:9b334a45a8ff | 66 | * @param length the number of characters to read |
bogdanm | 0:9b334a45a8ff | 67 | * |
bogdanm | 0:9b334a45a8ff | 68 | * @returns |
bogdanm | 0:9b334a45a8ff | 69 | * The number of characters read (zero at end of file) on success, -1 on error. |
bogdanm | 0:9b334a45a8ff | 70 | */ |
bogdanm | 0:9b334a45a8ff | 71 | virtual ssize_t read(void* buffer, size_t length) = 0; |
bogdanm | 0:9b334a45a8ff | 72 | |
bogdanm | 0:9b334a45a8ff | 73 | /** Check if the handle is for a interactive terminal device. |
bogdanm | 0:9b334a45a8ff | 74 | * If so, line buffered behaviour is used by default |
bogdanm | 0:9b334a45a8ff | 75 | * |
bogdanm | 0:9b334a45a8ff | 76 | * @returns |
bogdanm | 0:9b334a45a8ff | 77 | * 1 if it is a terminal, |
bogdanm | 0:9b334a45a8ff | 78 | * 0 otherwise |
bogdanm | 0:9b334a45a8ff | 79 | */ |
bogdanm | 0:9b334a45a8ff | 80 | virtual int isatty() = 0; |
bogdanm | 0:9b334a45a8ff | 81 | |
bogdanm | 0:9b334a45a8ff | 82 | /** Move the file position to a given offset from a given location. |
bogdanm | 0:9b334a45a8ff | 83 | * |
bogdanm | 0:9b334a45a8ff | 84 | * @param offset The offset from whence to move to |
bogdanm | 0:9b334a45a8ff | 85 | * @param whence SEEK_SET for the start of the file, SEEK_CUR for the |
bogdanm | 0:9b334a45a8ff | 86 | * current file position, or SEEK_END for the end of the file. |
bogdanm | 0:9b334a45a8ff | 87 | * |
bogdanm | 0:9b334a45a8ff | 88 | * @returns |
bogdanm | 0:9b334a45a8ff | 89 | * new file position on success, |
bogdanm | 0:9b334a45a8ff | 90 | * -1 on failure or unsupported |
bogdanm | 0:9b334a45a8ff | 91 | */ |
bogdanm | 0:9b334a45a8ff | 92 | virtual off_t lseek(off_t offset, int whence) = 0; |
bogdanm | 0:9b334a45a8ff | 93 | |
bogdanm | 0:9b334a45a8ff | 94 | /** Flush any buffers associated with the FileHandle, ensuring it |
bogdanm | 0:9b334a45a8ff | 95 | * is up to date on disk |
bogdanm | 0:9b334a45a8ff | 96 | * |
bogdanm | 0:9b334a45a8ff | 97 | * @returns |
bogdanm | 0:9b334a45a8ff | 98 | * 0 on success or un-needed, |
bogdanm | 0:9b334a45a8ff | 99 | * -1 on error |
bogdanm | 0:9b334a45a8ff | 100 | */ |
bogdanm | 0:9b334a45a8ff | 101 | virtual int fsync() = 0; |
bogdanm | 0:9b334a45a8ff | 102 | |
bogdanm | 0:9b334a45a8ff | 103 | virtual off_t flen() { |
bogdanm | 0:9b334a45a8ff | 104 | /* remember our current position */ |
bogdanm | 0:9b334a45a8ff | 105 | off_t pos = lseek(0, SEEK_CUR); |
bogdanm | 0:9b334a45a8ff | 106 | if(pos == -1) return -1; |
bogdanm | 0:9b334a45a8ff | 107 | /* seek to the end to get the file length */ |
bogdanm | 0:9b334a45a8ff | 108 | off_t res = lseek(0, SEEK_END); |
bogdanm | 0:9b334a45a8ff | 109 | /* return to our old position */ |
bogdanm | 0:9b334a45a8ff | 110 | lseek(pos, SEEK_SET); |
bogdanm | 0:9b334a45a8ff | 111 | return res; |
bogdanm | 0:9b334a45a8ff | 112 | } |
bogdanm | 0:9b334a45a8ff | 113 | |
bogdanm | 0:9b334a45a8ff | 114 | virtual ~FileHandle(); |
bogdanm | 0:9b334a45a8ff | 115 | }; |
bogdanm | 0:9b334a45a8ff | 116 | |
bogdanm | 0:9b334a45a8ff | 117 | } // namespace mbed |
bogdanm | 0:9b334a45a8ff | 118 | |
bogdanm | 0:9b334a45a8ff | 119 | #endif |