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
openmsx::FileOperations::getConventionalPath
string getConventionalPath(string_view path)
Returns the path in conventional path-delimiter.
Definition: FileOperations.cc:461
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:500
openmsx::FileOperations::getStat
bool getStat(string_view filename_, Stat &st)
Call stat() and return the stat structure.
Definition: FileOperations.cc:635
openmsx::FileOperations::Stat
struct stat Stat
Definition: FileOperations.hh:212
openmsx::FileOperations::getAbsolutePath
string getAbsolutePath(string_view path)
Transform given path into an absolute path.
Definition: FileOperations.cc:489
openmsx::FileOperations::isRegularFile
bool isRegularFile(const Stat &st)
Definition: FileOperations.cc:654
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:452
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:431
openmsx::FileOperations::exists
bool exists(string_view filename)
Does this file (directory) exists?
Definition: FileOperations.cc:675
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:565
openmsx::FileOperations::getCurrentWorkingDirectory
string getCurrentWorkingDirectory()
Returns the current working directory.
Definition: FileOperations.cc:470
openmsx::FileOperations::getDirName
string_view getDirName(string_view path)
Returns the directory portion of a path.
Definition: FileOperations.cc:400
openmsx::FileOperations::getUserDataDir
string getUserDataDir()
Get the openMSX data dir in the user's home directory.
Definition: FileOperations.cc:558
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:544
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:608
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:732
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:793
openmsx::FileOperations::getTempDir
string getTempDir()
Get the name of the temp directory on the system.
Definition: FileOperations.cc:762
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:664
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:514
openmsx::FileOperations::getExtension
string_view getExtension(string_view path)
Returns the extension portion of a path.
Definition: FileOperations.cc:410
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:421
openmsx::FileOperations::getModificationDate
time_t getModificationDate(const Stat &st)
Get the date/time of last modification.
Definition: FileOperations.cc:681
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:705
openmsx::FileOperations::getFilename
string_view getFilename(string_view path)
Returns the file portion of a path name.
Definition: FileOperations.cc:390