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