Dataloger

Committer:
jhon309
Date:
Thu Aug 20 00:37:14 2015 +0000
Revision:
0:666850d06c9f
ok

Who changed what in which revision?

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