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