mbed libraries for KL25Z
FileHandle.h@8:c14af7958ef5, 2012-11-09 (annotated)
- Committer:
- emilmont
- Date:
- Fri Nov 09 11:33:53 2012 +0000
- Revision:
- 8:c14af7958ef5
- Parent:
- 0:8024c367e29f
- Child:
- 9:663789d7729f
SPI driver; ADC driver; DAC driver; microlib support; general bugfixing
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 | 8:c14af7958ef5 | 19 | /** An OO equivalent of the internal FILEHANDLE variable |
emilmont | 8:c14af7958ef5 | 20 | * and associated _sys_* functions. |
emilmont | 0:8024c367e29f | 21 | * |
emilmont | 0:8024c367e29f | 22 | * FileHandle is an abstract class, needing at least sys_write and |
emilmont | 8:c14af7958ef5 | 23 | * sys_read to be implmented for a simple interactive device. |
emilmont | 0:8024c367e29f | 24 | * |
emilmont | 0:8024c367e29f | 25 | * No one ever directly tals to/instanciates a FileHandle - it gets |
emilmont | 8:c14af7958ef5 | 26 | * created by FileSystem, and wrapped up by stdio. |
emilmont | 0:8024c367e29f | 27 | */ |
emilmont | 0:8024c367e29f | 28 | class FileHandle { |
emilmont | 0:8024c367e29f | 29 | |
emilmont | 0:8024c367e29f | 30 | public: |
emilmont | 8:c14af7958ef5 | 31 | /** Write the contents of a buffer to the file |
emilmont | 0:8024c367e29f | 32 | * |
emilmont | 8:c14af7958ef5 | 33 | * @param buffer the buffer to write from |
emilmont | 8:c14af7958ef5 | 34 | * @param length the number of characters to write |
emilmont | 0:8024c367e29f | 35 | * |
emilmont | 8:c14af7958ef5 | 36 | * @returns |
emilmont | 0:8024c367e29f | 37 | * The number of characters written (possibly 0) on success, -1 on error. |
emilmont | 0:8024c367e29f | 38 | */ |
emilmont | 0:8024c367e29f | 39 | virtual ssize_t write(const void* buffer, size_t length) = 0; |
emilmont | 0:8024c367e29f | 40 | |
emilmont | 8:c14af7958ef5 | 41 | /** Close the file |
emilmont | 0:8024c367e29f | 42 | * |
emilmont | 8:c14af7958ef5 | 43 | * @returns |
emilmont | 0:8024c367e29f | 44 | * Zero on success, -1 on error. |
emilmont | 0:8024c367e29f | 45 | */ |
emilmont | 0:8024c367e29f | 46 | virtual int close() = 0; |
emilmont | 0:8024c367e29f | 47 | |
emilmont | 8:c14af7958ef5 | 48 | /** Function read |
emilmont | 0:8024c367e29f | 49 | * Reads the contents of the file into a buffer |
emilmont | 0:8024c367e29f | 50 | * |
emilmont | 8:c14af7958ef5 | 51 | * @param buffer the buffer to read in to |
emilmont | 8:c14af7958ef5 | 52 | * @param length the number of characters to read |
emilmont | 0:8024c367e29f | 53 | * |
emilmont | 8:c14af7958ef5 | 54 | * @returns |
emilmont | 0:8024c367e29f | 55 | * The number of characters read (zero at end of file) on success, -1 on error. |
emilmont | 0:8024c367e29f | 56 | */ |
emilmont | 0:8024c367e29f | 57 | virtual ssize_t read(void* buffer, size_t length) = 0; |
emilmont | 0:8024c367e29f | 58 | |
emilmont | 8:c14af7958ef5 | 59 | /** Check if the handle is for a interactive terminal device. |
emilmont | 0:8024c367e29f | 60 | * If so, line buffered behaviour is used by default |
emilmont | 0:8024c367e29f | 61 | * |
emilmont | 8:c14af7958ef5 | 62 | * @returns |
emilmont | 8:c14af7958ef5 | 63 | * 1 if it is a terminal, |
emilmont | 8:c14af7958ef5 | 64 | * 0 otherwise |
emilmont | 0:8024c367e29f | 65 | */ |
emilmont | 8:c14af7958ef5 | 66 | virtual int isatty() = 0; |
emilmont | 0:8024c367e29f | 67 | |
emilmont | 8:c14af7958ef5 | 68 | /** Move the file position to a given offset from a given location. |
emilmont | 0:8024c367e29f | 69 | * |
emilmont | 8:c14af7958ef5 | 70 | * @param offset The offset from whence to move to |
emilmont | 8:c14af7958ef5 | 71 | * @param whence SEEK_SET for the start of the file, SEEK_CUR for the |
emilmont | 0:8024c367e29f | 72 | * current file position, or SEEK_END for the end of the file. |
emilmont | 0:8024c367e29f | 73 | * |
emilmont | 8:c14af7958ef5 | 74 | * @returns |
emilmont | 8:c14af7958ef5 | 75 | * new file position on success, |
emilmont | 8:c14af7958ef5 | 76 | * -1 on failure or unsupported |
emilmont | 0:8024c367e29f | 77 | */ |
emilmont | 0:8024c367e29f | 78 | virtual off_t lseek(off_t offset, int whence) = 0; |
emilmont | 0:8024c367e29f | 79 | |
emilmont | 8:c14af7958ef5 | 80 | /** Flush any buffers associated with the FileHandle, ensuring it |
emilmont | 0:8024c367e29f | 81 | * is up to date on disk |
emilmont | 0:8024c367e29f | 82 | * |
emilmont | 8:c14af7958ef5 | 83 | * @returns |
emilmont | 8:c14af7958ef5 | 84 | * 0 on success or un-needed, |
emilmont | 8:c14af7958ef5 | 85 | * -1 on error |
emilmont | 0:8024c367e29f | 86 | */ |
emilmont | 0:8024c367e29f | 87 | virtual int fsync() = 0; |
emilmont | 0:8024c367e29f | 88 | |
emilmont | 0:8024c367e29f | 89 | virtual off_t flen() { |
emilmont | 0:8024c367e29f | 90 | /* remember our current position */ |
emilmont | 0:8024c367e29f | 91 | off_t pos = lseek(0, SEEK_CUR); |
emilmont | 0:8024c367e29f | 92 | if(pos == -1) return -1; |
emilmont | 0:8024c367e29f | 93 | /* seek to the end to get the file length */ |
emilmont | 0:8024c367e29f | 94 | off_t res = lseek(0, SEEK_END); |
emilmont | 0:8024c367e29f | 95 | /* return to our old position */ |
emilmont | 0:8024c367e29f | 96 | lseek(pos, SEEK_SET); |
emilmont | 0:8024c367e29f | 97 | return res; |
emilmont | 0:8024c367e29f | 98 | } |
emilmont | 8:c14af7958ef5 | 99 | |
emilmont | 8:c14af7958ef5 | 100 | virtual ~FileHandle(); |
emilmont | 0:8024c367e29f | 101 | }; |
emilmont | 0:8024c367e29f | 102 | |
emilmont | 0:8024c367e29f | 103 | } // namespace mbed |
emilmont | 0:8024c367e29f | 104 | |
emilmont | 0:8024c367e29f | 105 | #endif |