strat des robots

Fork of CRAC-Strat_2017 by CRAC Team

Committer:
ClementBreteau
Date:
Fri May 19 17:14:07 2017 +0000
Revision:
17:d1594579eec6
Parent:
0:ad97421fb1fb
strat du robot, 19-05-2017, 19h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
antbig 0:ad97421fb1fb 1 /* mbed Microcontroller Library - FileHandler
antbig 0:ad97421fb1fb 2 * Copyright (c) 2007-2009 ARM Limited. All rights reserved.
antbig 0:ad97421fb1fb 3 */
antbig 0:ad97421fb1fb 4
antbig 0:ad97421fb1fb 5 #ifndef MBED_FILEHANDLE_H
antbig 0:ad97421fb1fb 6 #define MBED_FILEHANDLE_H
antbig 0:ad97421fb1fb 7
antbig 0:ad97421fb1fb 8 typedef int FILEHANDLE;
antbig 0:ad97421fb1fb 9
antbig 0:ad97421fb1fb 10 #include <stdio.h>
antbig 0:ad97421fb1fb 11 #ifdef __ARMCC_VERSION
antbig 0:ad97421fb1fb 12 typedef int ssize_t;
antbig 0:ad97421fb1fb 13 typedef long off_t;
antbig 0:ad97421fb1fb 14 #else
antbig 0:ad97421fb1fb 15 #include <sys/types.h>
antbig 0:ad97421fb1fb 16 #endif
antbig 0:ad97421fb1fb 17
antbig 0:ad97421fb1fb 18 namespace mbed {
antbig 0:ad97421fb1fb 19
antbig 0:ad97421fb1fb 20 /* Class FileHandle
antbig 0:ad97421fb1fb 21 * An OO equivalent of the internal FILEHANDLE variable
antbig 0:ad97421fb1fb 22 * and associated _sys_* functions
antbig 0:ad97421fb1fb 23 *
antbig 0:ad97421fb1fb 24 * FileHandle is an abstract class, needing at least sys_write and
antbig 0:ad97421fb1fb 25 * sys_read to be implmented for a simple interactive device
antbig 0:ad97421fb1fb 26 *
antbig 0:ad97421fb1fb 27 * No one ever directly tals to/instanciates a FileHandle - it gets
antbig 0:ad97421fb1fb 28 * created by FileSystem, and wrapped up by stdio
antbig 0:ad97421fb1fb 29 */
antbig 0:ad97421fb1fb 30 class FileHandle {
antbig 0:ad97421fb1fb 31
antbig 0:ad97421fb1fb 32 public:
antbig 0:ad97421fb1fb 33
antbig 0:ad97421fb1fb 34 /* Function write
antbig 0:ad97421fb1fb 35 * Write the contents of a buffer to the file
antbig 0:ad97421fb1fb 36 *
antbig 0:ad97421fb1fb 37 * Parameters
antbig 0:ad97421fb1fb 38 * buffer - the buffer to write from
antbig 0:ad97421fb1fb 39 * length - the number of characters to write
antbig 0:ad97421fb1fb 40 *
antbig 0:ad97421fb1fb 41 * Returns
antbig 0:ad97421fb1fb 42 * The number of characters written (possibly 0) on success, -1 on error.
antbig 0:ad97421fb1fb 43 */
antbig 0:ad97421fb1fb 44 virtual ssize_t write(const void* buffer, size_t length) = 0;
antbig 0:ad97421fb1fb 45
antbig 0:ad97421fb1fb 46 /* Function close
antbig 0:ad97421fb1fb 47 * Close the file
antbig 0:ad97421fb1fb 48 *
antbig 0:ad97421fb1fb 49 * Returns
antbig 0:ad97421fb1fb 50 * Zero on success, -1 on error.
antbig 0:ad97421fb1fb 51 */
antbig 0:ad97421fb1fb 52 virtual int close() = 0;
antbig 0:ad97421fb1fb 53
antbig 0:ad97421fb1fb 54 /* Function read
antbig 0:ad97421fb1fb 55 * Reads the contents of the file into a buffer
antbig 0:ad97421fb1fb 56 *
antbig 0:ad97421fb1fb 57 * Parameters
antbig 0:ad97421fb1fb 58 * buffer - the buffer to read in to
antbig 0:ad97421fb1fb 59 * length - the number of characters to read
antbig 0:ad97421fb1fb 60 *
antbig 0:ad97421fb1fb 61 * Returns
antbig 0:ad97421fb1fb 62 * The number of characters read (zero at end of file) on success, -1 on error.
antbig 0:ad97421fb1fb 63 */
antbig 0:ad97421fb1fb 64 virtual ssize_t read(void* buffer, size_t length) = 0;
antbig 0:ad97421fb1fb 65
antbig 0:ad97421fb1fb 66 /* Function isatty
antbig 0:ad97421fb1fb 67 * Check if the handle is for a interactive terminal device
antbig 0:ad97421fb1fb 68 *
antbig 0:ad97421fb1fb 69 * If so, line buffered behaviour is used by default
antbig 0:ad97421fb1fb 70 *
antbig 0:ad97421fb1fb 71 * Returns
antbig 0:ad97421fb1fb 72 * 1 if it is a terminal, 0 otherwise
antbig 0:ad97421fb1fb 73 */
antbig 0:ad97421fb1fb 74 virtual int isatty() = 0 ;
antbig 0:ad97421fb1fb 75
antbig 0:ad97421fb1fb 76 /* Function lseek
antbig 0:ad97421fb1fb 77 * Move the file position to a given offset from a given location.
antbig 0:ad97421fb1fb 78 *
antbig 0:ad97421fb1fb 79 * Parameters
antbig 0:ad97421fb1fb 80 * offset - The offset from whence to move to
antbig 0:ad97421fb1fb 81 * whence - SEEK_SET for the start of the file, SEEK_CUR for the
antbig 0:ad97421fb1fb 82 * current file position, or SEEK_END for the end of the file.
antbig 0:ad97421fb1fb 83 *
antbig 0:ad97421fb1fb 84 * Returns
antbig 0:ad97421fb1fb 85 * New file position on success, -1 on failure or unsupported
antbig 0:ad97421fb1fb 86 */
antbig 0:ad97421fb1fb 87 virtual off_t lseek(off_t offset, int whence) = 0;
antbig 0:ad97421fb1fb 88
antbig 0:ad97421fb1fb 89 /* Function fsync
antbig 0:ad97421fb1fb 90 * Flush any buffers associated with the FileHandle, ensuring it
antbig 0:ad97421fb1fb 91 * is up to date on disk
antbig 0:ad97421fb1fb 92 *
antbig 0:ad97421fb1fb 93 * Returns
antbig 0:ad97421fb1fb 94 * 0 on success or un-needed, -1 on error
antbig 0:ad97421fb1fb 95 */
antbig 0:ad97421fb1fb 96 virtual int fsync() = 0;
antbig 0:ad97421fb1fb 97
antbig 0:ad97421fb1fb 98 virtual off_t flen() {
antbig 0:ad97421fb1fb 99 /* remember our current position */
antbig 0:ad97421fb1fb 100 off_t pos = lseek(0, SEEK_CUR);
antbig 0:ad97421fb1fb 101 if(pos == -1) return -1;
antbig 0:ad97421fb1fb 102 /* seek to the end to get the file length */
antbig 0:ad97421fb1fb 103 off_t res = lseek(0, SEEK_END);
antbig 0:ad97421fb1fb 104 /* return to our old position */
antbig 0:ad97421fb1fb 105 lseek(pos, SEEK_SET);
antbig 0:ad97421fb1fb 106 return res;
antbig 0:ad97421fb1fb 107 }
antbig 0:ad97421fb1fb 108
antbig 0:ad97421fb1fb 109 };
antbig 0:ad97421fb1fb 110
antbig 0:ad97421fb1fb 111 } // namespace mbed
antbig 0:ad97421fb1fb 112
antbig 0:ad97421fb1fb 113 #endif
antbig 0:ad97421fb1fb 114