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