refactoredCPPNeuronMesher
utils.h File Reference

Utility functions for file system operations. More...

#include <string>
#include <vector>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

std::string getExecutableDir ()
 Gets the directory containing the current executable. More...
 
void checkFolder (std::string &folderPath)
 Ensures a directory exists, creating it if necessary. More...
 
void deleteFolder (const std::string &path)
 Recursively deletes a directory and all its contents. More...
 
std::vector< std::string > listFilesInDirectory (const std::string &path)
 Lists all regular files in a directory. More...
 

Detailed Description

Utility functions for file system operations.

This header provides cross-platform utility functions for common file system operations used throughout the CPPNeuronMesher project. These utilities handle path manipulation, directory creation, and file listing.

Author
CPPNeuronMesher Team
Date
2025-07-27
Version
1.0

Function Documentation

◆ checkFolder()

void checkFolder ( std::string &  folderPath)

Ensures a directory exists, creating it if necessary.

Parameters
[in,out]folderPathPath to the directory to check/create

This function checks if the specified directory exists and creates it (including any necessary parent directories) if it doesn't exist. The folderPath parameter is modified to contain an absolute path.

Exceptions
std::runtime_errorif the directory cannot be created
Note
Uses std::filesystem::create_directories()

Ensures a directory exists, creating it if necessary.

Parameters
[in,out]folderPathPath to the folder to check/create

This function checks if the specified folder exists. If it doesn't exist, it attempts to create the folder and all necessary parent directories. Status messages are printed to standard output/error streams.

Note
The function modifies the input string to ensure it's in a canonical form
If folder creation fails, an error message is printed to stderr
On success, the folderPath parameter will be updated to the canonical path
See also
std::filesystem::create_directories() for details on directory creation
std::filesystem::exists() for details on existence checking
Here is the caller graph for this function:

◆ deleteFolder()

void deleteFolder ( const std::string &  path)

Recursively deletes a directory and all its contents.

Parameters
[in]pathPath to the directory to delete

This function removes the specified directory and all files and subdirectories it contains. If the path does not exist, the function does nothing.

Exceptions
std::runtime_errorif the directory cannot be deleted
Note
Uses std::filesystem::remove_all()
Warning
This operation is not reversible
Parameters
[in]pathPath to the directory to be deleted

This function attempts to delete the specified directory and all its contents recursively. It uses std::filesystem::remove_all() for the operation. Status messages are printed to standard output/error streams.

Note
The operation is not atomic - some files might be deleted even if others fail
If the operation fails, an error message is printed to stderr
The function will not throw exceptions on filesystem errors
See also
std::filesystem::remove_all() for details on the underlying operation
std::error_code for error handling details

◆ getExecutableDir()

std::string getExecutableDir ( )

Gets the directory containing the current executable.

Returns
Absolute path to the directory containing the executable

This function retrieves the full path to the directory containing the currently running executable. The path is returned with a trailing directory separator.

Note
On Linux, this uses /proc/self/exe to determine the path
The returned path is guaranteed to be absolute
The function may return an empty string if the path cannot be determined

Gets the directory containing the current executable.

Returns
std::string The absolute path to the directory containing the executable

This function determines the directory containing the currently running executable by resolving the symbolic link at "/proc/self/exe" and returning its parent directory.

Note
This implementation is Linux-specific and uses /proc filesystem
The returned path is always absolute and canonical (no . or .. components)
On error, the function may return an empty string
See also
https://man7.org/linux/man-pages/man5/proc.5.html for /proc/self/exe details
Here is the caller graph for this function:

◆ listFilesInDirectory()

std::vector<std::string> listFilesInDirectory ( const std::string &  path)

Lists all regular files in a directory.

Parameters
[in]pathPath to the directory to scan
Returns
Vector of filenames (not including directory path)

This function returns a list of all regular files (not directories) in the specified directory. The returned filenames do not include the directory path.

Exceptions
std::runtime_errorif the directory cannot be accessed
Note
The order of files in the result is filesystem-dependent
Symbolic links are not followed

Lists all regular files in a directory.

Parameters
[in]pathPath to the directory to scan
Returns
std::vector<std::string> Vector containing absolute paths to all regular files

This function scans the specified directory and returns a list of all regular files it contains. The function only includes files (not directories) in the results.

Note
The function returns absolute file paths by default
The function does not recurse into subdirectories
If the directory cannot be accessed, an empty vector is returned
The order of files in the result is filesystem-dependent
See also
std::filesystem::directory_iterator for iteration details
std::filesystem::is_regular_file() for file type checking