,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

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