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