refactoredCPPNeuronMesher
utils.cpp File Reference

Implementation of utility functions for file system operations. More...

#include "utils.h"
#include <filesystem>
#include <iostream>
Include dependency graph for utils.cpp:

Functions

std::string getExecutableDir ()
 Gets the directory containing the currently running executable. More...
 
void checkFolder (std::string &folderPath)
 Checks if a folder exists and creates it if it doesn't. 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 the specified directory. More...
 

Detailed Description

Implementation of utility functions for file system operations.

This file contains general-purpose utility functions for file system operations used throughout the CPPNeuronMesher project. It provides cross-platform functionality for working with files and directories.

Key features include:

  • Directory management (creation, deletion, checking existence)
  • File system path manipulation
  • Directory listing and file enumeration
  • Executable path resolution

The implementation uses the C++17 filesystem library for cross-platform compatibility and modern C++ practices.

Author
CPPNeuronMesher Team
Date
2025-07-27
Version
1.0
See also
https://github.com/yourusername/refactoredCPPNeuronMesher

Function Documentation

◆ checkFolder()

void checkFolder ( std::string &  folderPath)

Checks if a folder exists and creates it if it doesn't.

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 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 currently running executable.

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 the specified directory.

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