Anklang-0.3.0.dev956+gd75ac925 anklang-0.3.0.dev956+gd75ac925
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
path.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#pragma once
3
4#include <ase/cxxaux.hh>
5
6#ifdef _WIN32 // includes _WIN64
7#define ASE_UNIX_PATHS 0
8#define ASE_DOS_PATHS 1
9#define ASE_DIRSEP '/'
10#define ASE_DIRSEP2 '\\'
11#define ASE_DIRSEPARATORS "/\\"
12#define ASE_SEARCHPATH_SEPARATOR ';'
13#define ASE_LIBEXT ".dll"
14#else // !_WIN32
15#define ASE_UNIX_PATHS 1
16#define ASE_DOS_PATHS 0
17#define ASE_DIRSEP '/'
18#define ASE_DIRSEP2 '/'
19#define ASE_DIRSEPARATORS "/"
20#define ASE_SEARCHPATH_SEPARATOR ':'
21#define ASE_LIBEXT ".so"
22#endif // !_WIN32
23#define ASE_PATH_MAX (PATH_MAX > 4096 ? PATH_MAX : 4096)
24
25namespace Ase {
26
28namespace Path {
29
30String dirname (const String &path);
31String basename (const String &path);
32String normalize (const String &path);
33String realpath (const String &path);
34String dir_terminate (const String &path);
35String strip_slashes (const String &path);
36String abspath (const String &path, const String &incwd = "");
37bool isabs (const String &path);
38bool isroot (const String &path, bool dos_drives = ASE_DOS_PATHS);
39bool isdirname (const String &path);
40bool dircontains (const String &dirpath, const String &descendant, String *relpath = nullptr);
41bool mkdirs (const String &dirpath, uint mode = 0750);
42void rmrf (const String &dir);
43bool copy_file (const String &src, const String &dest);
44bool rename (const String &src, const String &dest);
45size_t file_size (const String &path);
46StringPair split_extension (const std::string &filepath, bool lastdot = false);
47String expand_tilde (const String &path);
48String user_home (const String &username = "");
49String xdg_dir (const String &xdgdir = "");
53void config_names (const String &names);
58String skip_root (const String &path);
59template<class ...S>
60String join (String path, const S &...more);
61bool check (const String &file, const String &mode);
62bool equals (const String &file1, const String &file2);
63char* memread (const String &filename, size_t *lengthp, ssize_t maxlength = -1);
64void memfree (char *memread_mem);
65bool memwrite (const String &filename, size_t len, const uint8 *bytes, bool append = false, int perms = -1);
66String stringread (const String &filename, ssize_t maxlength = -1);
67bool stringwrite (const String &filename, const String &data, bool mkdirs = false, int perms = -1);
68bool stringappend (const String &filename, const String &data, bool mkdirs = false, int perms = -1);
69String cwd ();
70String vpath_find (const String &file, const String &mode = "e");
71String simplify_abspath (const std::string &abspath_expression);
72bool searchpath_contains (const String &searchpath, const String &element);
73String searchpath_find (const String &searchpath, const String &file, const String &mode = "e");
74StringS searchpath_list (const String &searchpath, const String &mode = "e");
75String searchpath_multiply (const String &searchpath, const String &postfixes);
76StringS searchpath_split (const String &searchpath);
77String searchpath_join (const StringS &string_vector);
78template<class ...S>
79String searchpath_join (String path, const S &...more);
80void glob (const String &pathpattern, StringS &matches);
81void glob (const String &pathpattern, StringS &dirs, StringS &files);
82void rglob (const String &basedir, const String &pattern, StringS &matches);
83void unique_realpaths (StringS &pathnames);
84StringS list_old_files (const String &abspath_glob, double min_age_seconds);
85String glob_stem (const String &abspath_glob, const String &matched_path);
86
87// == implementations ==
88String join_with (const String &head, char joiner, const String &tail);
89
90template<class ...S> inline String
91join (String path, const S &...more)
92{
93 ((path = join_with (path, ASE_DIRSEP, more)), ...); // C++17 fold expression
94 return path;
95}
96
97template<class ...S> inline String
98searchpath_join (String path, const S &...more)
99{
100 ((path = join_with (path, ASE_SEARCHPATH_SEPARATOR, more)), ...); // C++17 fold expression
101 return path;
102}
103
104} // Path
105} // Ase
106
basename
dirname
glob
String user_home(const String &username)
Get a user's home directory, uses $HOME if no username is given.
Definition path.cc:287
String cwd()
Return the current working directoy, including symlinks used in $PWD if available.
Definition path.cc:662
bool searchpath_contains(const String &searchpath, const String &element)
Check if searchpath contains element, a trailing slash searches for directories.
Definition path.cc:715
String config_dirs()
Get the $XDG_CONFIG_DIRS directory list, see: https://specifications.freedesktop.org/basedir-spec/lat...
Definition path.cc:401
void unique_realpaths(StringS &pathnames)
Convert all pathnames via realpath() and eliminate duplicates.
Definition path.cc:869
String config_names()
Get config names as set with config_names(), if unset defaults to program_alias().
Definition path.cc:442
String config_home()
Get the $XDG_CONFIG_HOME directory, see: https://specifications.freedesktop.org/basedir-spec/latest.
Definition path.cc:312
String searchpath_multiply(const String &searchpath, const String &postfixes)
Yield a new searchpath by combining each element of searchpath with each element of postfixes.
Definition path.cc:778
bool mkdirs(const String &dirpath, uint mode)
Create the directories in dirpath with mode, check errno on false returns.
Definition path.cc:197
bool check(const String &file, const String &mode)
Definition path.cc:625
String expand_tilde(const String &path)
Expand a "~/" or "~user/" path which refers to user home directories.
Definition path.cc:468
bool equals(const String &file1, const String &file2)
Definition path.cc:641
String dir_terminate(const String &path)
Append trailing slash to path, unless it's present.
Definition path.cc:109
String simplify_abspath(const std::string &abspath_expression)
Remove extra slashes, './' and '../' from abspath_expression.
Definition path.cc:886
bool isdirname(const String &path)
Return wether path is pointing to a directory component.
Definition path.cc:181
String strip_slashes(const String &path)
Strip trailing directory terminators.
Definition path.cc:118
String data_home()
Get the $XDG_DATA_HOME directory, see: https://specifications.freedesktop.org/basedir-spec/latest.
Definition path.cc:302
String cache_home()
Get the $XDG_CACHE_HOME directory, see: https://specifications.freedesktop.org/basedir-spec/latest.
Definition path.cc:322
String join_with(const String &head, char joiner, const String &tail)
Construct head + joiner + tail avoiding duplicates of joiner.
Definition path.cc:761
StringS list_old_files(const String &abspath_glob, double min_age_seconds)
List files matching abspath_glob that are older than min_age_seconds
Definition path.cc:903
String runtime_dir()
Get the $XDG_RUNTIME_DIR directory, see: https://specifications.freedesktop.org/basedir-spec/latest.
Definition path.cc:332
String searchpath_find(const String &searchpath, const String &file, const String &mode)
Find the first file in searchpath which matches mode (see check()).
Definition path.cc:737
String glob_stem(const String &abspath_glob, const String &matched_path)
Extract the stem from the last '*' part of abspath_glob when matched against matched_path
Definition path.cc:924
String abspath(const String &path, const String &incwd)
Definition path.cc:134
void rmrf(const String &dir)
Recursively delete directory tree.
Definition path.cc:236
size_t file_size(const String &path)
Retrieve the on-disk size in bytes of path.
Definition path.cc:514
bool copy_file(const String &src, const String &dest)
Copy a file to a new non-existing location, sets errno and returns false on error.
Definition path.cc:244
bool isabs(const String &path)
Return wether path is an absolute pathname.
Definition path.cc:148
bool isroot(const String &path, bool dos_drives)
Return wether path is an absolute pathname which identifies the root directory.
Definition path.cc:160
String data_dirs()
Get the $XDG_DATA_DIRS directory list, see: https://specifications.freedesktop.org/basedir-spec/lates...
Definition path.cc:412
bool dircontains(const String &dirpath, const String &descendant, String *relpath)
Check if descendant belongs to the directory hierarchy under dirpath.
Definition path.cc:221
void rglob(const String &basedir, const String &pattern, StringS &matches)
Recursively match files with glob pattern under basedir.
Definition path.cc:844
StringS searchpath_list(const String &searchpath, const String &mode)
Find all searchpath entries matching mode (see check()).
Definition path.cc:750
String normalize(const String &path)
Convert path to normal form.
Definition path.cc:86
The Anklang C++ API namespace.
Definition api.hh:8
uint8_t uint8
An 8-bit unsigned integer.
Definition cxxaux.hh:21
std::vector< String > StringS
Convenience alias for a std::vector<std::string>.
Definition cxxaux.hh:35
std::string String
Convenience alias for std::string.
Definition cxxaux.hh:34
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:17
#define ASE_DOS_PATHS
Equals 1 on _WIN32 and _WIN64 and 0 on Unix.
Definition path.hh:16
#define ASE_DIRSEP
Platform directory separator character, '/' on Unix-like systems, a '\' on _WIN32.
Definition path.hh:17
#define ASE_SEARCHPATH_SEPARATOR
Platform searchpath separator, ':' on Unix-like systems, ';' on _WIN32.
Definition path.hh:20
realpath
rename
typedef ssize_t