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 <string_view>
12
14
15 struct FClose {
16 void operator()(FILE* f) { fclose(f); }
17 };
18 using FILE_t = std::unique_ptr<FILE, FClose>;
19
21#ifdef _WIN32
22 '\\';
23#else
24 '/';
25#endif
26
32 [[nodiscard]] std::string expandTilde(std::string path);
33
38 [[nodiscard]] bool needsTildeExpansion(std::string_view path);
39
51 void mkdir(zstring_view path, mode_t mode);
52
59 void mkdirp(std::string path);
60
64 int unlink(zstring_view path);
65
69 int rmdir(zstring_view path);
70
75
83
89 void openofstream(std::ofstream& stream, zstring_view filename);
90
97 void openofstream(std::ofstream& stream, zstring_view filename,
98 std::ios_base::openmode mode);
99
105 [[nodiscard]] std::string_view getFilename(std::string_view path);
106
114 [[nodiscard]] std::string_view getDirName(std::string_view path);
115
123 [[nodiscard]] std::string_view getExtension(std::string_view path);
124
132 [[nodiscard]] std::string_view stripExtension(std::string_view path);
133
140 [[nodiscard]] std::string join(std::string_view part1, std::string_view part2);
141 [[nodiscard]] std::string join(std::string_view part1, std::string_view part2, std::string_view part3);
142 [[nodiscard]] std::string join(std::string_view part1, std::string_view part2,
143 std::string_view part3, std::string_view part4);
144
152#ifdef _WIN32
153 [[nodiscard]] std::string getConventionalPath( std::string path);
154#else
155 [[nodiscard]] inline const std::string& getConventionalPath(const std::string& path) { return path; }
156 [[nodiscard]] inline std::string getConventionalPath( std::string&& path) { return std::move(path); }
157#endif
158
166#ifdef _WIN32
167 [[nodiscard]] std::string getNativePath( std::string path);
168#else
169 [[nodiscard]] inline const std::string& getNativePath(const std::string& path) { return path; }
170 [[nodiscard]] inline std::string getNativePath( std::string&& path) { return std::move(path); }
171#endif
172
177#ifdef _WIN32
178 [[nodiscard]] std::string expandCurrentDirFromDrive(std::string path);
179#else
180 [[nodiscard]] inline const std::string& expandCurrentDirFromDrive(const std::string& path) { return path; }
181 [[nodiscard]] inline std::string expandCurrentDirFromDrive( std::string&& path) { return std::move(path); }
182#endif
183
187 [[nodiscard]] std::string getCurrentWorkingDirectory();
188
192 [[nodiscard]] std::string getAbsolutePath(std::string_view path);
193
198 [[nodiscard]] bool isAbsolutePath(std::string_view path);
199
211 [[nodiscard]] std::string getUserHomeDir(std::string_view username);
212
217 [[nodiscard]] const std::string& getUserOpenMSXDir();
218
223 [[nodiscard]] const std::string& getUserDataDir();
224
230 [[nodiscard]] const std::string& getSystemDataDir();
231
232#ifdef _WIN32
233 typedef struct _stat Stat;
234#else
235 using Stat = struct stat;
236#endif
243 [[nodiscard]] bool getStat(zstring_view filename, Stat& st);
244
248 [[nodiscard]] bool isRegularFile(zstring_view filename);
249 [[nodiscard]] bool isRegularFile(const Stat& st);
250
254 [[nodiscard]] bool isDirectory(zstring_view directory);
255 [[nodiscard]] bool isDirectory(const Stat& st);
256
260 [[nodiscard]] bool exists(zstring_view filename);
261
265 [[nodiscard]] inline time_t getModificationDate(const Stat& st) {
266 return Date::adjustTimeT(st.st_mtime);
267 }
268
278 [[nodiscard]] std::string getNextNumberedFileName(
279 std::string_view directory, std::string_view prefix, std::string_view extension);
280
289 [[nodiscard]] std::string parseCommandFileArgument(
290 std::string_view argument, std::string_view directory,
291 std::string_view prefix, std::string_view extension);
292
297 [[nodiscard]] std::string getTempDir();
298
305 [[nodiscard]] FILE_t openUniqueFile(const std::string& directory, std::string& filename);
306
307} // namespace openmsx::FileOperations
308
309#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:13
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.
void openofstream(std::ofstream &stream, zstring_view filename)
Open an ofstream in a platform-independent manner.
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.
bool getStat(zstring_view filename, Stat &st)
Call stat() and return the stat structure.
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)
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.
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.
constexpr const char *const filename