CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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