IoTools¶
- class IoTools¶
Bases:
pybind11_object
File and directories basic tools.
The example below shows how to manipulate the functions of this class to create first a directory which name corresponds to the user name and then create a file in this directory.
#include <fstream> #include <iostream> #include <string> #include <visp3/core/vpIoTools.h> int main() { std::string username; vpIoTools::getUserName(username); // Test if a username directory exist. If no try to create it if (vpIoTools::checkDirectory(username) == false) { try { // Create a directory with name "username" vpIoTools::makeDirectory(username); } catch (...) { std::cout << "Cannot create " << username << " directory" << std::endl; return EXIT_FAILURE; } } // Create a empty filename with name "username/file.txt" std::ofstream f; std::string filename = username + "/file.txt"; // Under Windows converts the filename string into "username\\file.txt" filename = vpIoTools::path(filename); std::cout << "Create: " << filename << std::endl; f.open(filename.c_str()); f.close(); // Rename the file std::string newfilename = username + "/newfile.txt"; std::cout << "Rename: " << filename << " in: " << newfilename << std::endl; if (vpIoTools::rename(filename, newfilename) == false) std::cout << "Unable to rename: " << filename << std::endl; // Remove the file std::cout << "Remove: " << newfilename << std::endl; if (vpIoTools::remove(newfilename) == false) std::cout << "Unable to remove: " << newfilename << std::endl; return EXIT_SUCCESS; }
The example below shows how to read a configuration file and how to create a name for experiment files. We assume the following file “/home/user/demo/config.txt” :
expNumber 2 save 0 lambda 0.4 use2D 0 use3D 1
#include <iostream> #include <string> #include <visp3/core/vpIoTools.h> int main() { // reading configuration file vpIoTools::loadConfigFile("/home/user/demo/config.txt"); std::string nExp;vpIoTools::readConfigVar("expNumber", nExp); // nExp <- "2" double lambda;vpIoTools::readConfigVar("lambda", lambda); // lambda <- 0.4 bool use2D;vpIoTools::readConfigVar("use2D", use2D); // use2D <- false bool use3D;vpIoTools::readConfigVar("use3D", use3D); // use3D <- true bool doSave;vpIoTools::readConfigVar("save", doSave); // doSave <- false // creating name for experiment files vpIoTools::setBaseDir("/home/user/data"); // full name <- "/home/user/data/exp2" vpIoTools::setBaseName("exp" + nExp); // full name <- "/home/user/data/exp2" since use2D==false vpIoTools::addNameElement("2D", use2D); // full name <- "/home/user/data/exp2_3D" vpIoTools::addNameElement("3D", use3D); // full name <- "/home/user/data/exp2_3D_lambda0.4" vpIoTools::addNameElement("lambda", lambda); // Saving file.Would copy "/home/user/demo/config.txt" to // "/home/user/data/exp2_3D_lambda0.4_config.txt" if doSave was true vpIoTools::saveConfigFile(doSave); // create sub directory vpIoTools::createBaseNamePath(); // creates "/home/user/data/exp2_3D_lambda0.4/" }
Methods
Overloaded function.
Check if a directory exists.
Check if a fifo file exists.
Check if a file exists.
Copy a src file or directory in dst .
Creates the directory baseDir/baseName .
Return the file path that corresponds to the concatenated parent and child string files by adding the corresponding separator for unix or windows.
- return:
According to realpath() manual, returns an absolute pathname that names the same file, whose resolution does not involve '.', '..', or symbolic links for Unix systems. According to GetFullPathName() documentation, retrieves the full path of the specified file for Windows systems.
Gets the base name (prefix) of the experiment files.
Return build informations (OS, compiler, build flags, used 3rd parties...).
- return:
A vector of files' names in that directory
Returns the extension of the file or an empty string if the file has no extension.
Gets the full path of the experiment files : baseDir/baseName
Checks file name format and extracts its index.
- return:
The name of the file or directory denoted by this pathname, or an empty string if this pathname's name sequence is empty.
- return:
The name of the file without extension or directory denoted by this pathname, or an empty string if this pathname's name sequence is empty.
Returns the pathname string of this pathname's parent.
Return path to the default temporary folder:
Overloaded function.
Extract major, minor and patch from a version given as "x.x.x".
Get ViSP images data path.
Get the content of an environment variable.
Return whether a path is absolute.
Return true if the two pathnames are identical.
Reads the configuration file and parses it.
Create a new directory.
Create a new FIFO file.
Create a new temporary directory with a unique name based on dirname parameter.
Converts a path name to the current system's format.
Overloaded function.
Tries to read the parameter named var as a bool .
Tries to read the parameter named var as a double .
Tries to read the parameter named var as a float .
Tries to read the parameter named var as a int .
Tries to read the parameter named var as a std::string .
Tries to read the parameter named var as a unsigned int.
Remove a file or a directory.
Rename an existing file oldfilename in newfilename .
Copy the initial configuration file to the experiment directory.
Sets the base directory of the experiment files.
Sets the base name (prefix) of the experiment files.
The following code shows how to use this function:
- return:
a pair whose the first element is the drive specification and the second element the path specification
Return a lower-case version of the string input .
Return a upper-case version of the string input .
Remove leading and trailing whitespaces from a string.
Overloaded function.
Inherited Methods
Operators
__doc__
__module__
Attributes
__annotations__
- __init__(*args, **kwargs)¶
- static addNameElement(*args, **kwargs)¶
Overloaded function.
addNameElement(strTrue: str, cond: bool = true, strFalse: str = ) -> None
Augments the prefix of the experiment files by strTrue if cond is verified, and by strFalse otherwise.
- Parameters:
- strTrue
String to add if cond is true
- cond
Condition managing the file name
- strFalse
String to add if cond is false (default “”)
addNameElement(strTrue: str, val: float) -> None
Augments the prefix of the experiment files by strTrue followed by val .
- Parameters:
- strTrue
String to add
- val
Value to add
- static checkFifo(filename: str) bool ¶
Check if a fifo file exists.
Note
See checkFilename(const std::string &)
- Returns:
true : If the fifo file exists and is accessible with read access.false : If fifofilename string is null, or is not a fifo filename, or has no read access.
- static createBaseNamePath(empty: bool = false) None ¶
Creates the directory baseDir/baseName . If already exists, empties it if empty is true. Useful to save the images corresponding to a particular experiment.
- static createFilePath(parent: str, child: str) str ¶
Return the file path that corresponds to the concatenated parent and child string files by adding the corresponding separator for unix or windows.
The corresponding path is also converted. Under windows, all the “/” characters are converted into “\” characters. Under Unix systems all the “\” characters are converted into “/” characters.
- static getAbsolutePathname(pathname: str) str ¶
- Returns:
According to realpath() manual, returns an absolute pathname that names the same file, whose resolution does not involve ‘.’, ‘..’, or symbolic links for Unix systems. According to GetFullPathName() documentation, retrieves the full path of the specified file for Windows systems.
- static getBaseName() str ¶
Gets the base name (prefix) of the experiment files.
- Returns:
the base name of the experiment files.
- static getBuildInformation() str ¶
Return build informations (OS, compiler, build flags, used 3rd parties…).
- static getFileExtension(pathname: str, checkFile: bool = false) str ¶
Returns the extension of the file or an empty string if the file has no extension. If checkFile flag is set, it will check first if the pathname denotes a directory and so return an empty string and second it will check if the file denoted by the pathanme exists. If so, it will return the extension if present.
The following code shows how to use this function:
#include <visp3/core/vpIoTools.h> int main() { std::string filename = "my/path/to/file.xml" std::string ext = vpIoTools::getFileExtension(opt_learning_data); std::cout << "ext: " << ext << std::endl; }
It produces the following output:
ext: .xml
- Parameters:
- Returns:
The extension of the file including the dot “.” or an empty string if the file has no extension or if the pathname is empty.
- static getFullName() str ¶
Gets the full path of the experiment files : baseDir/baseName
- Returns:
the full path of the experiment files.
- static getIndex(filename: str, format: str) int ¶
Checks file name format and extracts its index.
Format must contain substring “%0xd”, defining the length of image index. For example, format can be “img%04d.jpg”. Then “img0001.jpg” and “img0000.jpg” satisfy it, while “picture001.jpg” and “img001.jpg” don’t.
The following sample code shows how to use this function:
#include <visp3/core/vpIoTools.h> int main() { std::cout << vpIoTools::getIndex("file-1.txt", "file-%d.txt") << std::endl; std::cout << vpIoTools::getIndex("/tmp/file0040.txt", "/tmp/file%04d.txt") << std::endl; std::cout << vpIoTools::getIndex("file.txt", "file%d.txt") << std::endl; std::cout << vpIoTools::getIndex("file03.txt", "file%02d.txt") << std::endl; std::cout << vpIoTools::getIndex("file-03.txt", "file%02d.txt") << std::endl; }
It produces the following output:
1 40 -1 3 -1
- static getName(pathname: str) str ¶
- Returns:
The name of the file or directory denoted by this pathname, or an empty string if this pathname’s name sequence is empty.
- static getNameWE(pathname: str) str ¶
- Returns:
The name of the file without extension or directory denoted by this pathname, or an empty string if this pathname’s name sequence is empty.
- static getParent(pathname: str) str ¶
Returns the pathname string of this pathname’s parent.
For example
when pathname is set to “./executable” it returns “.”
When pathname is set to “folder/executable” it returns “folder”
When pathname is set to “foldersubfolderfile.cpp” it returns “foldersubfolder”
When pathname is set to “executable” it returns “.”
- static getTempPath() str ¶
Return path to the default temporary folder:
on Windows it returns GetTempPath()
on Unix it returns /tmp/<username>
Warning
This function is not implemented on WINRT.
The following sample shows how to use this function to create unique temporary directories:
include <visp3/core/vpIoTools.h> int main() { std::string tmp_path = vpIoTools::getTempPath(); std::cout << "Temp path: " << tmp_path << std::endl; std::string tmp_dir1 = vpIoTools::makeTempDirectory(tmp_path); std::cout << "Created unique temp dir1: " << tmp_dir1 << std::endl; std::string tmp_dir2_template = tmp_path + vpIoTools::path("/") + "dir_XXXXXX"; std::string tmp_dir2 = vpIoTools::makeTempDirectory(tmp_dir2_template); std::cout << "Created unique temp dir2: " << tmp_dir2 << std::endl; if (vpIoTools::remove(tmp_dir1)) { std::cout << "Temp dir1 was deleted" << std::endl; } if (vpIoTools::remove(tmp_dir2)) { std::cout << "Temp dir2 was deleted" << std::endl; } }
On Windows it produces:
Temp path: C:\Users\<username>\AppData\Local\Temp Created unique temp dir1: C:\Users\<username>\AppData\Local\Temp\ddaac8c3-7a95-447f-8a1c-fe31bb2426f9 Created unique temp dir2: C:\Users\<username>\AppData\Local\Temp\dir\_8b9e6e9a-fe9b-4b44-8382-fc2368dfed68 Temp dir1 was deleted Temp dir2 was deleted
while on Unix it produces:
Temp path: /tmp/<username> Created unique temp dir1: /tmp/<username>/AMIsXF Created unique temp dir2: /tmp/<username>/dir\_KP7119 Temp dir1 was deleted Temp dir2 was deleted
Note
See makeTempDirectory() , remove()
- static getUserName(*args, **kwargs)¶
Overloaded function.
getUserName() -> str
Get the user name.
Under unix, get the content of the LOGNAME environment variable. For most purposes (especially in conjunction with crontab), it is more useful to use the environment variable LOGNAME to find out who the user is, rather than the getlogin() function. This is more flexible precisely because the user can set LOGNAME arbitrarily.
Under windows, uses the GetUserName() function.
- Returns:
A tuple containing:
username: The user name. When the username cannot be retrieved, set username to “unknown” string.
getUserName() -> str
Get the user name.
Under unix, get the content of the LOGNAME environment variable. For most purposes (especially in conjunction with crontab), it is more useful to use the environment variable LOGNAME to find out who the user is, rather than the getlogin() function. This is more flexible precisely because the user can set LOGNAME arbitrarily.
Under windows, uses the GetUserName() function.
Note
See getUserName(std::string &)
- Returns:
The user name.
- static getVersion(version: str) tuple[int, int, int] ¶
Extract major, minor and patch from a version given as “x.x.x”. Ex: If version is “1.2.1”, major will be 1, minor 2 and patch 1.
- static getViSPImagesDataPath() str ¶
Get ViSP images data path. ViSP images data can be installed from Debian or Ubuntu visp-images-data package. It can be also installed from visp-images-3.x.y.zip that can be found on https://visp.inria.fr/download page.
This function returns the path to the folder that contains the data.
It checks first if VISP_INPUT_IMAGE_PATH environment variable that gives the location of the data is set. In that case returns the content of this environment var.
Otherwise it checks if visp-images-data binary package (Ubuntu, Debian) is installed. In that case returns then /usr/share/visp-images-data” .
If the path is not found, returns an empty string.
- static getenv(env: str) str ¶
Get the content of an environment variable.
#include <iostream> #include <string> #include <visp3/core/vpIoTools.h> int main() { std::string envvalue; try { std::string env = "HOME"; envvalue = vpIoTools::getenv(env); std::cout << "$HOME = \"" << envvalue << "\"" << std::endl; } catch (const vpException &e) { std::cout << e.getMessage() << std::endl; return -1; } return 0; }
- static isAbsolutePathname(pathname: str) bool ¶
Return whether a path is absolute.
- Returns:
true if the pathname is absolute, false otherwise.
- static isSamePathname(pathname1: str, pathname2: str) bool ¶
Return true if the two pathnames are identical.
Note
It uses path() to normalize the path and getAbsolutePathname() to get the absolute pathname.
- Returns:
true if the two pathnames are identical, false otherwise.
- static makeDirectory(dirname: str) None ¶
Create a new directory. It will create recursively the parent directories if needed.
Note
See makeTempDirectory() , remove()
- static makeFifo(dirname: str) None ¶
Create a new FIFO file. A FIFO file is a special file, similar to a pipe, but actually existing on the hard drive. It can be used to communicate data between multiple processes.
Warning
This function is only implemented on unix-like OS.
- static makeTempDirectory(dirname: str) str ¶
Create a new temporary directory with a unique name based on dirname parameter.
The following sample shows how to use this function to create unique temporary directories:
include <visp3/core/vpIoTools.h> int main() { std::string tmp_path = vpIoTools::getTempPath(); std::cout << "Temp path: " << tmp_path << std::endl; std::string tmp_dir1 = vpIoTools::makeTempDirectory(tmp_path); std::cout << "Created unique temp dir1: " << tmp_dir1 << std::endl; std::string tmp_dir2_template = tmp_path + vpIoTools::path("/") + "dir_XXXXXX"; std::string tmp_dir2 = vpIoTools::makeTempDirectory(tmp_dir2_template); std::cout << "Created unique temp dir2: " << tmp_dir2 << std::endl; if (vpIoTools::remove(tmp_dir1)) { std::cout << "Temp dir1 was deleted" << std::endl; } if (vpIoTools::remove(tmp_dir2)) { std::cout << "Temp dir2 was deleted" << std::endl; } }
On Windows it produces:
Temp path: C:\Users\<username>\AppData\Local\Temp Created unique temp dir1: C:\Users\<username>\AppData\Local\Temp\ddaac8c3-7a95-447f-8a1c-fe31bb2426f9 Created unique temp dir2: C:\Users\<username>\AppData\Local\Temp\dir\_8b9e6e9a-fe9b-4b44-8382-fc2368dfed68 Temp dir1 was deleted Temp dir2 was deleted
while on Unix it produces:
Temp path: /tmp/<username> Created unique temp dir1: /tmp/<username>/AMIsXF Created unique temp dir2: /tmp/<username>/dir\_KP7119 Temp dir1 was deleted Temp dir2 was deleted
Note
See makeDirectory() , getTempPath() , remove()
- Parameters:
- Returns:
String corresponding to the absolute path to the generated directory name.
- static readConfigVar(*args, **kwargs)¶
Overloaded function.
readConfigVar(var: str, value: visp._visp.core.Color) -> bool
Tries to read the parameter named var as a * vpColor * .
- Parameters:
- var
Name of the parameter in the configuration file.
- value
Value to be read. See vpColor.cpp for the color number.
- Returns:
true if the parameter could be read.
readConfigVar(var: str, value: visp._visp.core.ArrayDouble2D, nCols: int = 0, nRows: int = 0) -> bool
Tries to read the parameter named var as a * vpMatrix * . If nCols and nRows are indicated, will resize the matrix. Otherwise, will try to read as many values as indicated by the dimension of value .
- Parameters:
- var
Name of the parameter in the configuration file.
- value
Value to be read.
- nCols
Column dimension if resized.
- nRows
Row dimension if resized
- Returns:
true if the parameter could be read.
- static readConfigVarBoolean(var: str) tuple[bool, bool] ¶
Tries to read the parameter named var as a bool .
- static readConfigVarDouble(var: str) tuple[bool, float] ¶
Tries to read the parameter named var as a double .
- static readConfigVarFloat(var: str) tuple[bool, float] ¶
Tries to read the parameter named var as a float .
- static readConfigVarInt(var: str) tuple[bool, int] ¶
Tries to read the parameter named var as a int .
- static readConfigVarString(var: str) tuple[bool, str] ¶
Tries to read the parameter named var as a std::string .
- static readConfigVarUnsigned(var: str) tuple[bool, int] ¶
Tries to read the parameter named var as a unsigned int.
- static remove(filename: str) bool ¶
Remove a file or a directory.
Note
See makeDirectory() , makeTempDirectory()
- Returns:
true if the file or the directory was removed, false otherwise.
- static rename(oldfilename: str, newfilename: str) bool ¶
Rename an existing file oldfilename in newfilename .
- static saveConfigFile(actuallySave: bool = true) None ¶
Copy the initial configuration file to the experiment directory.
- static splitChain(chain: str, sep: str) list[str] ¶
The following code shows how to use this function:
#include <visp3/core/vpIoTools.h> int main() { { std::string chain("/home/user;/usr/local/include;/usr/include"); std::string sep = ";"; std::vector<std::string> subChain = vpIoTools::splitChain(chain, sep); std::cout << "Found the following subchains: " << std::endl; for (size_t i=0; i < subChain.size(); ++i) std::cout << subChain[i] << std::endl; } { std::string chain("This is an other example"); std::string sep = " "; std::vector<std::string> subChain = vpIoTools::splitChain(chain, sep); std::cout << "Found the following subchains: " << std::endl; for (size_t i=0; i < subChain.size(); ++i) std::cout << subChain[i] << std::endl; } }
It produces the following output:
Found the following subchains: /home/user /usr/local/include /usr/include Found the following subchains: This is an other example
- static splitDrive(pathname: str) tuple[str, str] ¶
- Returns:
a pair whose the first element is the drive specification and the second element the path specification
- static toLowerCase(input: str) str ¶
Return a lower-case version of the string input . Numbers and special characters stay the same.
- static toUpperCase(input: str) str ¶
Return a upper-case version of the string input . Numbers and special characters stay the same.
- static writeBinaryValueLE(*args, **kwargs)¶
Overloaded function.
writeBinaryValueLE(file: std::basic_ofstream<char, std::char_traits<char> >, short_value: int) -> None
Write a 16-bits integer value in little endian.
writeBinaryValueLE(file: std::basic_ofstream<char, std::char_traits<char> >, ushort_value: int) -> None
Write a 16-bits unsigned integer value in little endian.
writeBinaryValueLE(file: std::basic_ofstream<char, std::char_traits<char> >, int_value: int) -> None
Write a 32-bits integer value in little endian.
writeBinaryValueLE(file: std::basic_ofstream<char, std::char_traits<char> >, int_value: int) -> None
Write a 32-bits unsigned integer value in little endian.
writeBinaryValueLE(file: std::basic_ofstream<char, std::char_traits<char> >, float_value: float) -> None
Write a float value in little endian.
writeBinaryValueLE(file: std::basic_ofstream<char, std::char_traits<char> >, double_value: float) -> None
Write a double value in little endian.