SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 09:08:29 2019 +0000
Revision:
0:aa3fc5ad02f7
SPKT

Who changed what in which revision?

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