Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

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