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