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 <sys/types.h>
8 #include <fstream>
9 #include <memory>
10 #include <string_view>
11 
12 namespace openmsx::FileOperations {
13 
14  struct FClose {
15  void operator()(FILE* f) { fclose(f); }
16  };
17  using FILE_t = std::unique_ptr<FILE, FClose>;
18 
19  const char nativePathSeparator =
20 #ifdef _WIN32
21  '\\';
22 #else
23  '/';
24 #endif
25 
31  [[nodiscard]] std::string expandTilde(std::string_view path);
32 
44  void mkdir(const std::string& path, mode_t mode);
45 
52  void mkdirp(std::string_view path);
53 
57  int unlink(const std::string& path);
58 
62  int rmdir(const std::string& path);
63 
67  int deleteRecursive(const std::string& path);
68 
75  [[nodiscard]] FILE_t openFile(const std::string& filename, const std::string& mode);
76 
82  void openofstream(std::ofstream& stream, const std::string& filename);
83 
90  void openofstream(std::ofstream& stream, const std::string& filename,
91  std::ios_base::openmode mode);
92 
98  [[nodiscard]] std::string_view getFilename(std::string_view path);
99 
107  [[nodiscard]] std::string_view getDirName(std::string_view path);
108 
116  [[nodiscard]] std::string_view getExtension(std::string_view path);
117 
125  [[nodiscard]] std::string_view stripExtension(std::string_view path);
126 
133  [[nodiscard]] std::string join(std::string_view part1, std::string_view part2);
134  [[nodiscard]] std::string join(std::string_view part1, std::string_view part2, std::string_view part3);
135  [[nodiscard]] std::string join(std::string_view part1, std::string_view part2,
136  std::string_view part3, std::string_view part4);
137 
145 #ifdef _WIN32
146  [[nodiscard]] std::string getConventionalPath(std::string path);
147 #else
148  [[nodiscard]] inline const std::string& getConventionalPath(const std::string& path) { return path; }
149  [[nodiscard]] inline std::string getConventionalPath(std::string&& path) { return std::move(path); }
150 #endif
151 
159  [[nodiscard]] std::string getNativePath(std::string_view path);
160 
164  [[nodiscard]] std::string getCurrentWorkingDirectory();
165 
169  [[nodiscard]] std::string getAbsolutePath(std::string_view path);
170 
175  [[nodiscard]] bool isAbsolutePath(std::string_view path);
176 
188  [[nodiscard]] std::string getUserHomeDir(std::string_view username);
189 
194  [[nodiscard]] const std::string& getUserOpenMSXDir();
195 
200  [[nodiscard]] std::string getUserDataDir();
201 
207  [[nodiscard]] std::string getSystemDataDir();
208 
213  [[nodiscard]] std::string expandCurrentDirFromDrive(std::string_view path);
214 
215 #ifdef _WIN32
216  typedef struct _stat Stat;
217 #else
218  using Stat = struct stat;
219 #endif
220 
226  [[nodiscard]] bool getStat(const std::string& filename, Stat& st);
227 
231  [[nodiscard]] bool isRegularFile(const std::string& filename);
232  [[nodiscard]] bool isRegularFile(const Stat& st);
233 
237  [[nodiscard]] bool isDirectory(const std::string& directory);
238  [[nodiscard]] bool isDirectory(const Stat& st);
239 
243  [[nodiscard]] bool exists(const std::string& filename);
244 
248  [[nodiscard]] inline time_t getModificationDate(const Stat& st) {
249  return Date::adjustTimeT(st.st_mtime);
250  }
251 
261  [[nodiscard]] std::string getNextNumberedFileName(
262  std::string_view directory, std::string_view prefix, std::string_view extension);
263 
272  [[nodiscard]] std::string parseCommandFileArgument(
273  std::string_view argument, std::string_view directory,
274  std::string_view prefix, std::string_view extension);
275 
280  [[nodiscard]] std::string getTempDir();
281 
288  [[nodiscard]] FILE_t openUniqueFile(const std::string& directory, std::string& filename);
289 
290 } // namespace openmsx::FileOperations
291 
292 #endif
openmsx::FileOperations::FILE_t
std::unique_ptr< FILE, FClose > FILE_t
Definition: FileOperations.hh:17
openmsx::FileOperations::deleteRecursive
int deleteRecursive(const std::string &path)
Recurively delete a file or directory and (in case of a directory) all its sub-components.
Definition: FileOperations.cc:323
statp.hh
openmsx::FileOperations::isAbsolutePath
bool isAbsolutePath(string_view path)
Checks whether it's a absolute path or not.
Definition: FileOperations.cc:491
Date.hh
openmsx::FileOperations::getStat
bool getStat(const std::string &filename, Stat &st)
Call stat() and return the stat structure.
Definition: FileOperations.cc:626
openmsx::FileOperations::Stat
struct stat Stat
Definition: FileOperations.hh:218
openmsx::FileOperations::getAbsolutePath
string getAbsolutePath(string_view path)
Transform given path into an absolute path.
Definition: FileOperations.cc:480
openmsx::FileOperations::isRegularFile
bool isRegularFile(const Stat &st)
Definition: FileOperations.cc:644
openmsx::FileOperations::unlink
int unlink(const std::string &path)
Call unlink() in a platform-independent manner.
Definition: FileOperations.cc:282
openmsx::FileOperations::getNativePath
string getNativePath(string_view path)
Returns the path in native path-delimiter.
Definition: FileOperations.cc:444
openmsx::FileOperations::nativePathSeparator
const char nativePathSeparator
Definition: FileOperations.hh:19
openmsx::FileOperations::openofstream
void openofstream(std::ofstream &stream, const std::string &filename)
Open an ofstream in a platform-independent manner.
Definition: FileOperations.cc:367
openmsx::FileOperations::join
string join(string_view part1, string_view part2)
Join two paths.
Definition: FileOperations.cc:423
openmsx::FileOperations::mkdirp
void mkdirp(string_view path_)
Acts like the unix command "mkdir -p".
Definition: FileOperations.cc:248
openmsx::FileOperations::getSystemDataDir
string getSystemDataDir()
Get system directory.
Definition: FileOperations.cc:556
openmsx::FileOperations::getCurrentWorkingDirectory
string getCurrentWorkingDirectory()
Returns the current working directory.
Definition: FileOperations.cc:461
openmsx::FileOperations::getDirName
string_view getDirName(string_view path)
Returns the directory portion of a path.
Definition: FileOperations.cc:398
openmsx::FileOperations::getUserDataDir
string getUserDataDir()
Get the openMSX data dir in the user's home directory.
Definition: FileOperations.cc:549
unistdp.hh
openmsx::FileOperations
Definition: FileOperations.cc:77
openmsx::FileOperations::expandTilde
string expandTilde(string_view path)
Expand the '~' character to the users home directory.
Definition: FileOperations.cc:195
openmsx::filename
constexpr const char *const filename
Definition: FirmwareSwitch.cc:10
openmsx::FileOperations::FClose::operator()
void operator()(FILE *f)
Definition: FileOperations.hh:15
openmsx::FileOperations::getUserOpenMSXDir
const string & getUserOpenMSXDir()
Get the openMSX dir in the user's home directory.
Definition: FileOperations.cc:535
openmsx::FileOperations::expandCurrentDirFromDrive
string expandCurrentDirFromDrive(string_view path)
Get the current directory of the specified drive Linux: just return an empty string.
Definition: FileOperations.cc:599
openmsx::FileOperations::parseCommandFileArgument
string parseCommandFileArgument(string_view argument, string_view directory, string_view prefix, string_view extension)
Helper function for parsing filename arguments in Tcl commands.
Definition: FileOperations.cc:716
openmsx::FileOperations::openUniqueFile
FILE_t openUniqueFile(const std::string &directory, std::string &filename)
Open a new file with a unique name in the provided directory.
Definition: FileOperations.cc:777
openmsx::FileOperations::getTempDir
string getTempDir()
Get the name of the temp directory on the system.
Definition: FileOperations.cc:746
openmsx::FileOperations::openFile
FILE_t openFile(const std::string &filename, const std::string &mode)
Call fopen() in a platform-independent manner.
Definition: FileOperations.cc:353
openmsx::FileOperations::isDirectory
bool isDirectory(const Stat &st)
Definition: FileOperations.cc:654
openmsx::FileOperations::rmdir
int rmdir(const std::string &path)
Call rmdir() in a platform-independent manner.
Definition: FileOperations.cc:291
openmsx::FileOperations::getUserHomeDir
string getUserHomeDir(string_view username)
Get user's home directory.
Definition: FileOperations.cc:505
openmsx::FileOperations::getConventionalPath
const std::string & getConventionalPath(const std::string &path)
Returns the path in conventional path-delimiter.
Definition: FileOperations.hh:148
openmsx::FileOperations::exists
bool exists(const std::string &filename)
Does this file (directory) exists?
Definition: FileOperations.cc:665
openmsx::Date::adjustTimeT
time_t adjustTimeT(time_t time)
Definition: Date.hh:13
openmsx::FileOperations::getExtension
string_view getExtension(string_view path)
Returns the extension portion of a path.
Definition: FileOperations.cc:406
openmsx::FileOperations::mkdir
void mkdir(const string &path, mode_t mode)
Create the specified directory.
Definition: FileOperations.cc:220
openmsx::FileOperations::FClose
Definition: FileOperations.hh:14
openmsx::FileOperations::stripExtension
string_view stripExtension(string_view path)
Returns the path without extension.
Definition: FileOperations.cc:415
openmsx::FileOperations::getModificationDate
time_t getModificationDate(const Stat &st)
Get the date/time of last modification.
Definition: FileOperations.hh:248
openmsx::FileOperations::getNextNumberedFileName
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,...
Definition: FileOperations.cc:689
openmsx::FileOperations::getFilename
string_view getFilename(string_view path)
Returns the file portion of a path name.
Definition: FileOperations.cc:390