openMSX
FileOperations.hh
Go to the documentation of this file.
1#ifndef FILEOPERATIONS_HH
2#define FILEOPERATIONS_HH
3
4#include "Date.hh"
5#include "unistdp.hh" // needed for mode_t definition when building with VC++
6#include "statp.hh"
7#include "zstring_view.hh"
8#include <sys/types.h>
9#include <fstream>
10#include <memory>
11#include <optional>
12#include <string_view>
13
15
16 struct FClose {
17 void operator()(FILE* f) { fclose(f); }
18 };
19 using FILE_t = std::unique_ptr<FILE, FClose>;
20
22#ifdef _WIN32
23 '\\';
24#else
25 '/';
26#endif
27
33 [[nodiscard]] std::string expandTilde(std::string path);
34
39 [[nodiscard]] bool needsTildeExpansion(std::string_view path);
40
52 void mkdir(zstring_view path, mode_t mode);
53
60 void mkdirp(std::string path);
61
65 int unlink(zstring_view path);
66
70 int rmdir(zstring_view path);
71
76
83 [[nodiscard]] FILE_t openFile(zstring_view filename, zstring_view mode);
84
90 void openOfStream(std::ofstream& stream, zstring_view filename);
91
98 void openOfStream(std::ofstream& stream, zstring_view filename,
99 std::ios_base::openmode mode);
100
106 [[nodiscard]] std::string_view getFilename(std::string_view path);
107
115 [[nodiscard]] std::string_view getDirName(std::string_view path);
116
124 [[nodiscard]] std::string_view getExtension(std::string_view path);
125
133 [[nodiscard]] std::string_view stripExtension(std::string_view path);
134
141 [[nodiscard]] std::string join(std::string_view part1, std::string_view part2);
142 [[nodiscard]] std::string join(std::string_view part1, std::string_view part2, std::string_view part3);
143 [[nodiscard]] std::string join(std::string_view part1, std::string_view part2,
144 std::string_view part3, std::string_view part4);
145
153#ifdef _WIN32
154 [[nodiscard]] std::string getConventionalPath( std::string path);
155#else
156 [[nodiscard]] inline const std::string& getConventionalPath(const std::string& path) { return path; }
157 [[nodiscard]] inline std::string getConventionalPath( std::string&& path) { return std::move(path); }
158#endif
159
167#ifdef _WIN32
168 [[nodiscard]] std::string getNativePath( std::string path);
169#else
170 [[nodiscard]] inline const std::string& getNativePath(const std::string& path) { return path; }
171 [[nodiscard]] inline std::string getNativePath( std::string&& path) { return std::move(path); }
172#endif
173
178#ifdef _WIN32
179 [[nodiscard]] std::string expandCurrentDirFromDrive(std::string path);
180#else
181 [[nodiscard]] inline const std::string& expandCurrentDirFromDrive(const std::string& path) { return path; }
182 [[nodiscard]] inline std::string expandCurrentDirFromDrive( std::string&& path) { return std::move(path); }
183#endif
184
188 [[nodiscard]] std::string getCurrentWorkingDirectory();
189
193 [[nodiscard]] std::string getAbsolutePath(std::string_view path);
194
199 [[nodiscard]] bool isAbsolutePath(std::string_view path);
200
212 [[nodiscard]] std::string getUserHomeDir(std::string_view username);
213
218 [[nodiscard]] const std::string& getUserOpenMSXDir();
219
224 [[nodiscard]] const std::string& getUserDataDir();
225
231 [[nodiscard]] const std::string& getSystemDataDir();
232
233#ifdef _WIN32
234 typedef struct _stat Stat;
235#else
236 using Stat = struct stat;
237#endif
242 [[nodiscard]] std::optional<Stat> getStat(zstring_view filename);
243
247 [[nodiscard]] bool isRegularFile(zstring_view filename);
248 [[nodiscard]] bool isRegularFile(const Stat& st);
249
253 [[nodiscard]] bool isDirectory(zstring_view directory);
254 [[nodiscard]] bool isDirectory(const Stat& st);
255
259 [[nodiscard]] bool exists(zstring_view filename);
260
264 [[nodiscard]] inline time_t getModificationDate(const Stat& st) {
265 return Date::adjustTimeT(st.st_mtime);
266 }
267
277 [[nodiscard]] std::string getNextNumberedFileName(
278 std::string_view directory, std::string_view prefix, std::string_view extension);
279
288 [[nodiscard]] std::string parseCommandFileArgument(
289 std::string_view argument, std::string_view directory,
290 std::string_view prefix, std::string_view extension);
291
296 [[nodiscard]] std::string getTempDir();
297
304 [[nodiscard]] FILE_t openUniqueFile(const std::string& directory, std::string& filename);
305
306} // namespace openmsx::FileOperations
307
308#endif
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
Definition: zstring_view.hh:22
constexpr time_t adjustTimeT(time_t time)
Definition: Date.hh:14
string parseCommandFileArgument(string_view argument, string_view directory, string_view prefix, string_view extension)
Helper function for parsing filename arguments in Tcl commands.
bool exists(zstring_view filename)
Does this file (directory) exists?
FILE_t openUniqueFile(const std::string &directory, std::string &filename)
Open a new file with a unique name in the provided directory.
void mkdir(zstring_view path, mode_t mode)
Create the specified directory.
string expandTilde(string path)
Expand the '~' character to the users home directory.
string_view getDirName(string_view path)
Returns the directory portion of a path.
const std::string & getNativePath(const std::string &path)
Returns the path in native path-delimiter.
string getCurrentWorkingDirectory()
Returns the current working directory.
const std::string & expandCurrentDirFromDrive(const std::string &path)
Get the current directory of the specified drive Linux: return the given string unchanged.
string getUserHomeDir(string_view username)
Get user's home directory.
bool isRegularFile(const Stat &st)
string_view getExtension(string_view path)
Returns the extension portion of a path.
const string & getUserOpenMSXDir()
Get the openMSX dir in the user's home directory.
int rmdir(zstring_view path)
Call rmdir() in a platform-independent manner.
string getAbsolutePath(string_view path)
Transform given path into an absolute path.
string_view stripExtension(string_view path)
Returns the path without extension.
string getTempDir()
Get the name of the temp directory on the system.
bool isDirectory(const Stat &st)
void openOfStream(std::ofstream &stream, zstring_view filename)
Open an ofstream in a platform-independent manner.
bool isAbsolutePath(string_view path)
Checks whether it's a absolute path or not.
string_view getFilename(string_view path)
Returns the file portion of a path name.
const string & getSystemDataDir()
Get system directory.
std::optional< Stat > getStat(zstring_view filename)
Call stat() and return the stat structure.
FILE_t openFile(zstring_view filename, zstring_view mode)
Call fopen() in a platform-independent manner.
bool needsTildeExpansion(std::string_view path)
Returns true iff expandTilde(s) would have an effect.
int deleteRecursive(zstring_view path)
Recursively delete a file or directory and (in case of a directory) all its sub-components.
const std::string & getConventionalPath(const std::string &path)
Returns the path in conventional path-delimiter.
string getNextNumberedFileName(string_view directory, string_view prefix, string_view extension)
Gets the next numbered file name with the specified prefix in the specified directory,...
void mkdirp(string path)
Acts like the unix command "mkdir -p".
const string & getUserDataDir()
Get the openMSX data dir in the user's home directory.
std::unique_ptr< FILE, FClose > FILE_t
int unlink(zstring_view path)
Call unlink() in a platform-independent manner.
string join(string_view part1, string_view part2)
Join two paths.
time_t getModificationDate(const Stat &st)
Get the date/time of last modification.