Maxim mbed development library
Dependents: MAX34417_demo MAXREFDES1265 MAXREFDES1265
Fork of mbed-dev by
drivers/FileHandle.h@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
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> |
<> | 149:156823d33999 | 22 | |
<> | 149:156823d33999 | 23 | #if defined(__ARMCC_VERSION) || defined(__ICCARM__) |
<> | 149:156823d33999 | 24 | typedef int ssize_t; |
<> | 149:156823d33999 | 25 | typedef long off_t; |
<> | 149:156823d33999 | 26 | |
<> | 149:156823d33999 | 27 | #else |
<> | 149:156823d33999 | 28 | # include <sys/types.h> |
<> | 149:156823d33999 | 29 | #endif |
<> | 149:156823d33999 | 30 | |
<> | 149:156823d33999 | 31 | namespace mbed { |
<> | 149:156823d33999 | 32 | /** \addtogroup drivers */ |
<> | 149:156823d33999 | 33 | /** @{*/ |
<> | 149:156823d33999 | 34 | |
<> | 149:156823d33999 | 35 | /** An OO equivalent of the internal FILEHANDLE variable |
<> | 149:156823d33999 | 36 | * and associated _sys_* functions. |
<> | 149:156823d33999 | 37 | * |
<> | 149:156823d33999 | 38 | * FileHandle is an abstract class, needing at least sys_write and |
<> | 149:156823d33999 | 39 | * sys_read to be implmented for a simple interactive device. |
<> | 149:156823d33999 | 40 | * |
<> | 149:156823d33999 | 41 | * No one ever directly tals to/instanciates a FileHandle - it gets |
<> | 149:156823d33999 | 42 | * created by FileSystem, and wrapped up by stdio. |
<> | 149:156823d33999 | 43 | * |
<> | 149:156823d33999 | 44 | * @Note Synchronization level: Set by subclass |
<> | 149:156823d33999 | 45 | */ |
<> | 149:156823d33999 | 46 | class FileHandle { |
<> | 149:156823d33999 | 47 | |
<> | 149:156823d33999 | 48 | public: |
<> | 149:156823d33999 | 49 | /** Write the contents of a buffer to the file |
<> | 149:156823d33999 | 50 | * |
<> | 149:156823d33999 | 51 | * @param buffer the buffer to write from |
<> | 149:156823d33999 | 52 | * @param length the number of characters to write |
<> | 149:156823d33999 | 53 | * |
<> | 149:156823d33999 | 54 | * @returns |
<> | 149:156823d33999 | 55 | * The number of characters written (possibly 0) on success, -1 on error. |
<> | 149:156823d33999 | 56 | */ |
<> | 149:156823d33999 | 57 | virtual ssize_t write(const void* buffer, size_t length) = 0; |
<> | 149:156823d33999 | 58 | |
<> | 149:156823d33999 | 59 | /** Close the file |
<> | 149:156823d33999 | 60 | * |
<> | 149:156823d33999 | 61 | * @returns |
<> | 149:156823d33999 | 62 | * Zero on success, -1 on error. |
<> | 149:156823d33999 | 63 | */ |
<> | 149:156823d33999 | 64 | virtual int close() = 0; |
<> | 149:156823d33999 | 65 | |
<> | 149:156823d33999 | 66 | /** Function read |
<> | 149:156823d33999 | 67 | * Reads the contents of the file into a buffer |
<> | 149:156823d33999 | 68 | * |
<> | 149:156823d33999 | 69 | * @param buffer the buffer to read in to |
<> | 149:156823d33999 | 70 | * @param length the number of characters to read |
<> | 149:156823d33999 | 71 | * |
<> | 149:156823d33999 | 72 | * @returns |
<> | 149:156823d33999 | 73 | * The number of characters read (zero at end of file) on success, -1 on error. |
<> | 149:156823d33999 | 74 | */ |
<> | 149:156823d33999 | 75 | virtual ssize_t read(void* buffer, size_t length) = 0; |
<> | 149:156823d33999 | 76 | |
<> | 149:156823d33999 | 77 | /** Check if the handle is for a interactive terminal device. |
<> | 149:156823d33999 | 78 | * If so, line buffered behaviour is used by default |
<> | 149:156823d33999 | 79 | * |
<> | 149:156823d33999 | 80 | * @returns |
<> | 149:156823d33999 | 81 | * 1 if it is a terminal, |
<> | 149:156823d33999 | 82 | * 0 otherwise |
<> | 149:156823d33999 | 83 | */ |
<> | 149:156823d33999 | 84 | virtual int isatty() = 0; |
<> | 149:156823d33999 | 85 | |
<> | 149:156823d33999 | 86 | /** Move the file position to a given offset from a given location. |
<> | 149:156823d33999 | 87 | * |
<> | 149:156823d33999 | 88 | * @param offset The offset from whence to move to |
<> | 149:156823d33999 | 89 | * @param whence SEEK_SET for the start of the file, SEEK_CUR for the |
<> | 149:156823d33999 | 90 | * current file position, or SEEK_END for the end of the file. |
<> | 149:156823d33999 | 91 | * |
<> | 149:156823d33999 | 92 | * @returns |
<> | 149:156823d33999 | 93 | * new file position on success, |
<> | 149:156823d33999 | 94 | * -1 on failure or unsupported |
<> | 149:156823d33999 | 95 | */ |
<> | 149:156823d33999 | 96 | virtual off_t lseek(off_t offset, int whence) = 0; |
<> | 149:156823d33999 | 97 | |
<> | 149:156823d33999 | 98 | /** Flush any buffers associated with the FileHandle, ensuring it |
<> | 149:156823d33999 | 99 | * is up to date on disk |
<> | 149:156823d33999 | 100 | * |
<> | 149:156823d33999 | 101 | * @returns |
<> | 149:156823d33999 | 102 | * 0 on success or un-needed, |
<> | 149:156823d33999 | 103 | * -1 on error |
<> | 149:156823d33999 | 104 | */ |
<> | 149:156823d33999 | 105 | virtual int fsync() = 0; |
<> | 149:156823d33999 | 106 | |
<> | 149:156823d33999 | 107 | virtual off_t flen() { |
<> | 149:156823d33999 | 108 | lock(); |
<> | 149:156823d33999 | 109 | /* remember our current position */ |
<> | 149:156823d33999 | 110 | off_t pos = lseek(0, SEEK_CUR); |
<> | 149:156823d33999 | 111 | if(pos == -1) { |
<> | 149:156823d33999 | 112 | unlock(); |
<> | 149:156823d33999 | 113 | return -1; |
<> | 149:156823d33999 | 114 | } |
<> | 149:156823d33999 | 115 | /* seek to the end to get the file length */ |
<> | 149:156823d33999 | 116 | off_t res = lseek(0, SEEK_END); |
<> | 149:156823d33999 | 117 | /* return to our old position */ |
<> | 149:156823d33999 | 118 | lseek(pos, SEEK_SET); |
<> | 149:156823d33999 | 119 | unlock(); |
<> | 149:156823d33999 | 120 | return res; |
<> | 149:156823d33999 | 121 | } |
<> | 149:156823d33999 | 122 | |
<> | 149:156823d33999 | 123 | virtual ~FileHandle(); |
<> | 149:156823d33999 | 124 | |
<> | 149:156823d33999 | 125 | protected: |
<> | 149:156823d33999 | 126 | |
<> | 149:156823d33999 | 127 | /** Acquire exclusive access to this object. |
<> | 149:156823d33999 | 128 | */ |
<> | 149:156823d33999 | 129 | virtual void lock() { |
<> | 149:156823d33999 | 130 | // Stub |
<> | 149:156823d33999 | 131 | } |
<> | 149:156823d33999 | 132 | |
<> | 149:156823d33999 | 133 | /** Release exclusive access to this object. |
<> | 149:156823d33999 | 134 | */ |
<> | 149:156823d33999 | 135 | virtual void unlock() { |
<> | 149:156823d33999 | 136 | // Stub |
<> | 149:156823d33999 | 137 | } |
<> | 149:156823d33999 | 138 | }; |
<> | 149:156823d33999 | 139 | |
<> | 149:156823d33999 | 140 | } // namespace mbed |
<> | 149:156823d33999 | 141 | |
<> | 149:156823d33999 | 142 | #endif |
<> | 149:156823d33999 | 143 | |
<> | 149:156823d33999 | 144 | /** @}*/ |