openMSX
Classes | Typedefs | Functions | Variables
openmsx::FileOperations Namespace Reference

Classes

struct  FClose
 

Typedefs

using FILE_t = std::unique_ptr< FILE, FClose >
 
using Stat = stat
 

Functions

bool needsTildeExpansion (std::string_view path)
 Returns true iff expandTilde(s) would have an effect.
 
string expandTilde (std::string path)
 Expand the '~' character to the users home directory.
 
void mkdir (zstring_view path, mode_t mode)
 Create the specified directory.
 
void mkdirp (std::string path)
 Acts like the unix command "mkdir -p".
 
int unlink (zstring_view path)
 Call unlink() in a platform-independent manner.
 
int rmdir (zstring_view path)
 Call rmdir() in a platform-independent manner.
 
int deleteRecursive (const std::string &path)
 
FILE_t openFile (zstring_view filename, zstring_view mode)
 Call fopen() in a platform-independent manner.
 
void openOfStream (std::ofstream &stream, zstring_view filename)
 Open an ofstream in a platform-independent manner.
 
void openOfStream (std::ofstream &stream, zstring_view filename, std::ios_base::openmode mode)
 Open an ofstream in a platform-independent manner.
 
string_view getFilename (std::string_view path)
 Returns the file portion of a path name.
 
string_view getDirName (std::string_view path)
 Returns the directory portion of a path.
 
string_view getExtension (std::string_view path)
 Returns the extension portion of a path.
 
string_view stripExtension (std::string_view path)
 Returns the path without extension.
 
string join (std::string_view part1, std::string_view part2)
 Join two paths.
 
string join (string_view part1, string_view part2, string_view part3)
 
string join (string_view part1, string_view part2, string_view part3, string_view part4)
 
string getCurrentWorkingDirectory ()
 Returns the current working directory.
 
string getAbsolutePath (std::string_view path)
 Transform given path into an absolute path.
 
bool isAbsolutePath (std::string_view path)
 Checks whether it's a absolute path or not.
 
string getUserHomeDir (std::string_view username)
 Get user's home directory.
 
const string & getUserOpenMSXDir ()
 Get the openMSX dir in the user's home directory.
 
const string & getUserDataDir ()
 Get the openMSX data dir in the user's home directory.
 
const string & getSystemDataDir ()
 Get system directory.
 
const string & getSystemDocDir ()
 Get system doc directory.
 
std::optional< StatgetStat (zstring_view filename)
 Call stat() and return the stat structure.
 
bool isRegularFile (const Stat &st)
 
bool isRegularFile (zstring_view filename)
 Is this a regular file (no directory, device, ..)?
 
bool isDirectory (const Stat &st)
 
bool isDirectory (zstring_view directory)
 Is this a directory?
 
bool exists (zstring_view filename)
 Does this file (directory) exists?
 
string getNextNumberedFileName (std::string_view directory, std::string_view prefix, std::string_view extension, bool addSeparator=false)
 Gets the next numbered file name with the specified prefix in the specified directory, with the specified extension.
 
string parseCommandFileArgument (std::string_view argument, std::string_view directory, std::string_view prefix, std::string_view extension)
 Helper function for parsing filename arguments in Tcl commands.
 
string getTempDir ()
 Get the name of the temp directory on the system.
 
FILE_t openUniqueFile (const std::string &directory, std::string &filename)
 Open a new file with a unique name in the provided directory.
 
int deleteRecursive (zstring_view path)
 Recursively delete a file or directory and (in case of a directory) all its sub-components.
 
const std::string & getConventionalPath (const std::string &path)
 Returns the path in conventional path-delimiter.
 
std::string getConventionalPath (std::string &&path)
 
const std::string & getNativePath (const std::string &path)
 Returns the path in native path-delimiter.
 
std::string getNativePath (std::string &&path)
 
const std::string & expandCurrentDirFromDrive (const std::string &path)
 Get the current directory of the specified drive Linux: return the given string unchanged.
 
std::string expandCurrentDirFromDrive (std::string &&path)
 
time_t getModificationDate (const Stat &st)
 Get the date/time of last modification.
 
std::string findResourceDir (const std::string &resourceDirName)
 

Variables

const char nativePathSeparator
 

Typedef Documentation

◆ FILE_t

using openmsx::FileOperations::FILE_t = typedef std::unique_ptr<FILE, FClose>

Definition at line 19 of file FileOperations.hh.

◆ Stat

using openmsx::FileOperations::Stat = typedef stat

Definition at line 242 of file FileOperations.hh.

Function Documentation

◆ deleteRecursive() [1/2]

int openmsx::FileOperations::deleteRecursive ( const std::string &  path)

◆ deleteRecursive() [2/2]

int openmsx::FileOperations::deleteRecursive ( zstring_view  path)

Recursively delete a file or directory and (in case of a directory) all its sub-components.

◆ exists()

bool openmsx::FileOperations::exists ( zstring_view  filename)

◆ expandCurrentDirFromDrive() [1/2]

const std::string & openmsx::FileOperations::expandCurrentDirFromDrive ( const std::string &  path)
inline

Get the current directory of the specified drive Linux: return the given string unchanged.

Definition at line 181 of file FileOperations.hh.

◆ expandCurrentDirFromDrive() [2/2]

std::string openmsx::FileOperations::expandCurrentDirFromDrive ( std::string &&  path)
inline

Definition at line 182 of file FileOperations.hh.

◆ expandTilde()

std::string openmsx::FileOperations::expandTilde ( std::string  path)

Expand the '~' character to the users home directory.

Parameters
pathPathname, with or without '~' character
Returns
The expanded pathname

Definition at line 84 of file FileOperations.cc.

References getUserHomeDir(), and needsTildeExpansion().

Referenced by openmsx::NowindCommand::execute(), openmsx::RestoreMachineCommand::execute(), openmsx::FilePoolCore::getFile(), getUserOpenMSXDir(), and parseCommandFileArgument().

◆ findResourceDir()

std::string openmsx::FileOperations::findResourceDir ( const std::string &  resourceDirName)

◆ getAbsolutePath()

std::string openmsx::FileOperations::getAbsolutePath ( std::string_view  path)

Transform given path into an absolute path.

Exceptions
FileException

Definition at line 364 of file FileOperations.cc.

References getCurrentWorkingDirectory(), isAbsolutePath(), and join().

Referenced by openmsx::HardwareConfig::createRomConfig(), getSystemDataDir(), getSystemDocDir(), and openmsx::Filename::updateAfterLoadState().

◆ getConventionalPath() [1/2]

const std::string & openmsx::FileOperations::getConventionalPath ( const std::string &  path)
inline

Returns the path in conventional path-delimiter.

Parameters
pathThe pathname.
Returns
The path in conventional path-delimiter. On UNI*Y systems, it will have no effect indeed. Just for portability issue. (Especially for Win32)

Definition at line 156 of file FileOperations.hh.

Referenced by openmsx::ImGuiOpenFile::doPaint(), getSystemDataDir(), getSystemDocDir(), getUserHomeDir(), mkdirp(), and openmsx::CommandLineParser::parse().

◆ getConventionalPath() [2/2]

std::string openmsx::FileOperations::getConventionalPath ( std::string &&  path)
inline

Definition at line 157 of file FileOperations.hh.

◆ getCurrentWorkingDirectory()

std::string openmsx::FileOperations::getCurrentWorkingDirectory ( )

Returns the current working directory.

Exceptions
FileException(for example when directory has been deleted).

Definition at line 345 of file FileOperations.cc.

References MAXPATHLEN.

Referenced by getAbsolutePath().

◆ getDirName()

std::string_view openmsx::FileOperations::getDirName ( std::string_view  path)

Returns the directory portion of a path.

Parameters
pathThe pathname
Returns
The directory portion. This includes the ending '/'. If path doesn't have a directory portion the result is an empty string.

Definition at line 285 of file FileOperations.cc.

Referenced by parseCommandFileArgument().

◆ getExtension()

std::string_view openmsx::FileOperations::getExtension ( std::string_view  path)

Returns the extension portion of a path.

Parameters
pathThe pathname
Returns
The extension portion. This excludes the '.'. If path doesn't have an extension portion the result is an empty string.

Definition at line 293 of file FileOperations.cc.

References getFilename().

Referenced by openmsx::ImGuiManager::paintImGui().

◆ getFilename()

std::string_view openmsx::FileOperations::getFilename ( std::string_view  path)

◆ getModificationDate()

time_t openmsx::FileOperations::getModificationDate ( const Stat st)
inline

Get the date/time of last modification.

Is guaranteed to not be INVALID_TIME_T.

Definition at line 270 of file FileOperations.hh.

References openmsx::Date::adjustTimeT().

◆ getNativePath() [1/2]

const std::string & openmsx::FileOperations::getNativePath ( const std::string &  path)
inline

Returns the path in native path-delimiter.

Parameters
pathThe pathname.
Returns
The path in native path-delimiter. On UNI*Y systems, it will have no effect indeed. Just for portability issue. (Especially for Win32)

Definition at line 170 of file FileOperations.hh.

Referenced by openmsx::LocalFile::LocalFile(), openmsx::LocalFile::LocalFile(), mkdir(), and openmsx::LocalFile::preCacheFile().

◆ getNativePath() [2/2]

std::string openmsx::FileOperations::getNativePath ( std::string &&  path)
inline

Definition at line 171 of file FileOperations.hh.

◆ getNextNumberedFileName()

std::string openmsx::FileOperations::getNextNumberedFileName ( std::string_view  directory,
std::string_view  prefix,
std::string_view  extension,
bool  addSeparator = false 
)

Gets the next numbered file name with the specified prefix in the specified directory, with the specified extension.

Examples: automatic numbering of filenames for new screenshots or sound logs.

Parameters
directoryName of the directory in the openMSX user dir in which should be searched for the next filename
prefixPrefix of the filename with numbers
extensionExtension of the filename with numbers
addSeparatorAdd separator character between 'prefix' and 'number'.

Definition at line 619 of file FileOperations.cc.

References openmsx::ReadDir::getEntry(), getUserOpenMSXDir(), join(), mkdirp(), and strCat().

Referenced by openmsx::StoreMachineCommand::execute(), parseCommandFileArgument(), openmsx::Paper::save(), and openmsx::ImGuiReverseBar::showMenu().

◆ getStat()

std::optional< Stat > openmsx::FileOperations::getStat ( zstring_view  filename)

Call stat() and return the stat structure.

Parameters
filenamethe file path

Definition at line 548 of file FileOperations.cc.

References zstring_view::c_str().

Referenced by exists(), openmsx::detail::foreach_dirent(), isDirectory(), and isRegularFile().

◆ getSystemDataDir()

const std::string & openmsx::FileOperations::getSystemDataDir ( )

Get system directory.

UNI*Y: statically defined as "/opt/openMSX/share". Win32: use "same directory as .exe" + "/share".

Definition at line 448 of file FileOperations.cc.

References findResourceDir(), getAbsolutePath(), getConventionalPath(), and MAXPATHLEN.

Referenced by getTempDir(), and openmsx::Interpreter::Interpreter().

◆ getSystemDocDir()

const std::string & openmsx::FileOperations::getSystemDocDir ( )

Get system doc directory.

Win32: use "same directory as .exe" + "/doc".

Definition at line 482 of file FileOperations.cc.

References findResourceDir(), getAbsolutePath(), getConventionalPath(), and MAXPATHLEN.

Referenced by openmsx::ImGuiHelp::showMenu().

◆ getTempDir()

std::string openmsx::FileOperations::getTempDir ( )

Get the name of the temp directory on the system.

Typically /tmp on *nix and C:/WINDOWS/TEMP on windows

Definition at line 681 of file FileOperations.cc.

References getSystemDataDir(), openmsx::L, and VLA.

Referenced by TEST_CASE().

◆ getUserDataDir()

const std::string & openmsx::FileOperations::getUserDataDir ( )

Get the openMSX data dir in the user's home directory.

Default value is "~/.openMSX/share" (UNIX) or "~/openMSX/share" (win)

Definition at line 437 of file FileOperations.cc.

References getUserOpenMSXDir().

Referenced by openmsx::Interpreter::Interpreter().

◆ getUserHomeDir()

std::string openmsx::FileOperations::getUserHomeDir ( std::string_view  username)

Get user's home directory.

Parameters
usernameThe name of the user
Returns
Home directory of the user or empty string in case of error UNI*Y: get from env var "HOME" or from /etc/passwd empty string means current user Win32: Currently use "My Documents" as home directory. Not "Documents and Settings". This is because to support Win9x. Ignores the username parameter

Definition at line 388 of file FileOperations.cc.

References getConventionalPath(), and MAXPATHLEN.

Referenced by expandTilde().

◆ getUserOpenMSXDir()

const std::string & openmsx::FileOperations::getUserOpenMSXDir ( )

Get the openMSX dir in the user's home directory.

Default value is "~/.openMSX" (UNIX) or "~/openMSX" (win)

Definition at line 418 of file FileOperations.cc.

References expandTilde(), and strCat().

Referenced by openmsx::RestoreMachineCommand::execute(), getNextNumberedFileName(), getUserDataDir(), and parseCommandFileArgument().

◆ isAbsolutePath()

bool openmsx::FileOperations::isAbsolutePath ( std::string_view  path)

Checks whether it's a absolute path or not.

Parameters
pathThe pathname.

Definition at line 374 of file FileOperations.cc.

Referenced by getAbsolutePath(), join(), and mkdirp().

◆ isDirectory() [1/2]

bool openmsx::FileOperations::isDirectory ( const Stat st)

◆ isDirectory() [2/2]

bool openmsx::FileOperations::isDirectory ( zstring_view  directory)

Is this a directory?

Definition at line 590 of file FileOperations.cc.

References getStat(), and isDirectory().

◆ isRegularFile() [1/2]

bool openmsx::FileOperations::isRegularFile ( const Stat st)

◆ isRegularFile() [2/2]

bool openmsx::FileOperations::isRegularFile ( zstring_view  filename)

Is this a regular file (no directory, device, ..)?

Definition at line 579 of file FileOperations.cc.

References getStat(), and isRegularFile().

◆ join() [1/3]

std::string openmsx::FileOperations::join ( std::string_view  part1,
std::string_view  part2 
)

Join two paths.

Returns the equivalent of 'path1 + '/' + path2'. If 'part2' is an absolute path, that path is returned ('part1' is ignored). If 'part1' is empty or if it already ends with '/', there will be no extra '/' added between 'part1' and 'part2'.

Definition at line 310 of file FileOperations.cc.

References isAbsolutePath(), and strCat().

Referenced by getAbsolutePath(), openmsx::Reactor::getHwConfigs(), getNextNumberedFileName(), join(), join(), and openmsx::FileContext::resolveCreate().

◆ join() [2/3]

std::string openmsx::FileOperations::join ( string_view  part1,
string_view  part2,
string_view  part3 
)

Definition at line 320 of file FileOperations.cc.

References join().

◆ join() [3/3]

std::string openmsx::FileOperations::join ( string_view  part1,
string_view  part2,
string_view  part3,
string_view  part4 
)

Definition at line 325 of file FileOperations.cc.

References join().

◆ mkdir()

void openmsx::FileOperations::mkdir ( zstring_view  path,
mode_t  mode 
)

Create the specified directory.

Does some sanity checks so that it behaves the same on all platforms. The mode parameter is ignored on windows. For compatibility with *nix creating the root dir (or a drive name) is not an error instead the operation is silently ignored. This function can only create one directory at-a-time. You probably want to use the mkdirp function (see below).

Parameters
pathThe path of the directory to create
modeThe permission bits (*nix only)
Exceptions
FileException

Definition at line 109 of file FileOperations.cc.

References zstring_view::c_str(), zstring_view::ends_with(), getNativePath(), and mkdir().

Referenced by mkdir(), and mkdirp().

◆ mkdirp()

void openmsx::FileOperations::mkdirp ( std::string  path)

Acts like the unix command "mkdir -p".

Creates the specified directory, including the parent directories.

Parameters
pathThe path of the directory to create
Exceptions
FileException

Definition at line 135 of file FileOperations.cc.

References getConventionalPath(), isAbsolutePath(), isDirectory(), and mkdir().

Referenced by getNextNumberedFileName(), openmsx::LocalFile::LocalFile(), parseCommandFileArgument(), openmsx::FileContext::resolveCreate(), and TEST_CASE().

◆ needsTildeExpansion()

bool openmsx::FileOperations::needsTildeExpansion ( std::string_view  path)

Returns true iff expandTilde(s) would have an effect.

In other words: expandTilde(s) != s Mostly useful to assert that tilde expansion has already been done.

Definition at line 79 of file FileOperations.cc.

Referenced by expandTilde(), openmsx::FileContext::resolve(), and openmsx::FileContext::resolveCreate().

◆ openFile()

FILE_t openmsx::FileOperations::openFile ( zstring_view  filename,
zstring_view  mode 
)

Call fopen() in a platform-independent manner.

Parameters
filenamethe file path
modethe mode parameter, same as fopen
Returns
A pointer to the opened file, or nullptr on error On error the global variable 'errno' is filled in (see man fopen for details).

Definition at line 240 of file FileOperations.cc.

References zstring_view::c_str(), and zstring_view::find().

Referenced by openmsx::LocalFile::LocalFile(), openmsx::LocalFile::LocalFile(), openmsx::MidiInReader::plugHelper(), openmsx::RS232Tester::plugHelper(), and openmsx::XmlOutputArchive::XmlOutputArchive().

◆ openOfStream() [1/2]

void openmsx::FileOperations::openOfStream ( std::ofstream &  stream,
zstring_view  filename 
)

Open an ofstream in a platform-independent manner.

Parameters
streaman ofstream
filenamethe file path

Definition at line 254 of file FileOperations.cc.

References zstring_view::c_str().

Referenced by openmsx::MidiOutLogger::plugHelper(), and openmsx::RS232Tester::plugHelper().

◆ openOfStream() [2/2]

void openmsx::FileOperations::openOfStream ( std::ofstream &  stream,
zstring_view  filename,
std::ios_base::openmode  mode 
)

Open an ofstream in a platform-independent manner.

Parameters
streaman ofstream
filenamethe file path
modethe open mode

Definition at line 265 of file FileOperations.cc.

References zstring_view::c_str().

◆ openUniqueFile()

FILE_t openmsx::FileOperations::openUniqueFile ( const std::string &  directory,
std::string &  filename 
)

Open a new file with a unique name in the provided directory.

Parameters
directorydirectory in which to open the temp file
filename[output param] the name of the resulting file
Returns
pointer to the opened file

Definition at line 712 of file FileOperations.cc.

References openmsx::L.

◆ parseCommandFileArgument()

std::string openmsx::FileOperations::parseCommandFileArgument ( std::string_view  argument,
std::string_view  directory,
std::string_view  prefix,
std::string_view  extension 
)

Helper function for parsing filename arguments in Tcl commands.

  • If argument is empty then getNextNumberedFileName() is used with given directory, prefix and extension.
  • If argument doesn't already end with the given extension that extension is appended.
  • If argument doesn't already include a directory, the given directory is used (and created if required).

Definition at line 652 of file FileOperations.cc.

References exists(), expandTilde(), getDirName(), getNextNumberedFileName(), getUserOpenMSXDir(), mkdirp(), and strCat().

Referenced by openmsx::ImGuiReverseBar::showMenu().

◆ rmdir()

int openmsx::FileOperations::rmdir ( zstring_view  path)

Call rmdir() in a platform-independent manner.

Definition at line 178 of file FileOperations.cc.

References zstring_view::c_str().

Referenced by deleteRecursive().

◆ stripExtension()

std::string_view openmsx::FileOperations::stripExtension ( std::string_view  path)

Returns the path without extension.

Parameters
pathThe pathname
Returns
The path without extension. This excludes the '.'. If path doesn't have an extension portion the result remains unchanged.

Definition at line 302 of file FileOperations.cc.

Referenced by openmsx::ImGuiReverseBar::showMenu(), and openmsx::ImGuiSettings::showMenu().

◆ unlink()

int openmsx::FileOperations::unlink ( zstring_view  path)

Call unlink() in a platform-independent manner.

Definition at line 169 of file FileOperations.cc.

References zstring_view::c_str().

Referenced by deleteRecursive(), openmsx::ImGuiSettings::showMenu(), TEST_CASE(), and openmsx::AviWriter::~AviWriter().

Variable Documentation

◆ nativePathSeparator

const char openmsx::FileOperations::nativePathSeparator
Initial value:
=
'/'

Definition at line 21 of file FileOperations.hh.