| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 6 мар. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_IO_STDIOFILE_H_ |
| 23 |
#define LSP_PLUG_IN_IO_STDIOFILE_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/io/File.h> |
| 27 |
#include <lsp-plug.in/io/Path.h> |
| 28 |
#include <lsp-plug.in/common/types.h> |
| 29 |
#include <lsp-plug.in/stdlib/stdio.h> |
| 30 |
|
| 31 |
namespace lsp |
| 32 |
{ |
| 33 |
namespace io |
| 34 |
{ |
| 35 |
/** |
| 36 |
* This class provides file interface that uses <stdio.h> file functions |
| 37 |
* at the backend for file operations. Also this class allows to |
| 38 |
* wrap standard FILE * pointer into an object instance. |
| 39 |
*/ |
| 40 |
class StdioFile: public File |
| 41 |
{ |
| 42 |
private: |
| 43 |
enum flags_t |
| 44 |
{ |
| 45 |
SF_READ = 1 << 0, |
| 46 |
SF_WRITE = 1 << 1, |
| 47 |
SF_CLOSE = 1 << 2 |
| 48 |
}; |
| 49 |
|
| 50 |
protected: |
| 51 |
FILE *pFD; |
| 52 |
size_t nFlags; |
| 53 |
|
| 54 |
public: |
| 55 |
explicit StdioFile(); |
| 56 |
StdioFile(const StdioFile &) = delete; |
| 57 |
StdioFile(StdioFile &&) = delete; |
| 58 |
virtual ~StdioFile() override; |
| 59 |
|
| 60 |
StdioFile &operator = (const StdioFile &) = delete; |
| 61 |
StdioFile &operator = (StdioFile &&) = delete; |
| 62 |
|
| 63 |
public: |
| 64 |
/** |
| 65 |
* Open file |
| 66 |
* @param path file location |
| 67 |
* @param mode open mode |
| 68 |
* @return status of operation |
| 69 |
*/ |
| 70 |
status_t open(const char *path, size_t mode); |
| 71 |
|
| 72 |
/** |
| 73 |
* Open file |
| 74 |
* @param path file location |
| 75 |
* @param mode open mode |
| 76 |
* @return status of operation |
| 77 |
*/ |
| 78 |
status_t open(const LSPString *path, size_t mode); |
| 79 |
|
| 80 |
/** |
| 81 |
* Open file |
| 82 |
* @param path file location |
| 83 |
* @param mode open mode |
| 84 |
* @return status of operation |
| 85 |
*/ |
| 86 |
status_t open(const Path *path, size_t mode); |
| 87 |
|
| 88 |
/** |
| 89 |
* Wrap the standard file descriptor and allow both read |
| 90 |
* and write operations |
| 91 |
* @param fd file descriptor to wrap |
| 92 |
* @param close close the file descriptor on close() call |
| 93 |
* @return status of operation |
| 94 |
*/ |
| 95 |
status_t wrap(FILE *fd, bool close); |
| 96 |
|
| 97 |
/** |
| 98 |
* Wrap the standard file descriptor |
| 99 |
* @param fd file descriptor to wrap |
| 100 |
* @param mode allowed access modes (read, write or both) |
| 101 |
* @param close close the file descriptor on close() call |
| 102 |
* @return status of operation |
| 103 |
*/ |
| 104 |
status_t wrap(FILE *fd, size_t mode, bool close); |
| 105 |
|
| 106 |
/** |
| 107 |
* Read binary file |
| 108 |
* @param dst target buffer to perform read |
| 109 |
* @param count amount of bytes to read |
| 110 |
* @return number of bytes read or negative status of operation, |
| 111 |
* on end of file -STATUS_EOF is returned |
| 112 |
*/ |
| 113 |
virtual ssize_t read(void *dst, size_t count) override; |
| 114 |
|
| 115 |
/** |
| 116 |
* Perform positioned read of binary file |
| 117 |
* @param pos offset in bytes relative to the beginning of the file |
| 118 |
* @param dst target buffer to perform read |
| 119 |
* @param count amount of bytes to read |
| 120 |
* @return number of bytes read or status of operation |
| 121 |
*/ |
| 122 |
virtual ssize_t pread(wsize_t pos, void *dst, size_t count) override; |
| 123 |
|
| 124 |
/** |
| 125 |
* Write binary file |
| 126 |
* @param src source buffer to perform write |
| 127 |
* @param count number of bytes to write |
| 128 |
* @return number of bytes written or negative status of operation |
| 129 |
*/ |
| 130 |
virtual ssize_t write(const void *src, size_t count) override; |
| 131 |
|
| 132 |
/** |
| 133 |
* Perform positioned write of binary file |
| 134 |
* @param pos offset in bytes relative to the beginning of the file |
| 135 |
* @param src source buffer to perform write |
| 136 |
* @param count number of bytes to write |
| 137 |
* @return status of operation |
| 138 |
*/ |
| 139 |
virtual ssize_t pwrite(wsize_t pos, const void *src, size_t count) override; |
| 140 |
|
| 141 |
/** |
| 142 |
* Perform seek to the specified position |
| 143 |
* @param pos position to perform seek |
| 144 |
* @param type seek type |
| 145 |
* @return status of operation |
| 146 |
*/ |
| 147 |
virtual status_t seek(wssize_t pos, size_t type) override; |
| 148 |
|
| 149 |
/** |
| 150 |
* Obtain current file's position |
| 151 |
* @return current file's position or negative error code |
| 152 |
*/ |
| 153 |
virtual wssize_t position() override; |
| 154 |
|
| 155 |
/** |
| 156 |
* Obtain current file's size |
| 157 |
* @return current file's size or negative error code |
| 158 |
*/ |
| 159 |
virtual wssize_t size() override; |
| 160 |
|
| 161 |
/** |
| 162 |
* Get file attributes |
| 163 |
* @param attr file attributes |
| 164 |
* @return status of operation |
| 165 |
*/ |
| 166 |
virtual status_t stat(fattr_t *attr) override; |
| 167 |
|
| 168 |
/** |
| 169 |
* Truncate the file |
| 170 |
* @param length the final file length |
| 171 |
* @return status of operation |
| 172 |
*/ |
| 173 |
virtual status_t truncate(wsize_t length) override; |
| 174 |
|
| 175 |
/** |
| 176 |
* Flush file buffer to underlying storage |
| 177 |
* @return status of operation |
| 178 |
*/ |
| 179 |
virtual status_t flush() override; |
| 180 |
|
| 181 |
/** |
| 182 |
* Sync file with the underlying storage |
| 183 |
* @return status of operation |
| 184 |
*/ |
| 185 |
virtual status_t sync() override; |
| 186 |
|
| 187 |
/** |
| 188 |
* Close file |
| 189 |
* @return status of operation |
| 190 |
*/ |
| 191 |
virtual status_t close() override; |
| 192 |
}; |
| 193 |
|
| 194 |
} /* namespace io */ |
| 195 |
} /* namespace lsp */ |
| 196 |
|
| 197 |
#endif /* LSP_PLUG_IN_IO_STDIOFILE_H_ */ |