1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
/*
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
*
* This file is part of lsp-runtime-lib
* Created on: 8 февр. 2019 г.
*
* lsp-runtime-lib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* lsp-runtime-lib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef LSP_PLUG_IN_IO_PATH_H_
#define LSP_PLUG_IN_IO_PATH_H_
#include <lsp-plug.in/runtime/version.h>
#include <lsp-plug.in/runtime/LSPString.h>
#include <lsp-plug.in/common/status.h>
#include <lsp-plug.in/lltl/types.h>
#include <stdarg.h>
namespace lsp
{
namespace io
{
typedef struct fattr_t
{
enum ftype_t {
FT_BLOCK,
FT_CHARACTER,
FT_DIRECTORY,
FT_FIFO,
FT_SYMLINK,
FT_REGULAR,
FT_SOCKET,
FT_UNKNOWN
};
ftype_t type; // File type
size_t blk_size; // Block size
wsize_t size; // File size
wsize_t inode; // Index node
wsize_t ctime; // Creation time in milliseconds
wsize_t mtime; // Modification time in milliseconds
wsize_t atime; // Access time in milliseconds
} fattr_t;
/**
* Path object. All char * pointers are treated as UTF-8 strings unless the
* special case is described.
*/
class Path
{
private:
LSPString sPath;
private:
Path & operator = (const Path &);
inline void fixup_path();
status_t compute_relative(Path *base);
public:
explicit Path();
~Path();
Path *clone() const;
public:
// Setting and getting the path value
/**
* Set the native-encoded string path to the Path object
* @param path path to set
* @param charset character set to use
* @return status of operation
*/
status_t set_native(const char *path, const char *charset = NULL);
/**
* Assign string path to the Path object
* @param path path to set
* @return status of operation
*/
status_t set(const char *path);
status_t set(const LSPString *path);
status_t set(const Path *path);
/**
* Construct path of two parts
* @param path path to set
* @param child the child element to append to the path
* @return status of operation
*/
status_t set(const char *path, const char *child);
status_t set(const char *path, const LSPString *child);
status_t set(const char *path, const Path *child);
status_t set(const LSPString *path, const char *child);
status_t set(const LSPString *path, const LSPString *child);
status_t set(const LSPString *path, const Path *child);
status_t set(const Path *path, const char *child);
status_t set(const Path *path, const LSPString *child);
status_t set(const Path *path, const Path *child);
/**
* Get the current path value
* @return current path value
*/
inline const char *get() const { return sPath.get_utf8(); }
/**
* Store the path value in the provided character buffer
* @param path target buffer to store value
* @param maxlen maximum length of character buffer
* @return status of operation
*/
status_t get(char *path, size_t maxlen) const;
/**
* Store the path value in the string
* @param path target string object to store value
* @return status of operation
*/
status_t get(LSPString *path) const;
/**
* Store the path value in the string
* @param path target Path object to store value
* @return status of operation
*/
status_t get(Path *path) const;
public:
// Operations with last element in the path
/**
* Replace last element of the path with the new value
* @param path new value to use as the end of the path
* @return status of operation
*/
status_t set_last(const char *path);
/**
* Replace last element of the path with the new value
* @param path new value to use as the end of the path
* @return status of operation
*/
status_t set_last(const LSPString *path);
/**
* Replace last element of the path with the new value
* @param path new value to use as the end of the path
* @return status of operation
*/
status_t set_last(const Path *path);
/**
* Get last element of the path
* @param path character buffer to store character data
* @param maxlen maximum length of the buffer
* @return status of operation
*/
status_t get_last(char *path, size_t maxlen) const;
/**
* Get last element of the path
* @param path string object to store the value
* @return status of operation
*/
status_t get_last(LSPString *path) const;
/**
* Get last element of the path
* @param path Path object to store the value
* @return status of operation
*/
status_t get_last(Path *path) const;
/**
* Remove last element from the path object
* @return status of operation
*/
status_t remove_last();
/**
* Remove last element from the path object and return the removed element
* @param path character buffer to store the value
* @param maxlen maximum length of the buffer
* @return status of operation
*/
status_t remove_last(char *path, size_t maxlen);
/**
* Remove last element from the path object and return the removed element
* @param path string to store the value
* @param maxlen maximum length of the buffer
* @return status of operation
*/
status_t remove_last(LSPString *path);
/**
* Remove last element from the path object and return the removed element
* @param path string to store the value
* @param maxlen maximum length of the buffer
* @return status of operation
*/
status_t remove_last(Path *path);
/**
* Remove the last element from the path object and return the result value
* @param path character buffer to store the value
* @param maxlen maximum length of the buffer
* @return status of operation
*/
status_t without_last(char *path, size_t maxlen) const;
/**
* Remove the last element from the path object and return the result value
* @param path string to store the value
* @return status of operation
*/
status_t without_last(LSPString *path) const;
/**
* Remove the last element from the path object and return the result value
* @param path path object to store the value
* @return status of operation
*/
status_t without_last(Path *path) const;
public:
// Operations with first element in the path
/**
* Get first element of the path
* @param path character buffer to store character data
* @param maxlen maximum length of the buffer
* @return status of operation
*/
status_t get_first(char *path, size_t maxlen) const;
/**
* Get first element of the path
* @param path string object to store the value
* @return status of operation
*/
status_t get_first(LSPString *path) const;
/**
* Get first element of the path
* @param path Path object to store the value
* @return status of operation
*/
status_t get_first(Path *path) const;
/**
* Remove first element from the path object
* @return status of operation
*/
status_t remove_first();
/**
* Remove first element from the path object and return the removed element
* @param path character buffer to store the value
* @param maxlen maximum length of the buffer
* @return status of operation
*/
status_t remove_first(char *path, size_t maxlen);
/**
* Remove first element from the path object and return the removed element
* @param path string to store the value
* @param maxlen maximum length of the buffer
* @return status of operation
*/
status_t remove_first(LSPString *path);
/**
* Remove first element from the path object and return the removed element
* @param path string to store the value
* @param maxlen maximum length of the buffer
* @return status of operation
*/
status_t remove_first(Path *path);
/**
* Remove the first element from the path object and return the result value
* @param path character buffer to store the value
* @param maxlen maximum length of the buffer
* @return status of operation
*/
status_t without_first(char *path, size_t maxlen) const;
/**
* Remove the first element from the path object and return the result value
* @param path string to store the value
* @return status of operation
*/
status_t without_first(LSPString *path) const;
/**
* Remove the first element from the path object and return the result value
* @param path path object to store the value
* @return status of operation
*/
status_t without_first(Path *path) const;
public:
status_t get_ext(char *path, size_t maxlen) const;
status_t get_ext(LSPString *path) const;
status_t get_ext(Path *path) const;
status_t get_noext(char *path, size_t maxlen) const;
status_t get_noext(LSPString *path) const;
status_t get_noext(Path *path) const;
status_t get_last_noext(char *path, size_t maxlen) const;
status_t get_last_noext(LSPString *path) const;
status_t get_last_noext(Path *path) const;
status_t get_parent(char *path, size_t maxlen) const;
status_t get_parent(LSPString *path) const;
status_t get_parent(Path *path) const;
status_t set_parent(const char *path);
status_t set_parent(const LSPString *path);
status_t set_parent(const Path *path);
status_t concat(const char *path);
status_t concat(const LSPString *path);
status_t concat(const Path *path);
status_t append_child(const char *path);
status_t append_child(const LSPString *path);
status_t append_child(const Path *path);
status_t append(const char *path);
status_t append(const LSPString *path);
status_t append(const Path *path);
status_t remove_base();
status_t remove_base(const char *path);
status_t remove_base(const LSPString *path);
status_t remove_base(const Path *path);
status_t remove_root();
ssize_t fmt(const char *fmt...);
ssize_t fmt(const LSPString *fmt...);
ssize_t vfmt(const char *fmt, va_list args);
ssize_t vfmt(const LSPString *fmt, va_list args);
bool is_absolute() const;
bool is_relative() const;
bool is_canonical() const;
bool is_root() const;
bool is_dot() const;
bool is_dotdot() const;
bool is_dots() const;
inline bool is_empty() const { return sPath.is_empty(); }
inline void clear() { sPath.clear(); }
inline void swap(Path *path) { sPath.swap(&path->sPath); }
status_t canonicalize();
status_t root();
status_t current();
status_t parent();
status_t get_canonical(char *path, size_t maxlen) const;
status_t get_canonical(LSPString *path) const;
status_t get_canonical(Path *path) const;
status_t as_relative(const char *path);
status_t as_relative(const LSPString *path);
status_t as_relative(const Path *path);
bool equals(const Path *path) const;
bool equals(const LSPString *path) const;
bool equals(const char *path) const;
inline const LSPString *as_string() const { return &sPath; }
inline const char *as_utf8() const { return sPath.get_utf8(); }
inline const char *as_native(const char *charset = NULL) const { return sPath.get_native(charset); }
inline void take(Path *src) { sPath.take(&src->sPath); }
inline size_t length() const { return sPath.length(); }
void take(LSPString *src);
public:
// Some kind of file operations
/**
* Return information about the file
* @param attr pointer to structure to store the information
* @return status of operaiton
*/
status_t stat(fattr_t *attr) const;
/**
* Return information about the file, do not follow symbolic links
* @param attr pointer to structure to store the information
* @return status of operaiton
*/
status_t sym_stat(fattr_t *attr) const;
/**
* Obtain the size of the file
* @return file size or negative error code
*/
wssize_t size() const;
/**
* Check that path entry exists on file system
* @return true if path entry exists on file system
*/
bool exists() const;
/**
* Check that path entry exists on file system and is a regular file
* @return true if path entry exists on file system and is a regular file
*/
bool is_reg() const;
/**
* Check that path entry exists on file system and is a directory
* @return true if path entry exists on file system and is a directory
*/
bool is_dir() const;
/**
* Check that path entry exists on file system and is a block device
* @return true if path entry exists on file system and is a block device
*/
bool is_block_dev() const;
/**
* Check that path entry exists on file system and is a character device
* @return true if path entry exists on file system and is a character device
*/
bool is_char_dev() const;
/**
* Check that path entry exists on file system and is a FIFO
* @return true if path entry exists on file system and is a FIFO
*/
bool is_fifo() const;
/**
* Check that path entry exists on file system and is a symbolic link
* @return true if path entry exists on file system and is a symbolic link
*/
bool is_symlink() const;
/**
* Check that path entry exists on file system and is a socket
* @return true if path entry exists on file system and is a socket
*/
bool is_socket() const;
/**
* Create directory associated with the path name
* @return status of operation
*/
status_t mkdir() const;
/**
* Create directory associated with the path name
* @param recursive flag that allows to perform recursive directory creation
* @return status of operation
*/
status_t mkdir(bool recursive) const;
/**
* Create parent directory associated with the path name
* @return status of operation
*/
status_t mkparent() const;
/**
* Create parent directory associated with the path name
* @param recursive flag that allows to perform recursive directory creation
* @return status of operation
*/
status_t mkparent(bool recursive) const;
/**
* Remove the file associated with the path
* @return status of operation
*/
status_t remove() const;
/**
* Rename or move the file associated with the path name to another path
* @param dst destination file name
* @return status of operation
*/
status_t rename(const char *dst) const;
/**
* Rename or move the file associated with the path name to another path
* @param dst destination file name
* @return status of operation
*/
status_t rename(const LSPString *dst) const;
/**
* Rename or move the file associated with the path name to another path
* @param dst destination file name
* @return status of operation
*/
status_t rename(const io::Path *dst) const;
public:
inline size_t hash() const { return sPath.hash(); }
inline size_t compare_to(const io::Path *dst) const { return sPath.compare_to(&dst->sPath); }
public:
static bool is_dot(const LSPString *path);
static bool is_dot(const io::Path *path);
static bool is_dot(const char *path);
static bool is_dotdot(const LSPString *path);
static bool is_dotdot(const io::Path *path);
static bool is_dotdot(const char *path);
static bool is_dots(const LSPString *path);
static bool is_dots(const io::Path *path);
static bool is_dots(const char *path);
static bool valid_file_name(const LSPString *fname);
static bool valid_path_name(const LSPString *fname);
};
}
// LLTL specialization for Path class
namespace lltl
{
template <>
struct hash_spec<io::Path>: public hash_iface
{
static size_t hash_func(const void *ptr, size_t size);
explicit hash_spec()
{
hash = hash_func;
}
};
template <>
struct compare_spec<io::Path>: public compare_iface
{
static ssize_t cmp_func(const void *a, const void *b, size_t size);
explicit compare_spec()
{
compare = cmp_func;
}
};
template <>
struct allocator_spec<io::Path>: public allocator_iface
{
static void *clone_func(const void *src, size_t size);
static void free_func(void *ptr);
explicit allocator_spec()
{
clone = clone_func;
free = free_func;
}
};
}
} /* namespace lsp */
#endif /* LSP_PLUG_IN_IO_PATH_H_ */
|