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  std::string getConventionalPath(std::string_view path);
145 
153  std::string getNativePath(std::string_view path);
154 
158  std::string getCurrentWorkingDirectory();
159 
163  std::string getAbsolutePath(std::string_view path);
164 
169  bool isAbsolutePath(std::string_view path);
170 
182  std::string getUserHomeDir(std::string_view username);
183 
188  const std::string& getUserOpenMSXDir();
189 
194  std::string getUserDataDir();
195 
201  std::string getSystemDataDir();
202 
207  std::string expandCurrentDirFromDrive(std::string_view path);
208 
209 #ifdef _WIN32
210  typedef struct _stat Stat;
211 #else
212  using Stat = struct stat;
213 #endif
214 
220  bool getStat(std::string_view filename, Stat& st);
221 
225  bool isRegularFile(std::string_view filename);
226  bool isRegularFile(const Stat& st);
227 
231  bool isDirectory(std::string_view directory);
232  bool isDirectory(const Stat& st);
233 
237  bool exists(std::string_view filename);
238 
241  time_t getModificationDate(const Stat& st);
242 
252  std::string getNextNumberedFileName(
253  std::string_view directory, std::string_view prefix, std::string_view extension);
254 
263  std::string parseCommandFileArgument(
264  std::string_view argument, std::string_view directory,
265  std::string_view prefix, std::string_view extension);
266 
271  std::string getTempDir();
272 
279  FILE_t openUniqueFile(const std::string& directory, std::string& filename);
280 
281 } // namespace openmsx::FileOperations
282 
283 #endif
string_view getFilename(string_view path)
bool isAbsolutePath(string_view path)
bool isRegularFile(const Stat &st)
int unlink(const std::string &path)
Call unlink() in a platform-independent manner.
string getNextNumberedFileName(string_view directory, string_view prefix, string_view extension)
string getUserHomeDir(string_view username)
void openofstream(std::ofstream &stream, const std::string &filename)
Open an ofstream in a platform-independent manner.
string getSystemDataDir()
Get system directory.
string getCurrentWorkingDirectory()
Returns the current working directory.
FILE_t openUniqueFile(const std::string &directory, std::string &filename)
Open a new file with a unique name in the provided directory.
constexpr const char *const filename
string getUserDataDir()
Get the openMSX data dir in the user&#39;s home directory.
string getNativePath(string_view path)
string expandCurrentDirFromDrive(string_view path)
bool exists(string_view filename)
int rmdir(const std::string &path)
Call rmdir() in a platform-independent manner.
string_view getExtension(string_view path)
const string & getUserOpenMSXDir()
Get the openMSX dir in the user&#39;s home directory.
void mkdir(const string &path, mode_t mode)
Create the specified directory.
bool isDirectory(const Stat &st)
string_view stripExtension(string_view path)
bool getStat(string_view filename_, Stat &st)
string parseCommandFileArgument(string_view argument, string_view directory, string_view prefix, string_view extension)
string getConventionalPath(string_view path)
string_view getDirName(string_view path)
string expandTilde(string_view path)
string getTempDir()
Get the name of the temp directory on the system.
void mkdirp(string_view path_)
FILE_t openFile(const std::string &filename, const std::string &mode)
Call fopen() in a platform-independent manner.
string join(string_view part1, string_view part2)
std::unique_ptr< FILE, FClose > FILE_t
string getAbsolutePath(string_view path)
int deleteRecursive(const std::string &path)
Recurively delete a file or directory and (in case of a directory) all its sub-components.
time_t getModificationDate(const Stat &st)
Get the date/time of last modification.