SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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