openMSX
FileOperations.hh
Go to the documentation of this file.
1 #ifndef FILEOPERATIONS_HH
2 #define FILEOPERATIONS_HH
3 
4 #include "unistdp.hh" // needed for mode_t definition when building with VC++
5 #include "statp.hh"
6 #include <sys/types.h>
7 #include <fstream>
8 #include <memory>
9 #include <string_view>
10 
11 namespace openmsx::FileOperations {
12 
13  struct FClose {
14  void operator()(FILE* f) { fclose(f); }
15  };
16  using FILE_t = std::unique_ptr<FILE, FClose>;
17 
18  const char nativePathSeparator =
19 #ifdef _WIN32
20  '\\';
21 #else
22  '/';
23 #endif
24 
30  std::string expandTilde(std::string_view path);
31 
43  void mkdir(const std::string& path, mode_t mode);
44 
51  void mkdirp(std::string_view path);
52 
56  int unlink(const std::string& path);
57 
61  int rmdir(const std::string& path);
62 
66  int deleteRecursive(const std::string& path);
67 
74  FILE_t openFile(const std::string& filename, const std::string& mode);
75 
81  void openofstream(std::ofstream& stream, const std::string& filename);
82 
89  void openofstream(std::ofstream& stream, const std::string& filename,
90  std::ios_base::openmode mode);
91 
97  std::string_view getFilename(std::string_view path);
98 
106  std::string_view getDirName(std::string_view path);
107 
115  std::string_view getExtension(std::string_view path);
116 
124  std::string_view stripExtension(std::string_view path);
125 
132  std::string join(std::string_view part1, std::string_view part2);
133  std::string join(std::string_view part1, std::string_view part2, std::string_view part3);
134  std::string join(std::string_view part1, std::string_view part2,
135  std::string_view part3, std::string_view part4);
136 
144 #ifdef _WIN32
145  std::string getConventionalPath(std::string path);
146 #else
147  inline const std::string& getConventionalPath(const std::string& path) { return path; }
148  inline std::string getConventionalPath(std::string&& path) { return std::move(path); }
149 #endif
150 
158  std::string getNativePath(std::string_view path);
159 
163  std::string getCurrentWorkingDirectory();
164 
168  std::string getAbsolutePath(std::string_view path);
169 
174  bool isAbsolutePath(std::string_view path);
175 
187  std::string getUserHomeDir(std::string_view username);
188 
193  const std::string& getUserOpenMSXDir();
194 
199  std::string getUserDataDir();
200 
206  std::string getSystemDataDir();
207 
212  std::string expandCurrentDirFromDrive(std::string_view path);
213 
214 #ifdef _WIN32
215  typedef struct _stat Stat;
216 #else
217  using Stat = struct stat;
218 #endif
219 
225  bool getStat(const std::string& filename, Stat& st);
226 
230  bool isRegularFile(const std::string& filename);
231  bool isRegularFile(const Stat& st);
232 
236  bool isDirectory(const std::string& directory);
237  bool isDirectory(const Stat& st);
238 
242  bool exists(const std::string& filename);
243 
246  time_t getModificationDate(const Stat& st);
247 
257  std::string getNextNumberedFileName(
258  std::string_view directory, std::string_view prefix, std::string_view extension);
259 
268  std::string parseCommandFileArgument(
269  std::string_view argument, std::string_view directory,
270  std::string_view prefix, std::string_view extension);
271 
276  std::string getTempDir();
277 
284  FILE_t openUniqueFile(const std::string& directory, std::string& filename);
285 
286 } // namespace openmsx::FileOperations
287 
288 #endif
openmsx::FileOperations::FILE_t
std::unique_ptr< FILE, FClose > FILE_t
Definition: FileOperations.hh:16
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
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:217
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:18
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:14
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:722
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:783
openmsx::FileOperations::getTempDir
string getTempDir()
Get the name of the temp directory on the system.
Definition: FileOperations.cc:752
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:147
openmsx::FileOperations::exists
bool exists(const std::string &filename)
Does this file (directory) exists?
Definition: FileOperations.cc:665
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:13
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.cc:671
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:695
openmsx::FileOperations::getFilename
string_view getFilename(string_view path)
Returns the file portion of a path name.
Definition: FileOperations.cc:390