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