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
9#include <fstream>
10#include <memory>
11#include <optional>
12#include <string_view>
13#include <sys/types.h>
14
16
17 struct FClose {
18 void operator()(FILE* f) const { fclose(f); }
19 };
20 using FILE_t = std::unique_ptr<FILE, FClose>;
21
23#ifdef _WIN32
24 '\\';
25#else
26 '/';
27#endif
28
34 [[nodiscard]] std::string expandTilde(std::string path);
35
40 [[nodiscard]] bool needsTildeExpansion(std::string_view path);
41
53 void mkdir(zstring_view path, mode_t mode);
54
61 void mkdirp(std::string path);
62
66 int unlink(zstring_view path);
67
71 int rmdir(zstring_view path);
72
77
84 [[nodiscard]] FILE_t openFile(zstring_view filename, zstring_view mode);
85
91 void openOfStream(std::ofstream& stream, zstring_view filename);
92
99 void openOfStream(std::ofstream& stream, zstring_view filename,
100 std::ios_base::openmode mode);
101
107 [[nodiscard]] std::string_view getFilename(std::string_view path);
108
116 [[nodiscard]] std::string_view getDirName(std::string_view path);
117
125 [[nodiscard]] std::string_view getExtension(std::string_view path);
126
134 [[nodiscard]] std::string_view stripExtension(std::string_view path);
135
142 [[nodiscard]] std::string join(std::string_view part1, std::string_view part2);
143 [[nodiscard]] std::string join(std::string_view part1, std::string_view part2, std::string_view part3);
144 [[nodiscard]] std::string join(std::string_view part1, std::string_view part2,
145 std::string_view part3, std::string_view part4);
146
154#ifdef _WIN32
155 [[nodiscard]] std::string getConventionalPath( std::string path);
156#else
157 [[nodiscard]] inline const std::string& getConventionalPath(const std::string& path) { return path; }
158 [[nodiscard]] inline std::string getConventionalPath( std::string&& path) { return std::move(path); }
159#endif
160
168#ifdef _WIN32
169 [[nodiscard]] std::string getNativePath( std::string path);
170#else
171 [[nodiscard]] inline const std::string& getNativePath(const std::string& path) { return path; }
172 [[nodiscard]] inline std::string getNativePath( std::string&& path) { return std::move(path); }
173#endif
174
179#ifdef _WIN32
180 [[nodiscard]] std::string expandCurrentDirFromDrive(std::string path);
181#else
182 [[nodiscard]] inline const std::string& expandCurrentDirFromDrive(const std::string& path) { return path; }
183 [[nodiscard]] inline std::string expandCurrentDirFromDrive( std::string&& path) { return std::move(path); }
184#endif
185
189 [[nodiscard]] std::string getCurrentWorkingDirectory();
190
194 [[nodiscard]] std::string getAbsolutePath(std::string_view path);
195
200 [[nodiscard]] bool isAbsolutePath(std::string_view path);
201
213 [[nodiscard]] std::string getUserHomeDir(std::string_view username);
214
219 [[nodiscard]] const std::string& getUserOpenMSXDir();
220
225 [[nodiscard]] const std::string& getUserDataDir();
226
232 [[nodiscard]] const std::string& getSystemDataDir();
233
238 [[nodiscard]] const std::string& getSystemDocDir();
239
240#ifdef _WIN32
241 // using Stat = struct _stat; // doesn't work for some reason
242 typedef struct _stat Stat;
243#else
244 using Stat = struct stat;
245#endif
250 [[nodiscard]] std::optional<Stat> getStat(zstring_view filename);
251
255 [[nodiscard]] bool isRegularFile(zstring_view filename);
256 [[nodiscard]] bool isRegularFile(const Stat& st);
257
261 [[nodiscard]] bool isDirectory(zstring_view directory);
262 [[nodiscard]] bool isDirectory(const Stat& st);
263
267 [[nodiscard]] bool exists(zstring_view filename);
268
272 [[nodiscard]] inline time_t getModificationDate(const Stat& st) {
273 return Date::adjustTimeT(st.st_mtime);
274 }
275
286 [[nodiscard]] std::string getNextNumberedFileName(
287 std::string_view directory, std::string_view prefix, std::string_view extension,
288 bool addSeparator = false);
289
298 [[nodiscard]] std::string parseCommandFileArgument(
299 std::string_view argument, std::string_view directory,
300 std::string_view prefix, std::string_view extension);
301
306 [[nodiscard]] std::string getTempDir();
307
314 [[nodiscard]] FILE_t openUniqueFile(const std::string& directory, std::string& filename);
315
316} // namespace openmsx::FileOperations
317
318#endif
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
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.
string getNextNumberedFileName(string_view directory, string_view prefix, string_view extension, bool addSeparator)
Gets the next numbered file name with the specified prefix in the specified directory,...
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.
int deleteRecursive(const std::string &path)
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.
const string & getSystemDocDir()
Get system doc directory.
bool needsTildeExpansion(std::string_view path)
Returns true iff expandTilde(s) would have an effect.
const std::string & getConventionalPath(const std::string &path)
Returns the path in conventional path-delimiter.
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.