mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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