mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]

Various file system utilities

Files

file  fileUtils.hpp
 Declarations of utilities for working with files.
 
file  fileUtils.cpp
 Definitions of utilities for working with files.
 

Functions

int mx::ioutils::createDirectories (const std::string &path)
 Create a directory or directories. More...
 
std::string mx::ioutils::pathStem (const std::string &fname)
 Get the stem of the filename. More...
 
std::string mx::ioutils::pathFilename (const std::string &fname)
 Get the base filename. More...
 
std::string mx::ioutils::parentPath (const std::string &fname)
 Get the parent path from a filename. More...
 
std::vector< std::string > mx::ioutils::getFileNames (const std::string &directory, const std::string &prefix, const std::string &substr, const std::string &extension)
 Get a list of file names from the specified directory, specifying a prefix, a substring to match, and an extension. More...
 
std::vector< std::string > mx::ioutils::getFileNames (const std::string &directory, const std::string &extension)
 Get a list of file names from the specified directory, specifying the extension. More...
 
std::vector< std::string > mx::ioutils::getFileNames (const std::string &directory)
 Get a list of file names from the specified directory. More...
 
std::string mx::ioutils::fileNamePrependAppend (const std::string &fname, const std::string &prepend, const std::string &append)
 Prepend and/or append strings to a file name, leaving the directory and extension unaltered. More...
 
std::string mx::ioutils::fileNameAppend (const std::string &fname, const std::string &append)
 Append a string to a file name, leaving the directory and extension unaltered. More...
 
std::string mx::ioutils::fileNamePrepend (const std::string &fname, const std::string &prepend)
 Prepend strings to a file name, leaving the directory and extension unaltered. More...
 
std::string mx::ioutils::getSequentialFilename (const std::string &basename, const std::string &extension="", const int startat=0, int ndigit=4)
 Get the next file in a numbered sequence. More...
 
off_t mx::ioutils::fileSize (int fd)
 Get the size in bytes of a file. More...
 
off_t mx::ioutils::fileSize (FILE *f)
 Get the size in bytes of a file pointed to by a FILE pointer. More...
 

Function Documentation

◆ createDirectories()

int mx::ioutils::createDirectories ( const std::string &  path)

Create a directory or directories.

This will create any directories in path that don't exist. It silently ignores already existing directories.

Returns
0 on success, indicating the directories were created or already existed.
-1 on error
Parameters
[in]paththe path of the directory(ies)to create

Definition at line 52 of file fileUtils.cpp.

Referenced by mx::AO::path::dm::basisRoot(), mx::AO::path::dm::M2c(), mx::AO::path::dm::projectedModes(), mx::AO::path::basis::root(), mx::AO::path::dm::root(), mx::AO::path::pupil::root(), mx::AO::path::sys::root(), mx::AO::path::sys::cal::root(), and mx::AO::path::sys::cal::sysResp().

◆ fileNameAppend()

std::string mx::ioutils::fileNameAppend ( const std::string &  fname,
const std::string &  append 
)

Append a string to a file name, leaving the directory and extension unaltered.

Returns
the new file name
Parameters
[in]fnamethe original file name, possibly including a directory and extension
[in]appendis the string to insert at the end of the file name, before the extension

Definition at line 199 of file fileUtils.cpp.

References mx::ioutils::fileNamePrependAppend().

◆ fileNamePrepend()

std::string mx::ioutils::fileNamePrepend ( const std::string &  fname,
const std::string &  prepend 
)

Prepend strings to a file name, leaving the directory and extension unaltered.

Returns
the new file name
Parameters
[in]fnamethe original file name, possibly including a directory and extension
[in]prependis the string to insert at the beginning of the file name after the path

Definition at line 206 of file fileUtils.cpp.

References mx::ioutils::fileNamePrependAppend().

◆ fileNamePrependAppend()

std::string mx::ioutils::fileNamePrependAppend ( const std::string &  fname,
const std::string &  prepend,
const std::string &  append 
)

Prepend and/or append strings to a file name, leaving the directory and extension unaltered.

Returns
the new file name
Parameters
[in]fnamethe original file name, possibly including a directory and extension
[in]prependis the string to insert at the beginning of the file name after the path
[in]appendis the string to insert at the end of the file name, before the extension

Definition at line 181 of file fileUtils.cpp.

Referenced by mx::ioutils::fileNameAppend(), and mx::ioutils::fileNamePrepend().

◆ fileSize() [1/2]

off_t mx::ioutils::fileSize ( FILE *  f)

Get the size in bytes of a file pointed to by a FILE pointer.

Uses fileno to get the associated descriptor, then uses fstat.

Returns
the file size if fd is valid and no errors occur
-1 on an error

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
[in]fan open file

Definition at line 271 of file fileUtils.cpp.

References mx::ioutils::fileSize().

◆ fileSize() [2/2]

off_t mx::ioutils::fileSize ( int  fd)

Get the size in bytes of a file.

Uses fstat.

Returns
the file size if fd is valid and no errors occur
-1 on an error
Parameters
[in]fdan open file descriptor

Definition at line 253 of file fileUtils.cpp.

Referenced by mx::ioutils::fileSize().

◆ getFileNames() [1/3]

std::vector< std::string > mx::ioutils::getFileNames ( const std::string &  directory)

Get a list of file names from the specified directory.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns
a std::vector<std::string> which contains the matching file names.
Parameters
[in]directorythe path to the directory to search. Can not be empty.

Definition at line 175 of file fileUtils.cpp.

References mx::ioutils::getFileNames().

◆ getFileNames() [2/3]

std::vector< std::string > mx::ioutils::getFileNames ( const std::string &  directory,
const std::string &  extension 
)

Get a list of file names from the specified directory, specifying the extension.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns
a std::vector<std::string> which contains the matching file names.
Parameters
[in]directorythe path to the directory to search. Can not be empty.
[in]extensionthe file name extension to search for, if "" then not used. Note that this must include the ".", as in ".ext".

Definition at line 168 of file fileUtils.cpp.

References mx::ioutils::getFileNames().

◆ getFileNames() [3/3]

std::vector< std::string > mx::ioutils::getFileNames ( const std::string &  directory,
const std::string &  prefix,
const std::string &  substr,
const std::string &  extension 
)

Get a list of file names from the specified directory, specifying a prefix, a substring to match, and an extension.

Returns
a std::vector<std::string> which contains the matching file names.
Parameters
[in]directorythe path to the directory to search. Can not be empty.
[in]prefixthe file name prefix (the beginning characters of the file name) to search for, if "" then not used.
[in]substra substring of the filename to earch for, if "" then not used.
[in]extensionthe file name extension to search for, if "" then not used. Note that this must include the ".", as in".ext".

Definition at line 85 of file fileUtils.cpp.

Referenced by mx::ioutils::getFileNames(), mx::improc::HCIobservation< _realT >::load_fileList(), mx::improc::HCIobservation< _realT >::load_RDIfileList(), mx::improc::ADIobservation< _realT, _derotFunctObj >::readPSFSub(), and mx::improc::HCIobservation< _realT >::readPSFSub().

◆ getSequentialFilename()

std::string mx::ioutils::getSequentialFilename ( const std::string &  basename,
const std::string &  extension = "",
const int  startat = 0,
int  ndigit = 4 
)

Get the next file in a numbered sequence.

Searches for files in the path designated by basename of the form basenameXXXXextension where the number of digits in XXXX is set by the ndigit parameter.

Warning
this does not currently detect missing files in the sequence, e.g. if you have 0,1,3 in the directory this will start with 2!
Todo:
switch to using a regex or something so we can detect the missing file.
Return values
std::stringcontaining the next filename.
Test:
Verify creation of sequential file names [test doc]
Parameters
[in]basenamepath and initial name of the file
[in]extension[optional] extension to append after the number. Default is empty.
[in]startat[optional] number to start the search from. Default is 0.
[in]ndigit[optional] number of digits in string representation of the number. Default is 4.

Definition at line 213 of file fileUtils.cpp.

Referenced by SCENARIO(), and mx::improc::HCIobservation< _realT >::writeFinim().

◆ parentPath()

std::string mx::ioutils::parentPath ( const std::string &  fname)

Get the parent path from a filename.

Returns
the parent path of the file

Definition at line 77 of file fileUtils.cpp.

Referenced by mx::astro::phoenixSpectrum< _units >::readSpectrum(), and mx::astro::rewritePhoenixSpectrum().

◆ pathFilename()

std::string mx::ioutils::pathFilename ( const std::string &  fname)

◆ pathStem()

std::string mx::ioutils::pathStem ( const std::string &  fname)

Get the stem of the filename.

Returns
the stem for the filename, that is without the path or extension

Definition at line 65 of file fileUtils.cpp.