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