UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
OS Utility Functions

Utilities for interacting with the operating system. More...

Classes

class  Utils::Runtime_TraceError
 A custom exception class that captures and stores a backtrace on error. More...
 

Functions

bool Utils::get_environment (string_view ename, string &res)
 Fetches the value of an environment variable.
 
void Utils::set_environment (string_view ename, string_view newval, bool overwrite)
 Sets the value of an environment variable.
 
void Utils::get_MAC_address (std::map< string, string > &addr)
 Retrieves the MAC addresses of network interfaces.
 
string Utils::get_host_name ()
 Retrieves the hostname of the system.
 
void Utils::get_IP_address (std::vector< string > &addr)
 Fetches the IP addresses of the system.
 
string Utils::get_user_name ()
 Retrieves the username of the current user.
 
string Utils::get_home_directory ()
 Retrieves the home directory of the current user.
 
string Utils::get_executable_path_name ()
 Retrieves the full path to the current executable.
 
bool Utils::check_if_file_exists (string_view fname)
 Checks if a file exists.
 
bool Utils::check_if_dir_exists (string_view dirname)
 Checks if a directory exists.
 
bool Utils::make_directory (string_view dirname, unsigned mode=0777)
 Creates a directory if it does not exist.
 
string Utils::get_date ()
 
string Utils::get_day_time ()
 
string Utils::get_day_time_and_date ()
 
string Utils::get_log_date_time ()
 
void Utils::print_trace (int line, string_view file, string_view msg, ostream_type &stream)
 Prints a formatted trace message to the specified stream.
 
void Utils::printTrace (int line, string_view file, string_view reason, ostream_type &stream)
 

Detailed Description

Utilities for interacting with the operating system.

This module provides a variety of utility functions designed to facilitate interaction with the operating system. It includes functions for managing environment variables, retrieving network details such as IP and MAC addresses, querying system information, and performing common file system operations such as file path manipulation and directory management.

Function Documentation

◆ check_if_dir_exists()

bool Utils::check_if_dir_exists ( string_view dirname)

Checks if a directory exists.

Parameters
dirnameThe path to the directory to check.
Returns
True if the directory exists and is valid, false otherwise.

◆ check_if_file_exists()

bool Utils::check_if_file_exists ( string_view fname)

Checks if a file exists.

Parameters
fnameThe path to the file to check.
Returns
True if the file exists and is a regular file, false otherwise.

◆ get_date()

string Utils::get_date ( )

◆ get_day_time()

string Utils::get_day_time ( )

◆ get_day_time_and_date()

string Utils::get_day_time_and_date ( )

◆ get_environment()

bool Utils::get_environment ( string_view ename,
string & res )

Fetches the value of an environment variable.

This function retrieves the value of the environment variable with the name specified by ename and stores it in the reference res.

Parameters
enameName of the environment variable to retrieve.
resReference to a string where the result will be stored.
Returns
True if the environment variable was found and its value retrieved, false otherwise.

◆ get_executable_path_name()

string Utils::get_executable_path_name ( )

Retrieves the full path to the current executable.

Returns
A string containing the full path to the executable.

◆ get_home_directory()

string Utils::get_home_directory ( )

Retrieves the home directory of the current user.

Returns
A string containing the home directory path of the current user.

◆ get_host_name()

string Utils::get_host_name ( )

Retrieves the hostname of the system.

Returns
A string containing the system's hostname.

◆ get_IP_address()

void Utils::get_IP_address ( std::vector< string > & addr)

Fetches the IP addresses of the system.

This function retrieves all IP addresses associated with the current system's network interfaces and stores them in the provided vector addr.

Parameters
addrA reference to a vector where the IP addresses will be stored.

◆ get_log_date_time()

string Utils::get_log_date_time ( )

◆ get_MAC_address()

void Utils::get_MAC_address ( std::map< string, string > & addr)

Retrieves the MAC addresses of network interfaces.

This function retrieves the MAC addresses for all available network interfaces on the system and stores them in the provided map, with interface names as the keys and MAC addresses as the values.

Parameters
addrA reference to a map where the MAC addresses will be stored.

◆ get_user_name()

string Utils::get_user_name ( )

Retrieves the username of the current user.

Returns
A string containing the username of the current user.

◆ make_directory()

bool Utils::make_directory ( string_view dirname,
unsigned mode = 0777 )

Creates a directory if it does not exist.

This function creates a directory with the specified mode if it does not already exist.

Parameters
dirnameThe path to the directory to create.
modeThe permissions mode to set for the new directory.
Returns
True if the directory was created or already exists, false otherwise.

◆ print_trace()

void Utils::print_trace ( int line,
string_view file,
string_view msg,
ostream_type & stream )

Prints a formatted trace message to the specified stream.

This function outputs a trace message that includes the line number, file name, and a custom message to help in debugging or logging operations.

Parameters
lineThe line number in the source file where the trace is generated.
fileThe name of the source file where the trace is generated.
msgA custom message that provides additional information for the trace.
streamThe output stream where the trace will be printed (e.g., std::cout, std::cerr).
Note
This function is useful for logging and tracking the execution flow, especially in debugging scenarios. /

◆ printTrace()

void Utils::printTrace ( int line,
string_view file,
string_view reason,
ostream_type & stream )
inline

◆ set_environment()

void Utils::set_environment ( string_view ename,
string_view newval,
bool overwrite )

Sets the value of an environment variable.

This function sets the value of the environment variable specified by ename to newval. If the variable already exists, it will be overwritten if the overwrite flag is true.

Parameters
enameName of the environment variable to set.
newvalThe new value to set for the environment variable.
overwriteFlag to indicate whether the environment variable should be overwritten if it already exists.