|
16 | 16 | #ifndef MBED_FILELIKE_H |
17 | 17 | #define MBED_FILELIKE_H |
18 | 18 |
|
| 19 | +#include "platform/toolchain.h" |
19 | 20 | #include "drivers/FileBase.h" |
20 | | -#include "drivers/FileHandle.h" |
21 | 21 |
|
22 | 22 | namespace mbed { |
23 | 23 | /** \addtogroup drivers */ |
24 | 24 | /** @{*/ |
25 | 25 |
|
| 26 | + |
26 | 27 | /* Class FileLike |
27 | 28 | * A file-like object is one that can be opened with fopen by |
28 | | - * fopen("/name", mode). It is intersection of the classes Base and |
29 | | - * FileHandle. |
| 29 | + * fopen("/name", mode). |
30 | 30 | * |
31 | 31 | * @Note Synchronization level: Set by subclass |
32 | 32 | */ |
33 | | -class FileLike : public FileHandle, public FileBase { |
34 | | - |
| 33 | +class FileLike : public FileBase { |
35 | 34 | public: |
36 | | - /* Constructor FileLike |
| 35 | + /** Constructor FileLike |
| 36 | + * |
| 37 | + * @param name The name to use to open the file. |
| 38 | + */ |
| 39 | + FileLike(const char *name = NULL) : FileBase(name, FilePathType) {} |
| 40 | + virtual ~FileLike() {} |
| 41 | + |
| 42 | + /** Read the contents of a file into a buffer |
| 43 | + * |
| 44 | + * @param buffer The buffer to read in to |
| 45 | + * @param size The number of bytes to read |
| 46 | + * @return The number of bytes read, 0 at end of file, negative error on failure |
| 47 | + */ |
| 48 | + virtual ssize_t read(void *buffer, size_t len) = 0; |
| 49 | + |
| 50 | + /** Write the contents of a buffer to a file |
37 | 51 | * |
38 | | - * Variables |
39 | | - * name - The name to use to open the file. |
| 52 | + * @param buffer The buffer to write from |
| 53 | + * @param size The number of bytes to write |
| 54 | + * @return The number of bytes written, negative error on failure |
40 | 55 | */ |
41 | | - FileLike(const char *name); |
| 56 | + virtual ssize_t write(const void *buffer, size_t len) = 0; |
42 | 57 |
|
43 | | - virtual ~FileLike(); |
| 58 | + /** Close a file |
| 59 | + * |
| 60 | + * @return 0 on success, negative error code on failure |
| 61 | + */ |
| 62 | + virtual int close() = 0; |
44 | 63 |
|
| 64 | + /** Flush any buffers associated with the file |
| 65 | + * |
| 66 | + * @return 0 on success, negative error code on failure |
| 67 | + */ |
| 68 | + virtual int sync() = 0; |
| 69 | + |
| 70 | + /** Check if the file in an interactive terminal device |
| 71 | + * |
| 72 | + * @return True if the file is a terminal |
| 73 | + */ |
| 74 | + virtual int isatty() = 0; |
| 75 | + |
| 76 | + /** Move the file position to a given offset from from a given location |
| 77 | + * |
| 78 | + * @param offset The offset from whence to move to |
| 79 | + * @param whence The start of where to seek |
| 80 | + * SEEK_SET to start from beginning of file, |
| 81 | + * SEEK_CUR to start from current position in file, |
| 82 | + * SEEK_END to start from end of file |
| 83 | + * @return The new offset of the file |
| 84 | + */ |
| 85 | + virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0; |
| 86 | + |
| 87 | + /** Get the file position of the file |
| 88 | + * |
| 89 | + * @return The current offset in the file |
| 90 | + */ |
| 91 | + virtual off_t tell() = 0; |
| 92 | + |
| 93 | + /** Rewind the file position to the beginning of the file |
| 94 | + * |
| 95 | + * @note This is equivalent to file_seek(file, 0, FS_SEEK_SET) |
| 96 | + */ |
| 97 | + virtual void rewind() = 0; |
| 98 | + |
| 99 | + /** Get the size of the file |
| 100 | + * |
| 101 | + * @return Size of the file in bytes |
| 102 | + */ |
| 103 | + virtual size_t size() = 0; |
| 104 | + |
| 105 | + /** Move the file position to a given offset from a given location. |
| 106 | + * |
| 107 | + * @param offset The offset from whence to move to |
| 108 | + * @param whence SEEK_SET for the start of the file, SEEK_CUR for the |
| 109 | + * current file position, or SEEK_END for the end of the file. |
| 110 | + * |
| 111 | + * @returns |
| 112 | + * new file position on success, |
| 113 | + * -1 on failure or unsupported |
| 114 | + */ |
| 115 | + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::seek") |
| 116 | + virtual off_t lseek(off_t offset, int whence) { return seek(offset, whence); } |
| 117 | + |
| 118 | + /** Flush any buffers associated with the FileHandle, ensuring it |
| 119 | + * is up to date on disk |
| 120 | + * |
| 121 | + * @returns |
| 122 | + * 0 on success or un-needed, |
| 123 | + * -1 on error |
| 124 | + */ |
| 125 | + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::sync") |
| 126 | + virtual int fsync() { return sync(); } |
| 127 | + |
| 128 | + /** Find the length of the file |
| 129 | + * |
| 130 | + * @returns |
| 131 | + * Length of the file |
| 132 | + */ |
| 133 | + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::size") |
| 134 | + virtual off_t flen() { return size(); } |
| 135 | + |
| 136 | +protected: |
| 137 | + /** Acquire exclusive access to this object. |
| 138 | + */ |
| 139 | + virtual void lock() { |
| 140 | + // Stub |
| 141 | + } |
| 142 | + |
| 143 | + /** Release exclusive access to this object. |
| 144 | + */ |
| 145 | + virtual void unlock() { |
| 146 | + // Stub |
| 147 | + } |
45 | 148 | }; |
46 | 149 |
|
| 150 | + |
| 151 | +/** @}*/ |
47 | 152 | } // namespace mbed |
48 | 153 |
|
49 | 154 | #endif |
50 | | - |
51 | | -/** @}*/ |
|
0 commit comments