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