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