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

Classes

struct  FClose
 

Typedefs

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

Functions

string expandTilde (string_view path)
 
void mkdir (const std::string &path, mode_t mode)
 Create the specified directory. More...
 
void mkdirp (string_view path_)
 
int unlink (const std::string &path)
 Call unlink() in a platform-independent manner. More...
 
int rmdir (const std::string &path)
 Call rmdir() in a platform-independent manner. More...
 
int deleteRecursive (const std::string &path)
 Recurively delete a file or directory and (in case of a directory) all its sub-components. More...
 
FILE_t openFile (const std::string &filename, const std::string &mode)
 Call fopen() in a platform-independent manner. More...
 
void openofstream (std::ofstream &stream, const std::string &filename)
 Open an ofstream in a platform-independent manner. More...
 
void openofstream (std::ofstream &stream, const std::string &filename, std::ios_base::openmode mode)
 Open an ofstream in a platform-independent manner. More...
 
string_view getFilename (string_view path)
 
string_view getDirName (string_view path)
 
string_view getExtension (string_view path)
 
string_view stripExtension (string_view path)
 
string join (string_view part1, string_view part2)
 
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 getNativePath (string_view path)
 
string getConventionalPath (string_view path)
 
string getCurrentWorkingDirectory ()
 Returns the current working directory. More...
 
string getAbsolutePath (string_view path)
 
bool isAbsolutePath (string_view path)
 
string getUserHomeDir (string_view username)
 
const string & getUserOpenMSXDir ()
 Get the openMSX dir in the user's home directory. More...
 
string getUserDataDir ()
 Get the openMSX data dir in the user's home directory. More...
 
string getSystemDataDir ()
 Get system directory. More...
 
string expandCurrentDirFromDrive (string_view path)
 
bool getStat (string_view filename_, Stat &st)
 
bool isRegularFile (const Stat &st)
 
bool isRegularFile (string_view filename)
 
bool isDirectory (const Stat &st)
 
bool isDirectory (string_view directory)
 
bool exists (string_view filename)
 
time_t getModificationDate (const Stat &st)
 Get the date/time of last modification. More...
 
string getNextNumberedFileName (string_view directory, string_view prefix, string_view extension)
 
string parseCommandFileArgument (string_view argument, string_view directory, string_view prefix, string_view extension)
 
string getTempDir ()
 Get the name of the temp directory on the system. More...
 
FILE_t openUniqueFile (const std::string &directory, std::string &filename)
 Open a new file with a unique name in the provided directory. More...
 
std::string expandTilde (std::string_view path)
 Expand the '~' character to the users home directory. More...
 
void mkdirp (std::string_view path)
 Acts like the unix command "mkdir -p". More...
 
std::string_view getFilename (std::string_view path)
 Returns the file portion of a path name. More...
 
std::string_view getDirName (std::string_view path)
 Returns the directory portion of a path. More...
 
std::string_view getExtension (std::string_view path)
 Returns the extension portion of a path. More...
 
std::string_view stripExtension (std::string_view path)
 Returns the path without extension. More...
 
std::string join (std::string_view part1, std::string_view part2)
 Join two paths. More...
 
std::string join (std::string_view part1, std::string_view part2, std::string_view part3)
 
std::string join (std::string_view part1, std::string_view part2, std::string_view part3, std::string_view part4)
 
std::string getConventionalPath (std::string_view path)
 Returns the path in conventional path-delimiter. More...
 
std::string getNativePath (std::string_view path)
 Returns the path in native path-delimiter. More...
 
std::string getAbsolutePath (std::string_view path)
 Transform given path into an absolute path. More...
 
bool isAbsolutePath (std::string_view path)
 Checks whether it's a absolute path or not. More...
 
std::string getUserHomeDir (std::string_view username)
 Get user's home directory. More...
 
std::string expandCurrentDirFromDrive (std::string_view path)
 Get the current directory of the specified drive Linux: just return an empty string. More...
 
bool getStat (std::string_view filename, Stat &st)
 Call stat() and return the stat structure. More...
 
bool isRegularFile (std::string_view filename)
 Is this a regular file (no directory, device, ..)? More...
 
bool isDirectory (std::string_view directory)
 Is this a directory? More...
 
bool exists (std::string_view filename)
 Does this file (directory) exists? More...
 
std::string getNextNumberedFileName (std::string_view directory, std::string_view prefix, std::string_view extension)
 Gets the next numbered file name with the specified prefix in the specified directory, with the specified extension. More...
 
std::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. More...
 

Variables

const char nativePathSeparator
 

Typedef Documentation

◆ FILE_t

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

Definition at line 16 of file FileOperations.hh.

◆ Stat

using openmsx::FileOperations::Stat = typedef struct stat

Definition at line 212 of file FileOperations.hh.

Function Documentation

◆ deleteRecursive()

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

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

Definition at line 322 of file FileOperations.cc.

Referenced by rmdir().

◆ exists() [1/2]

bool openmsx::FileOperations::exists ( std::string_view  filename)

Does this file (directory) exists?

◆ exists() [2/2]

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

◆ expandCurrentDirFromDrive() [1/2]

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

Get the current directory of the specified drive Linux: just return an empty string.

◆ expandCurrentDirFromDrive() [2/2]

string openmsx::FileOperations::expandCurrentDirFromDrive ( string_view  path)

◆ expandTilde() [1/2]

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

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

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

◆ expandTilde() [2/2]

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

◆ getAbsolutePath() [1/2]

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

Transform given path into an absolute path.

Exceptions
FileException

◆ getAbsolutePath() [2/2]

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

◆ getConventionalPath() [1/2]

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

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)

◆ getConventionalPath() [2/2]

string openmsx::FileOperations::getConventionalPath ( string_view  path)

◆ getCurrentWorkingDirectory()

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

Returns the current working directory.

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

Definition at line 469 of file FileOperations.cc.

References MAXPATHLEN, and utf8::utf16to8().

Referenced by getAbsolutePath().

◆ getDirName() [1/2]

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.

◆ getDirName() [2/2]

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

◆ getExtension() [1/2]

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.

◆ getExtension() [2/2]

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

Definition at line 409 of file FileOperations.cc.

References openmsx::filename, and getFilename().

Referenced by openmsx::CommandLineParser::registerFileType().

◆ getFilename() [1/2]

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

Returns the file portion of a path name.

Parameters
pathThe pathname
Returns
The file portion

◆ getFilename() [2/2]

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

◆ getModificationDate()

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

Get the date/time of last modification.

Definition at line 681 of file FileOperations.cc.

Referenced by openmsx::FilePool::getFile().

◆ getNativePath() [1/2]

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

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)

◆ getNativePath() [2/2]

string openmsx::FileOperations::getNativePath ( string_view  path)

◆ getNextNumberedFileName() [1/2]

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

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

◆ getNextNumberedFileName() [2/2]

string openmsx::FileOperations::getNextNumberedFileName ( string_view  directory,
string_view  prefix,
string_view  extension 
)

◆ getStat() [1/2]

bool openmsx::FileOperations::getStat ( std::string_view  filename,
Stat st 
)

Call stat() and return the stat structure.

Parameters
filenamethe file path (will be tilde expanded)
stThe stat structute that will be filled in
Returns
true iff success

◆ getStat() [2/2]

bool openmsx::FileOperations::getStat ( string_view  filename_,
Stat st 
)

◆ getSystemDataDir()

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 565 of file FileOperations.cc.

References ad_printf, openmsx::filename, getAbsolutePath(), getConventionalPath(), MAXPATHLEN, utf8::unchecked::size(), and utf8::utf16to8().

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

◆ 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 762 of file FileOperations.cc.

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

Referenced by openmsx::LocalFileReference::LocalFileReference().

◆ getUserDataDir()

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 558 of file FileOperations.cc.

References getUserOpenMSXDir().

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

◆ getUserHomeDir() [1/2]

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

◆ getUserHomeDir() [2/2]

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

Definition at line 514 of file FileOperations.cc.

References getConventionalPath(), MAXPATHLEN, and utf8::utf16to8().

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 544 of file FileOperations.cc.

References expandTilde(), and strCat().

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

◆ isAbsolutePath() [1/2]

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

Checks whether it's a absolute path or not.

Parameters
pathThe pathname.

◆ isAbsolutePath() [2/2]

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

Definition at line 499 of file FileOperations.cc.

Referenced by openmsx::Completer::completeFileName(), getAbsolutePath(), and join().

◆ isDirectory() [1/3]

bool openmsx::FileOperations::isDirectory ( std::string_view  directory)

Is this a directory?

◆ isDirectory() [2/3]

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

◆ isDirectory() [3/3]

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

Definition at line 669 of file FileOperations.cc.

References getStat().

Referenced by mkdirp().

◆ isRegularFile() [1/3]

bool openmsx::FileOperations::isRegularFile ( std::string_view  filename)

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

◆ isRegularFile() [2/3]

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

◆ isRegularFile() [3/3]

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

Definition at line 658 of file FileOperations.cc.

References getStat().

◆ join() [1/6]

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 inbetween 'part1' and 'part2'.

◆ join() [2/6]

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

◆ join() [3/6]

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

◆ join() [4/6]

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

◆ join() [5/6]

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

Definition at line 440 of file FileOperations.cc.

References join().

◆ join() [6/6]

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

Definition at line 445 of file FileOperations.cc.

References join().

◆ mkdir()

void openmsx::FileOperations::mkdir ( const std::string &  path,
mode_t  mode 
)

Create the specified directory.

Does some sanity checks so that it bahaves the same on all platforms. The mode parameter is ignored on windows. For compatibility with *nix creating the root dir (or a drivename) is not an error instead the operation is silently ignored. This function can only create one dircetory 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 219 of file FileOperations.cc.

References StringOp::endsWith(), getNativePath(), and utf8::utf8to16().

◆ mkdirp() [1/2]

void openmsx::FileOperations::mkdirp ( std::string_view  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

◆ mkdirp() [2/2]

void openmsx::FileOperations::mkdirp ( string_view  path_)

◆ openFile()

FILE_t openmsx::FileOperations::openFile ( const std::string &  filename,
const std::string &  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 352 of file FileOperations.cc.

References utf8::utf8to16().

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

◆ openofstream() [1/2]

void openmsx::FileOperations::openofstream ( std::ofstream &  stream,
const std::string &  filename 
)

Open an ofstream in a platform-independent manner.

Parameters
streaman ofstream
filenamethe file path

Definition at line 366 of file FileOperations.cc.

References utf8::utf8to16().

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

◆ openofstream() [2/2]

void openmsx::FileOperations::openofstream ( std::ofstream &  stream,
const std::string &  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 377 of file FileOperations.cc.

References utf8::utf8to16().

◆ 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 793 of file FileOperations.cc.

References openmsx::L, utf8::utf16to8(), and utf8::utf8to16().

Referenced by openmsx::LocalFileReference::LocalFileReference().

◆ parseCommandFileArgument() [1/2]

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).

◆ parseCommandFileArgument() [2/2]

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

◆ rmdir()

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

Call rmdir() in a platform-independent manner.

Definition at line 290 of file FileOperations.cc.

References deleteRecursive(), and utf8::utf8to16().

Referenced by openmsx::LocalFileReference::~LocalFileReference().

◆ stripExtension() [1/2]

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.

◆ stripExtension() [2/2]

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

Definition at line 420 of file FileOperations.cc.

Referenced by openmsx::VLM5030::writeControl().

◆ unlink()

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

Variable Documentation

◆ nativePathSeparator

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

Definition at line 18 of file FileOperations.hh.

Referenced by openmsx::LocalFileReference::LocalFileReference().