cae  0.0.0
Cross-API graphics engine
Loading...
Searching...
No Matches
Path.hpp
Go to the documentation of this file.
1///
2/// @file Path.hpp
3/// @brief This file contains Path resolution utilities
4/// @namespace utl
5///
6
7#pragma once
8
9#ifdef _WIN32
10#include <windows.h>
11#elifdef __APPLE__
12#include <mach-o/dyld.h>
13#elif __linux__
14#include <unistd.h>
15#endif
16
17#include <filesystem>
18
19namespace utl
20{
21
22 namespace fs = std::filesystem;
23
24 ///
25 /// @class Path
26 /// @brief Class for path resolution utilities
27 /// @namespace utl
28 ///
29 class Path
30 {
31
32 public:
33 Path() = default;
34 ~Path() = default;
35
36 Path(const Path &) = delete;
37 Path &operator=(const Path &) = delete;
38 Path(Path &&) = delete;
39 Path &operator=(Path &&) = delete;
40
41 /// @param path Path to be normalized
42 /// @return Normalized path
43 /// @brief Normalize a path (resolve symlinks, relative paths, etc.)
44 static fs::path normalize(const fs::path &path)
45 {
46 try
47 {
48 return fs::weakly_canonical(path);
49 }
50 catch (...)
51 {
52 return fs::absolute(path);
53 }
54 }
55
56 ///
57 /// @param path Path to be checked
58 /// @return True if the file exists
59 /// @brief Check if a file exists
60 ///
61 static bool existsFile(const fs::path &path) { return fs::exists(path) && fs::is_regular_file(path); }
62
63 ///
64 /// @param path Path to be checked
65 /// @return True if the directory exists
66 /// @brief Check if a directory exists
67 ///
68 static bool existsDir(const fs::path &path) { return fs::exists(path) && fs::is_directory(path); }
69
70 ///
71 /// @param path Path to get the parent directory of
72 /// @return Parent directory of the path
73 /// @brief Get the parent directory of a path
74 ///
75 static fs::path parentDir(const fs::path &path) { return normalize(path).parent_path(); }
76
77 ///
78 /// @tparam Paths Variadic template for paths
79 /// @param paths Paths to be joined
80 /// @return Joined path
81 /// @brief Join multiple paths
82 ///
83 template <typename... Paths> static fs::path join(const Paths &...paths)
84 {
85 return normalize((fs::path(paths) / ...));
86 }
87
88 ///
89 /// @return Directory of the executable
90 /// @brief Get the directory of the executable
91 ///
92 static fs::path executableDir()
93 {
94#ifdef _WIN32
95 char buffer[MAX_PATH];
96 GetModuleFileNameA(NULL, buffer, MAX_PATH);
97 return fs::path(buffer).parent_path();
98#elifdef __APPLE__
99 char buffer[1024];
100 uint32_t size = sizeof(buffer);
101 if (_NSGetExecutablePath(buffer, &size) == 0)
102 {
103 return fs::path(buffer).parent_path();
104 }
105 return fs::current_path();
106#else
107 char buffer[1024];
108 if (const ssize_t count = readlink("/proc/self/exe", buffer, sizeof(buffer)); count != -1)
109 {
110 return fs::path(std::string(buffer, count)).parent_path();
111 }
112 return fs::current_path();
113#endif
114 }
115
116 ///
117 /// @param relativePath Relative path to be resolved
118 /// @return Resolved path relative to the executable directory
119 /// @brief Resolve a relative path to the executable directory
120 ///
121 static fs::path resolveRelativeToExe(const fs::path &relativePath)
122 {
123 return normalize(executableDir() / relativePath);
124 }
125
126 ///
127 /// @param relativePath Relative path to be resolved
128 /// @return Resolved path relative to the user cwd
129 /// @brief
130 ///
131 static fs::path resolveRelativeToCwd(const fs::path &relativePath)
132 {
133 return normalize(fs::current_path() / relativePath);
134 }
135
136 }; // class Path
137
138} // namespace utl
Class for path resolution utilities.
Definition Path.hpp:30
Path(const Path &)=delete
static fs::path executableDir()
Get the directory of the executable.
Definition Path.hpp:92
static fs::path normalize(const fs::path &path)
Normalize a path (resolve symlinks, relative paths, etc.)
Definition Path.hpp:44
Path()=default
Path(Path &&)=delete
static fs::path join(const Paths &...paths)
Join multiple paths.
Definition Path.hpp:83
static fs::path resolveRelativeToExe(const fs::path &relativePath)
Resolve a relative path to the executable directory.
Definition Path.hpp:121
static bool existsDir(const fs::path &path)
Check if a directory exists.
Definition Path.hpp:68
~Path()=default
static fs::path parentDir(const fs::path &path)
Get the parent directory of a path.
Definition Path.hpp:75
static bool existsFile(const fs::path &path)
Check if a file exists.
Definition Path.hpp:61
static fs::path resolveRelativeToCwd(const fs::path &relativePath)
Definition Path.hpp:131
Path & operator=(Path &&)=delete
Path & operator=(const Path &)=delete