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