openMSX
FileOperations.hh
Go to the documentation of this file.
1 #ifndef FILEOPERATIONS_HH
2 #define FILEOPERATIONS_HH
3 
4 #include "string_ref.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  typedef std::unique_ptr<FILE, FClose> FILE_t;
18 
19  const char nativePathSeparator =
20 #ifdef _WIN32
21  '\\';
22 #else
23  '/';
24 #endif
25 
31  std::string expandTilde(string_ref path);
32 
44  void mkdir(const std::string& path, mode_t mode);
45 
52  void mkdirp(string_ref 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_ref part1, string_ref part2);
134  std::string join(string_ref part1, string_ref part2, string_ref part3);
135  std::string join(string_ref part1, string_ref part2,
136  string_ref part3, string_ref part4);
137 
145  std::string getConventionalPath(string_ref path);
146 
154  std::string getNativePath(string_ref path);
155 
159  std::string getCurrentWorkingDirectory();
160 
164  std::string getAbsolutePath(string_ref path);
165 
170  bool isAbsolutePath(string_ref path);
171 
183  std::string getUserHomeDir(string_ref username);
184 
189  const std::string& getUserOpenMSXDir();
190 
195  std::string getUserDataDir();
196 
202  std::string getSystemDataDir();
203 
208  std::string expandCurrentDirFromDrive(string_ref path);
209 
210 #ifdef _WIN32
211  typedef struct _stat Stat;
212 #else
213  typedef struct stat Stat;
214 #endif
215 
221  bool getStat(string_ref filename, Stat& st);
222 
226  bool isRegularFile(string_ref filename);
227  bool isRegularFile(const Stat& st);
228 
232  bool isDirectory(string_ref directory);
233  bool isDirectory(const Stat& st);
234 
238  bool exists(string_ref filename);
239 
242  time_t getModificationDate(const Stat& st);
243 
253  std::string getNextNumberedFileName(
254  string_ref directory, string_ref prefix, string_ref extension);
255 
264  std::string parseCommandFileArgument(
265  string_ref argument, string_ref directory,
266  string_ref prefix, string_ref 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 getUserHomeDir(string_ref username)
Get user's home directory.
bool isAbsolutePath(string_ref path)
Checks whether it's a absolute path or not.
bool isRegularFile(const Stat &st)
int unlink(const std::string &path)
Call unlink() in a platform-independent manner.
void mkdirp(string_ref path_)
Acts like the unix command "mkdir -p".
string getNativePath(string_ref path)
Returns the path in native path-delimiter.
string getConventionalPath(string_ref path)
Returns the path in conventional path-delimiter.
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.
string join(string_ref part1, string_ref part2)
Join two paths.
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
string expandCurrentDirFromDrive(string_ref path)
Get the current directory of the specified drive Linux: just return an empty string.
string getAbsolutePath(string_ref path)
Transform given path into an absolute path.
bool getStat(string_ref filename_, Stat &st)
Call stat() and return the stat structure.
string_ref getFilename(string_ref path)
Returns the file portion of a path name.
FILE_t openUniqueFile(const std::string &directory, std::string &filename)
Open a new file with a unique name in the provided directory.
string parseCommandFileArgument(string_ref argument, string_ref directory, string_ref prefix, string_ref extension)
Helper function for parsing filename arguments in Tcl commands.
string getUserDataDir()
Get the openMSX data dir in the user's home directory.
string getNextNumberedFileName(string_ref directory, string_ref prefix, string_ref extension)
Gets the next numbered file name with the specified prefix in the specified directory, with the specified extension.
string_ref getBaseName(string_ref path)
Returns the directory portion of a path.
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:7
bool exists(string_ref filename)
Does this file (directory) exists?
int rmdir(const std::string &path)
Call rmdir() in a platform-independent manner.
string_ref stripExtension(string_ref path)
Returns the path without extension.
const string & getUserOpenMSXDir()
Get the openMSX dir in the user's home directory.
void mkdir(const string &path, mode_t mode)
Create the specified directory.
bool isDirectory(const Stat &st)
string_ref getExtension(string_ref path)
Returns the extension portion of a path.
string getTempDir()
Get the name of the temp directory on the system.
string expandTilde(string_ref path)
Expand the '~' character to the users home directory.
FILE_t openFile(const std::string &filename, const std::string &mode)
Call fopen() in a platform-independent manner.
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.
std::unique_ptr< FILE, FClose > FILE_t