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