| 1 |
/* |
| 2 |
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2023 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 18 мар. 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_DIR_H_ |
| 23 |
#define LSP_PLUG_IN_IO_DIR_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/status.h> |
| 27 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 28 |
#include <lsp-plug.in/io/Path.h> |
| 29 |
#include <lsp-plug.in/io/File.h> |
| 30 |
|
| 31 |
#ifndef PLATFORM_WINDOWS |
| 32 |
#include <dirent.h> |
| 33 |
#endif /* PLATFORM_WINDOWS */ |
| 34 |
|
| 35 |
namespace lsp |
| 36 |
{ |
| 37 |
namespace io |
| 38 |
{ |
| 39 |
#ifdef PLATFORM_WINDOWS |
| 40 |
struct dirhandle_t; |
| 41 |
#else |
| 42 |
typedef DIR dirhandle_t; |
| 43 |
#endif /* PLATFORM_WINDOWS */ |
| 44 |
|
| 45 |
class Dir |
| 46 |
{ |
| 47 |
protected: |
| 48 |
status_t nErrorCode; |
| 49 |
Path sPath; |
| 50 |
dirhandle_t *hDir; |
| 51 |
|
| 52 |
protected: |
| 53 |
inline status_t set_error(status_t error) { return nErrorCode = error; } |
| 54 |
|
| 55 |
public: |
| 56 |
explicit Dir(); |
| 57 |
Dir(const Dir &) = delete; |
| 58 |
Dir(Dir &&) = delete; |
| 59 |
virtual ~Dir(); |
| 60 |
|
| 61 |
Dir & operator = (const Dir &) = delete; |
| 62 |
Dir & operator = (Dir &&) = delete; |
| 63 |
|
| 64 |
public: |
| 65 |
/** |
| 66 |
* Open directory |
| 67 |
* @param path path to directory in UTF-8 encoding |
| 68 |
* @return status of operation |
| 69 |
*/ |
| 70 |
status_t open(const char *path); |
| 71 |
|
| 72 |
/** |
| 73 |
* Open directory |
| 74 |
* @param path path to directory |
| 75 |
* @return status of operation |
| 76 |
*/ |
| 77 |
status_t open(const LSPString *path); |
| 78 |
|
| 79 |
/** |
| 80 |
* Open directory |
| 81 |
* @param path path to directory |
| 82 |
* @return status of operation |
| 83 |
*/ |
| 84 |
status_t open(const Path *path); |
| 85 |
|
| 86 |
/** |
| 87 |
* Seek directory to it's beginning |
| 88 |
* @return status of operation |
| 89 |
*/ |
| 90 |
status_t rewind(); |
| 91 |
|
| 92 |
/** |
| 93 |
* Read the record from directory |
| 94 |
* @param path pointer to string to store new record |
| 95 |
* @param full retrieve full path instead of relative |
| 96 |
* @return status of operation |
| 97 |
*/ |
| 98 |
status_t read(LSPString *path, bool full = false); |
| 99 |
|
| 100 |
/** |
| 101 |
* Read the record from directory |
| 102 |
* @param path pointer to string to store new record |
| 103 |
* @param full retrieve full path instead of relative |
| 104 |
* @return status of operation |
| 105 |
*/ |
| 106 |
status_t read(Path *path, bool full = false); |
| 107 |
|
| 108 |
/** |
| 109 |
* Read and stat the record from directory |
| 110 |
* @param path pointer to string to store new record |
| 111 |
* @param full retrieve full path instead of relative |
| 112 |
* @return status of operation |
| 113 |
*/ |
| 114 |
status_t reads(LSPString *path, fattr_t *attr, bool full = false); |
| 115 |
|
| 116 |
/** |
| 117 |
* Read and stat the record from directory |
| 118 |
* @param path pointer to string to store new record |
| 119 |
* @param full retrieve full path instead of relative |
| 120 |
* @return status of operation |
| 121 |
*/ |
| 122 |
status_t reads(Path *path, fattr_t *attr, bool full = false); |
| 123 |
|
| 124 |
/** |
| 125 |
* Close current directory |
| 126 |
* @return status of operation |
| 127 |
*/ |
| 128 |
status_t close(); |
| 129 |
|
| 130 |
/** |
| 131 |
* Return last error code |
| 132 |
* @return last error code |
| 133 |
*/ |
| 134 |
inline status_t last_error() const { return nErrorCode; }; |
| 135 |
|
| 136 |
/** |
| 137 |
* Return true if last read operations reached end of file |
| 138 |
* @return true if last read operations reached end of file |
| 139 |
*/ |
| 140 |
inline bool eof() const { return nErrorCode == STATUS_EOF; }; |
| 141 |
|
| 142 |
/** |
| 143 |
* Get path associated with directory |
| 144 |
* @param dst pointer to path to store value |
| 145 |
* @return status of operation |
| 146 |
*/ |
| 147 |
inline status_t get_path(Path *dst) const { return (dst != NULL) ? dst->set(&sPath) : STATUS_BAD_ARGUMENTS; } |
| 148 |
|
| 149 |
/** |
| 150 |
* Get path associated with directory |
| 151 |
* @param dst pointer to path to store value |
| 152 |
* @return status of operation |
| 153 |
*/ |
| 154 |
inline status_t get_path(LSPString *dst) const { return (dst != NULL) ? dst->set(sPath.as_string()) : STATUS_BAD_ARGUMENTS; } |
| 155 |
|
| 156 |
/** |
| 157 |
* Get path associated with directory |
| 158 |
* @return pointer to UTF-8 string or NULL |
| 159 |
*/ |
| 160 |
inline const char *get_path() const { return sPath.as_string()->get_utf8(); } |
| 161 |
|
| 162 |
/** |
| 163 |
* Obtain file information, do not follow symlinks |
| 164 |
* @param path UTF-8 encoded path to file |
| 165 |
* @param attr structure to return file attributes |
| 166 |
* @return status of operation |
| 167 |
*/ |
| 168 |
status_t stat(const char *path, fattr_t *attr); |
| 169 |
|
| 170 |
/** |
| 171 |
* Obtain file information, do not follow symlinks |
| 172 |
* @param path path to file |
| 173 |
* @param attr structure to return file attributes |
| 174 |
* @return status of operation |
| 175 |
*/ |
| 176 |
status_t stat(const LSPString *path, fattr_t *attr); |
| 177 |
|
| 178 |
/** |
| 179 |
* Obtain file information, do not follow symlinks |
| 180 |
* @param path path to file |
| 181 |
* @param attr structure to return file attributes |
| 182 |
* @return status of operation |
| 183 |
*/ |
| 184 |
status_t stat(const Path *path, fattr_t *attr); |
| 185 |
|
| 186 |
/** |
| 187 |
* Obtain file information, follow symlinks |
| 188 |
* @param path UTF-8 encoded path to file |
| 189 |
* @param attr structure to return file attributes |
| 190 |
* @return status of operation |
| 191 |
*/ |
| 192 |
status_t sym_stat(const char *path, fattr_t *attr); |
| 193 |
|
| 194 |
/** |
| 195 |
* Obtain file information, follow symlinks |
| 196 |
* @param path path to file |
| 197 |
* @param attr structure to return file attributes |
| 198 |
* @return status of operation |
| 199 |
*/ |
| 200 |
status_t sym_stat(const LSPString *path, fattr_t *attr); |
| 201 |
|
| 202 |
/** |
| 203 |
* Obtain file information, follow symlinks |
| 204 |
* @param path path to file |
| 205 |
* @param attr structure to return file attributes |
| 206 |
* @return status of operation |
| 207 |
*/ |
| 208 |
status_t sym_stat(const Path *path, fattr_t *attr); |
| 209 |
|
| 210 |
/** |
| 211 |
* Create directory |
| 212 |
* @param path path to the directory |
| 213 |
* @return status of operation |
| 214 |
*/ |
| 215 |
static status_t create(const char *path); |
| 216 |
|
| 217 |
/** |
| 218 |
* Create directory |
| 219 |
* @param path path to the directory |
| 220 |
* @return status of operation |
| 221 |
*/ |
| 222 |
static status_t create(const LSPString *path); |
| 223 |
|
| 224 |
/** |
| 225 |
* Create directory |
| 226 |
* @param path path to the directory |
| 227 |
* @return status of operation |
| 228 |
*/ |
| 229 |
static status_t create(const Path *path); |
| 230 |
|
| 231 |
/** |
| 232 |
* Delete directory |
| 233 |
* @param path path to directory |
| 234 |
* @return status of operation |
| 235 |
*/ |
| 236 |
static status_t remove(const char *path); |
| 237 |
|
| 238 |
/** |
| 239 |
* Delete directory |
| 240 |
* @param path path to directory |
| 241 |
* @return status of operation |
| 242 |
*/ |
| 243 |
static status_t remove(const LSPString *path); |
| 244 |
|
| 245 |
/** |
| 246 |
* Delete directory |
| 247 |
* @param path path to directory |
| 248 |
* @return status of operation |
| 249 |
*/ |
| 250 |
static status_t remove(const Path *path); |
| 251 |
|
| 252 |
/** |
| 253 |
* Delete directory |
| 254 |
* @param path path to directory |
| 255 |
* @return status of operation |
| 256 |
*/ |
| 257 |
static status_t get_current(LSPString *path); |
| 258 |
|
| 259 |
/** |
| 260 |
* Delete directory |
| 261 |
* @param path path to directory |
| 262 |
* @return status of operation |
| 263 |
*/ |
| 264 |
static status_t get_current(Path *path); |
| 265 |
}; |
| 266 |
|
| 267 |
} /* namespace io */ |
| 268 |
} /* namespace lsp */ |
| 269 |
|
| 270 |
#endif /* LSP_PLUG_IN_IO_DIR_H_ */ |