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