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