TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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