@@ -25,62 +25,97 @@ namespace mbed {
25
25
/* * \addtogroup drivers */
26
26
/* * @{*/
27
27
28
- /* * An OO equivalent of the internal FILEHANDLE variable
29
- * and associated _sys_* functions.
30
- *
31
- * FileHandle is an abstract class, needing at least sys_write and
32
- * sys_read to be implmented for a simple interactive device.
28
+
29
+ /* * Class FileHandle
33
30
*
34
- * No one ever directly tals to/instanciates a FileHandle - it gets
35
- * created by FileSystem, and wrapped up by stdio.
31
+ * An abstract interface that represents operations on a file-like
32
+ * object. The core functions are read, write, and seek, but only
33
+ * a subset of these operations can be provided.
36
34
*
37
- * @Note Synchronization level: Set by subclass
35
+ * @note to create a file, @see File
36
+ * @note Synchronization level: Set by subclass
38
37
*/
39
38
class FileHandle {
40
-
41
39
public:
42
- MBED_DEPRECATED_SINCE (" mbed-os-5.4" ,
43
- " The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
44
- " Replaced by File" )
45
- FileHandle () {}
40
+ virtual ~FileHandle () {}
46
41
47
- /* * Write the contents of a buffer to the file
42
+ /* * Read the contents of a file into a buffer
48
43
*
49
- * @param buffer the buffer to write from
50
- * @param length the number of characters to write
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
51
51
*
52
- * @returns
53
- * The number of characters written (possibly 0) on success, -1 on error.
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
54
55
*/
55
- virtual ssize_t write (const void * buffer, size_t length ) = 0;
56
+ virtual ssize_t write (const void * buffer, size_t len ) = 0;
56
57
57
- /* * Close the file
58
+ /* * Close a file
58
59
*
59
- * @returns
60
- * Zero on success, -1 on error.
60
+ * @return 0 on success, negative error code on failure
61
61
*/
62
62
virtual int close () = 0;
63
63
64
- /* * Function read
65
- * Reads the contents of the file into a buffer
66
- *
67
- * @param buffer the buffer to read in to
68
- * @param length the number of characters to read
64
+ /* * Flush any buffers associated with the file
69
65
*
70
- * @returns
71
- * The number of characters read (zero at end of file) on success, -1 on error.
66
+ * @return 0 on success, negative error code on failure
72
67
*/
73
- virtual ssize_t read ( void * buffer, size_t length ) = 0;
68
+ virtual int sync ( ) = 0;
74
69
75
- /* * Check if the handle is for a interactive terminal device.
76
- * If so, line buffered behaviour is used by default
70
+ /* * Check if the file in an interactive terminal device
77
71
*
78
- * @returns
79
- * 1 if it is a terminal,
80
- * 0 otherwise
72
+ * @return True if the file is a terminal
81
73
*/
82
74
virtual int isatty () = 0;
83
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
+ * @note This is equivalent to seek(0, SEEK_CUR)
90
+ *
91
+ * @return The current offset in the file
92
+ */
93
+ virtual off_t tell ()
94
+ {
95
+ return seek (0 , SEEK_CUR);
96
+ }
97
+
98
+ /* * Rewind the file position to the beginning of the file
99
+ *
100
+ * @note This is equivalent to seek(0, SEEK_SET)
101
+ */
102
+ virtual void rewind ()
103
+ {
104
+ seek (0 , SEEK_SET);
105
+ }
106
+
107
+ /* * Get the size of the file
108
+ *
109
+ * @return Size of the file in bytes
110
+ */
111
+ virtual size_t size ()
112
+ {
113
+ off_t off = tell ();
114
+ size_t size = seek (0 , SEEK_END);
115
+ seek (off, SEEK_SET);
116
+ return size;
117
+ }
118
+
84
119
/* * Move the file position to a given offset from a given location.
85
120
*
86
121
* @param offset The offset from whence to move to
@@ -91,7 +126,8 @@ class FileHandle {
91
126
* new file position on success,
92
127
* -1 on failure or unsupported
93
128
*/
94
- virtual off_t lseek (off_t offset, int whence) = 0;
129
+ MBED_DEPRECATED_SINCE (" mbed-os-5.4" , " Replaced by FileHandle::seek" )
130
+ virtual off_t lseek (off_t offset, int whence) { return seek (offset, whence); }
95
131
96
132
/* * Flush any buffers associated with the FileHandle, ensuring it
97
133
* is up to date on disk
@@ -100,43 +136,20 @@ class FileHandle {
100
136
* 0 on success or un-needed,
101
137
* -1 on error
102
138
*/
103
- virtual int fsync () = 0;
104
-
105
- virtual off_t flen () {
106
- lock ();
107
- /* remember our current position */
108
- off_t pos = lseek (0 , SEEK_CUR);
109
- if (pos == -1 ) {
110
- unlock ();
111
- return -1 ;
112
- }
113
- /* seek to the end to get the file length */
114
- off_t res = lseek (0 , SEEK_END);
115
- /* return to our old position */
116
- lseek (pos, SEEK_SET);
117
- unlock ();
118
- return res;
119
- }
120
-
121
- virtual ~FileHandle () {};
139
+ MBED_DEPRECATED_SINCE (" mbed-os-5.4" , " Replaced by FileHandle::sync" )
140
+ virtual int fsync () { return sync (); }
122
141
123
- protected:
124
-
125
- /* * Acquire exclusive access to this object.
126
- */
127
- virtual void lock () {
128
- // Stub
129
- }
130
-
131
- /* * Release exclusive access to this object.
142
+ /* * Find the length of the file
143
+ *
144
+ * @returns
145
+ * Length of the file
132
146
*/
133
- virtual void unlock () {
134
- // Stub
135
- }
147
+ MBED_DEPRECATED_SINCE (" mbed-os-5.4" , " Replaced by FileHandle::size" )
148
+ virtual off_t flen () { return size (); }
136
149
};
137
150
151
+
152
+ /* * @}*/
138
153
} // namespace mbed
139
154
140
155
#endif
141
-
142
- /* * @}*/
0 commit comments