libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
fileinfo.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <filesystem>
11#include <optional>
13#include <promeki/core/string.h>
15
17
35class FileInfo {
36 public:
38 using Status = std::filesystem::file_status;
39
44 FileInfo(const String &filePath) : _path(filePath.str()) {
45
46 }
47
52 FileInfo(const char *filePath) : _path(filePath) {
53
54 }
55
60 FileInfo(const FilePath &fp) : _path(fp.toStdPath()) {
61
62 }
63
69 return FilePath(_path);
70 }
71
76 bool exists() const {
77 return std::filesystem::exists(status());
78 }
79
84 String fileName() const {
85 return _path.filename().string();
86 }
87
92 String baseName() const {
93 return _path.stem().string();
94 }
95
100 String suffix() const {
101 auto ext = _path.extension().string();
102 if(ext.empty()) return String();
103 return ext.substr(1); // Remove leading '.'
104 }
105
111 return _path.parent_path().string();
112 }
113
119 return std::filesystem::absolute(_path).string();
120 }
121
126 bool isFile() const {
127 return std::filesystem::is_regular_file(status());
128 }
129
134 bool isDirectory() const {
135 return std::filesystem::is_directory(status());
136 }
137
142 void updateStatus(bool force = false) const {
143 if(!_status.has_value() || force) {
144 _status = std::filesystem::status(_path);
145 }
146 return;
147 }
148
154 Status status(bool forceUpdate = false) const {
156 return _status.value();
157 }
158
163 std::uintmax_t size() const {
164 return isFile() ? std::filesystem::file_size(_path) : 0;
165 }
166
171 bool isReadable() const {
172 std::error_code ec;
173 auto perms = std::filesystem::status(_path, ec).permissions();
174 return (perms & std::filesystem::perms::owner_read) != std::filesystem::perms::none;
175 }
176
181 bool isWritable() const {
182 std::error_code ec;
183 auto perms = std::filesystem::status(_path, ec).permissions();
184 return (perms & std::filesystem::perms::owner_write) != std::filesystem::perms::none;
185 }
186
191 bool isExecutable() const {
192 std::error_code ec;
193 auto perms = std::filesystem::status(_path, ec).permissions();
194 return (perms & std::filesystem::perms::owner_exec) != std::filesystem::perms::none;
195 }
196
197 private:
198 std::filesystem::path _path;
199 mutable std::optional<Status> _status;
200
201};
202
204
Provides information about a file system entry.
Definition fileinfo.h:35
Status status(bool forceUpdate=false) const
Returns the file status, updating the cache if needed.
Definition fileinfo.h:154
void updateStatus(bool force=false) const
Updates the cached file status.
Definition fileinfo.h:142
String absoluteFilePath() const
Returns the absolute path to the file, including the filename.
Definition fileinfo.h:118
bool isExecutable() const
Returns true if the file is executable by the owner.
Definition fileinfo.h:191
FileInfo(const FilePath &fp)
Constructs a FileInfo from a FilePath.
Definition fileinfo.h:60
std::uintmax_t size() const
Returns the file size in bytes.
Definition fileinfo.h:163
bool exists() const
Returns true if the file or directory exists.
Definition fileinfo.h:76
bool isWritable() const
Returns true if the file is writable by the owner.
Definition fileinfo.h:181
String suffix() const
Returns the file extension without the leading dot.
Definition fileinfo.h:100
std::filesystem::file_status Status
File status type from the standard filesystem library.
Definition fileinfo.h:38
FilePath filePath() const
Returns the path as a FilePath.
Definition fileinfo.h:68
bool isReadable() const
Returns true if the file is readable by the owner.
Definition fileinfo.h:171
String baseName() const
Returns the filename without its extension.
Definition fileinfo.h:92
bool isFile() const
Returns true if the path refers to a regular file.
Definition fileinfo.h:126
FileInfo(const String &filePath)
Constructs a FileInfo for the given file path.
Definition fileinfo.h:44
FileInfo(const char *filePath)
Constructs a FileInfo from a C string.
Definition fileinfo.h:52
String fileName() const
Returns the filename component of the path (including extension).
Definition fileinfo.h:84
String absolutePath() const
Returns the absolute path of the parent directory.
Definition fileinfo.h:110
bool isDirectory() const
Returns true if the path refers to a directory.
Definition fileinfo.h:134
Simple value type wrapping std::filesystem::path.
Definition filepath.h:27
Dynamic array container wrapping std::vector.
Definition list.h:40
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19