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