Jetson Inference
DNN Vision Library

Functions for listing files in directories and manipulating file paths. More...

Enumerations

enum  fileTypes {
  FILE_MISSING = 0, FILE_REGULAR = (1 << 0), FILE_DIR = (1 << 1), FILE_LINK = (1 << 2),
  FILE_CHAR = (1 << 3), FILE_BLOCK = (1 << 4), FILE_FIFO = (1 << 5), FILE_SOCKET = (1 << 6)
}
 File types. More...
 

Functions

std::string absolutePath (const std::string &relative_path)
 Given a relative path, resolve the absolute path using the working directory. More...
 
std::string locateFile (const std::string &path)
 Locate a file from common system locations. More...
 
std::string locateFile (const std::string &path, std::vector< std::string > &locations)
 Locate a file from a set of locations provided by the user, in addition to common system locations such as "/opt" and "/usr/local". More...
 
std::string pathJoin (const std::string &a, const std::string &b)
 Join two paths, and properly include a path separator (/) as needed. More...
 
std::string pathDir (const std::string &path)
 Return the parent directory of the specified path, removing the filename and extension. More...
 
bool listDir (const std::string &path, std::vector< std::string > &list, uint32_t mask=0)
 Return a sorted list of the files in the specified directory. More...
 
bool fileExists (const std::string &path, uint32_t mask=0)
 Verify path and return true if the file exists. More...
 
bool fileIsType (const std::string &path, uint32_t mask)
 Return true if the file is one of the types in the fileTypes mask. More...
 
uint32_t fileType (const std::string &path)
 Return the file type, or FILE_MISSING if it doesn't exist. More...
 
size_t fileSize (const std::string &path)
 Return the size (in bytes) of the specified file. More...
 
std::string fileExtension (const std::string &path)
 Extract the file extension from the path. More...
 
bool fileHasExtension (const std::string &path, const std::string &extension)
 Return true if the file has the given extension, otherwise false. More...
 
bool fileHasExtension (const std::string &path, const std::vector< std::string > &extensions)
 Return true if the file has one of the given extensions, otherwise false. More...
 
bool fileHasExtension (const std::string &path, const char **extensions)
 Return true if the file has one of the given extensions, otherwise false. More...
 
std::string fileRemoveExtension (const std::string &filename)
 Return the input string with the file extension removed For example, fileRemoveExtension("~/workspace/somefile.xml") would return ~/user/somefile. More...
 
std::string fileChangeExtension (const std::string &filename, const std::string &newExtension)
 Return the input string with a changed file extension For example, fileChangeExtension("~/workspace/somefile.xml", "zip") would return ~/user/somefile.zip. More...
 
std::string processPath ()
 Return the absolute path that of the calling process executable, include the process executable's filename. More...
 
std::string processDirectory ()
 Return the directory that the calling process resides in. More...
 
std::string workingDirectory ()
 Return the current working directory of the calling process. More...
 

Detailed Description

Functions for listing files in directories and manipulating file paths.

Enumeration Type Documentation

◆ fileTypes

enum fileTypes

File types.

Enumerator
FILE_MISSING 
FILE_REGULAR 
FILE_DIR 
FILE_LINK 
FILE_CHAR 
FILE_BLOCK 
FILE_FIFO 
FILE_SOCKET 

Function Documentation

◆ absolutePath()

std::string absolutePath ( const std::string &  relative_path)

Given a relative path, resolve the absolute path using the working directory.

For example, if the current working directory /home/user/ and absolutePath("resources/example") is called, then this function would return the path /home/user/resources/example.

If the path is already an absolute path (i.e. it begins with / or ~/) then this function will be ignored and the path will be returned as-is.

◆ fileChangeExtension()

std::string fileChangeExtension ( const std::string &  filename,
const std::string &  newExtension 
)

Return the input string with a changed file extension For example, fileChangeExtension("~/workspace/somefile.xml", "zip") would return ~/user/somefile.zip.

◆ fileExists()

bool fileExists ( const std::string &  path,
uint32_t  mask = 0 
)

Verify path and return true if the file exists.

Parameters
maskfilter by file type (by default, any file including directories will be checked). The mask should consist of fileTypes OR'd together (e.g. FILE_REGULAR|FILE_DIR).

◆ fileExtension()

std::string fileExtension ( const std::string &  path)

Extract the file extension from the path.

This function will return all contents of the path to the right of the right-most `'.'` The extension will be returned in all lowercase characters.

◆ fileHasExtension() [1/3]

bool fileHasExtension ( const std::string &  path,
const std::string &  extension 
)

Return true if the file has the given extension, otherwise false.

For example, fileHasExtension("~/workspace/image.jpg", "jpg") would return true.

◆ fileHasExtension() [2/3]

bool fileHasExtension ( const std::string &  path,
const std::vector< std::string > &  extensions 
)

Return true if the file has one of the given extensions, otherwise false.

◆ fileHasExtension() [3/3]

bool fileHasExtension ( const std::string &  path,
const char **  extensions 
)

Return true if the file has one of the given extensions, otherwise false.

For example, fileHasExtension("image.jpg", {"jpg", "jpeg", NULL}) would return true.

Parameters
extensionslist of extensions, should end with NULL sentinel.

◆ fileIsType()

bool fileIsType ( const std::string &  path,
uint32_t  mask 
)

Return true if the file is one of the types in the fileTypes mask.

Parameters
maskfile types to check against (
See also
fileTypes) The mask should consist of fileTypes OR'd together (e.g. FILE_REGULAR|FILE_DIR).

◆ fileRemoveExtension()

std::string fileRemoveExtension ( const std::string &  filename)

Return the input string with the file extension removed For example, fileRemoveExtension("~/workspace/somefile.xml") would return ~/user/somefile.

◆ fileSize()

size_t fileSize ( const std::string &  path)

Return the size (in bytes) of the specified file.

Parameters
paththe path of the file
Returns
if successful, the size of the file in bytes otherwise, 0 will be returned.

◆ fileType()

uint32_t fileType ( const std::string &  path)

Return the file type, or FILE_MISSING if it doesn't exist.

See also
fileTypes

◆ listDir()

bool listDir ( const std::string &  path,
std::vector< std::string > &  list,
uint32_t  mask = 0 
)

Return a sorted list of the files in the specified directory.

listDir() will glob files from the specified path, and filter against wildcard characters including * and ?. For example, valid paths would include ~/workspace, ~/workspace/*.jpg, ect.

See also
here for a description of wildcard matching: https://www.man7.org/linux/man-pages/man7/glob.7.html
Parameters
paththe path of the directory (may include wildcard characters)
[out]listthe alphanumerically sorted output list of the files in the directory
maskfilter by file type (by default, any file including directories will be included). The mask should consist of fileTypes OR'd together (e.g. FILE_REGULAR|FILE_DIR).

◆ locateFile() [1/2]

std::string locateFile ( const std::string &  path)

Locate a file from common system locations.

First, this function will check if the file exists at the path provided, and if not it will check for the existance of the file in common system locations such as "/opt", "/usr/local", and "/usr/local/bin".

Returns
the confirmed path of the located file, or empty string if the file could not be found

◆ locateFile() [2/2]

std::string locateFile ( const std::string &  path,
std::vector< std::string > &  locations 
)

Locate a file from a set of locations provided by the user, in addition to common system locations such as "/opt" and "/usr/local".

Returns
the confirmed path of the located file, or empty string if the file could not be found

◆ pathDir()

std::string pathDir ( const std::string &  path)

Return the parent directory of the specified path, removing the filename and extension.

For example, pathDir("~/workspace/somefile.xml") would return ~/workspace/

◆ pathJoin()

std::string pathJoin ( const std::string &  a,
const std::string &  b 
)

Join two paths, and properly include a path separator (/) as needed.

For example, 'pathJoin("~/workspace", "somefile.xml")would return~/workspace/somefile.xml`.

◆ processDirectory()

std::string processDirectory ( )

Return the directory that the calling process resides in.

For example, if the process executable is located at /usr/bin/exe, then processDirectory() would return the path /usr/bin.

Note
to retrieve the full path of the calling process, including the process executable's filename,
See also
processPath()

◆ processPath()

std::string processPath ( )

Return the absolute path that of the calling process executable, include the process executable's filename.

◆ workingDirectory()

std::string workingDirectory ( )

Return the current working directory of the calling process.