openMSX
FileOperations.hh
Go to the documentation of this file.
1 #ifndef FILEOPERATIONS_HH
2 #define FILEOPERATIONS_HH
3 
4 #include "string_view.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 
11 namespace openmsx {
12 namespace 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  std::string expandTilde(string_view path);
32 
44  void mkdir(const std::string& path, mode_t mode);
45 
52  void mkdirp(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  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 
99 
108 
117 
126 
133  std::string join(string_view part1, string_view part2);
134  std::string join(string_view part1, string_view part2, string_view part3);
135  std::string join(string_view part1, string_view part2,
136  string_view part3, string_view part4);
137 
145  std::string getConventionalPath(string_view path);
146 
154  std::string getNativePath(string_view path);
155 
159  std::string getCurrentWorkingDirectory();
160 
164  std::string getAbsolutePath(string_view path);
165 
170  bool isAbsolutePath(string_view path);
171 
183  std::string getUserHomeDir(string_view username);
184 
189  const std::string& getUserOpenMSXDir();
190 
195  std::string getUserDataDir();
196 
202  std::string getSystemDataDir();
203 
208  std::string expandCurrentDirFromDrive(string_view path);
209 
210 #ifdef _WIN32
211  typedef struct _stat Stat;
212 #else
213  using Stat = struct stat;
214 #endif
215 
221  bool getStat(string_view filename, Stat& st);
222 
226  bool isRegularFile(string_view filename);
227  bool isRegularFile(const Stat& st);
228 
232  bool isDirectory(string_view directory);
233  bool isDirectory(const Stat& st);
234 
238  bool exists(string_view filename);
239 
242  time_t getModificationDate(const Stat& st);
243 
253  std::string getNextNumberedFileName(
254  string_view directory, string_view prefix, string_view extension);
255 
264  std::string parseCommandFileArgument(
265  string_view argument, string_view directory,
266  string_view prefix, string_view extension);
267 
272  std::string getTempDir();
273 
280  FILE_t openUniqueFile(const std::string& directory, std::string& filename);
281 
282 } // namespace FileOperations
283 } // namespace openmsx
284 
285 #endif
string_view getFilename(string_view path)
Returns the file portion of a path name.
bool isAbsolutePath(string_view path)
Checks whether it&#39;s a absolute path or not.
bool isRegularFile(const Stat &st)
string join(string_view part1, string_view part2)
Join two paths.
int unlink(const std::string &path)
Call unlink() in a platform-independent manner.
string parseCommandFileArgument(string_view argument, string_view directory, string_view prefix, string_view extension)
Helper function for parsing filename arguments in Tcl commands.
void openofstream(std::ofstream &stream, const std::string &filename)
Open an ofstream in a platform-independent manner.
string getSystemDataDir()
Get system directory.
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, with the specified extension.
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.
string expandCurrentDirFromDrive(string_view path)
Get the current directory of the specified drive Linux: just return an empty string.
string getConventionalPath(string_view path)
Returns the path in conventional path-delimiter.
string getNativePath(string_view path)
Returns the path in native path-delimiter.
string getUserDataDir()
Get the openMSX data dir in the user&#39;s home directory.
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
bool getStat(string_view filename_, Stat &st)
Call stat() and return the stat structure.
string getAbsolutePath(string_view path)
Transform given path into an absolute path.
bool exists(string_view filename)
Does this file (directory) exists?
int rmdir(const std::string &path)
Call rmdir() in a platform-independent manner.
string_view getExtension(string_view path)
Returns the extension portion of a path.
string getUserHomeDir(string_view username)
Get user&#39;s home directory.
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:15
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)
Returns the path without extension.
string_view getDirName(string_view path)
Returns the directory portion of a path.
string getTempDir()
Get the name of the temp directory on the system.
void mkdirp(string_view path_)
Acts like the unix command "mkdir -p".
FILE_t openFile(const std::string &filename, const std::string &mode)
Call fopen() in a platform-independent manner.
string expandTilde(string_view path)
Expand the &#39;~&#39; character to the users home directory.
std::unique_ptr< FILE, FClose > FILE_t
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.