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