Mbed OS Reference
Loading...
Searching...
No Matches
File.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2015 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef FILE_H
19#define FILE_H
20
21#include "filesystem/FileSystem.h"
22#include "platform/FileHandle.h"
23
24namespace mbed {
25/** \addtogroup filesystem */
26/** @{*/
27
28
29/** File class
30 */
31class File : public FileHandle {
32public:
33 /** Create an uninitialized file
34 *
35 * Must call open to initialize the file on a file system
36 */
38
39 /** Create a file on a filesystem
40 *
41 * Creates and opens a file on a filesystem
42 *
43 * @param fs Filesystem as target for the file
44 * @param path The name of the file to open
45 * @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
46 * bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
47 */
48 File(FileSystem *fs, const char *path, int flags = O_RDONLY);
49
50 /** Destroy a file
51 *
52 * Closes file if the file is still open
53 */
54 virtual ~File();
55
56 /** Open a file on the filesystem
57 *
58 * @param fs Filesystem as target for the file
59 * @param path The name of the file to open
60 * @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
61 * bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
62 * @return 0 on success, negative error code on failure
63 */
64 virtual int open(FileSystem *fs, const char *path, int flags = O_RDONLY);
65
66 /** Close a file
67 *
68 * @return 0 on success, negative error code on failure
69 */
70 virtual int close();
71
72 /** Read the contents of a file into a buffer
73 *
74 * @param buffer The buffer to read in to
75 * @param size The number of bytes to read
76 * @return The number of bytes read, 0 at end of file, negative error on failure
77 */
78
79 virtual ssize_t read(void *buffer, size_t size);
80
81 /** Write the contents of a buffer to a file
82 *
83 * @param buffer The buffer to write from
84 * @param size The number of bytes to write
85 * @return The number of bytes written, negative error on failure
86 */
87 virtual ssize_t write(const void *buffer, size_t size);
88
89 /** Flush any buffers associated with the file
90 *
91 * @return 0 on success, negative error code on failure
92 */
93 virtual int sync();
94
95 /** Check if the file in an interactive terminal device
96 *
97 * @return True if the file is a terminal
98 */
99 virtual int isatty();
100
101 /** Move the file position to a given offset from from a given location
102 *
103 * @param offset The offset from whence to move to
104 * @param whence The start of where to seek
105 * SEEK_SET to start from beginning of file,
106 * SEEK_CUR to start from current position in file,
107 * SEEK_END to start from end of file
108 * @return The new offset of the file
109 */
110 virtual off_t seek(off_t offset, int whence = SEEK_SET);
111
112 /** Get the file position of the file
113 *
114 * @return The current offset in the file
115 */
116 virtual off_t tell();
117
118 /** Rewind the file position to the beginning of the file
119 *
120 * @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
121 */
122 virtual void rewind();
123
124 /** Get the size of the file
125 *
126 * @return Size of the file in bytes
127 */
128 virtual off_t size();
129
130 /** Truncate or extend a file.
131 *
132 * The file's length is set to the specified value. The seek pointer is
133 * not changed. If the file is extended, the extended area appears as if
134 * it were zero-filled.
135 *
136 * @param length The requested new length for the file
137 *
138 * @return Zero on success, negative error code on failure
139 */
140 virtual int truncate(off_t length);
141
142private:
143 FileSystem *_fs;
144 fs_file_t _file;
145};
146
147
148/** @}*/
149} // namespace mbed
150
151#endif
Class FileHandle.
Definition: FileHandle.h:46
File class.
Definition: File.h:31
virtual int open(FileSystem *fs, const char *path, int flags=O_RDONLY)
Open a file on the filesystem.
virtual int close()
Close a file.
virtual off_t tell()
Get the file position of the file.
virtual int truncate(off_t length)
Truncate or extend a file.
virtual ~File()
Destroy a file.
virtual ssize_t read(void *buffer, size_t size)
Read the contents of a file into a buffer.
virtual int isatty()
Check if the file in an interactive terminal device.
virtual int sync()
Flush any buffers associated with the file.
File(FileSystem *fs, const char *path, int flags=O_RDONLY)
Create a file on a filesystem.
File()
Create an uninitialized file.
virtual ssize_t write(const void *buffer, size_t size)
Write the contents of a buffer to a file.
virtual off_t size()
Get the size of the file.
virtual off_t seek(off_t offset, int whence=SEEK_SET)
Move the file position to a given offset from from a given location.
virtual void rewind()
Rewind the file position to the beginning of the file.
A file system object.
Definition: FileSystem.h:50