HAL
hal::utils Namespace Reference

Classes

class  RecursiveDirectoryRange
 
class  DirectoryRange
 
struct  Color
 

Functions

bool file_exists (const std::string &filename)
 
bool folder_exists_and_is_accessible (const std::filesystem::path &folder)
 
std::filesystem::path get_binary_directory ()
 
std::filesystem::path get_base_directory ()
 
std::filesystem::path get_library_directory ()
 
std::filesystem::path get_share_directory ()
 
std::filesystem::path get_user_share_directory ()
 
std::filesystem::path get_config_directory ()
 
std::filesystem::path get_user_config_directory ()
 
std::filesystem::path get_default_log_directory (std::filesystem::path source_file)
 
std::vector< std::filesystem::path > get_gate_library_directories ()
 
std::vector< std::filesystem::path > get_plugin_directories ()
 
std::filesystem::path get_first_directory_exists (std::vector< std::filesystem::path > path_hints)
 
std::filesystem::path get_file (std::string file_name, std::vector< std::filesystem::path > path_hints)
 
std::filesystem::path which (const std::string &name, const std::string &path)
 
Result< std::filesystem::path > get_unique_temp_directory (const std::string &prefix, const u32 max_attmeps)
 
std::string get_open_source_licenses ()
 
Result< u64wrapped_stoull (const std::string &s, const u32 base)
 
Result< u32wrapped_stoul (const std::string &s, const u32 base)
 
template<typename T >
CORE_API bool unordered_vector_erase (std::vector< T > &vec, T element)
 
template<typename T >
bool vectors_have_same_content (std::vector< T > vec_1, std::vector< T > vec_2)
 
CORE_API u64 get_bit (const u64 value, const u64 index)
 
CORE_API u64 set_bit (const u64 value, const u64 index)
 
CORE_API u64 clear_bit (const u64 value, const u64 index)
 
CORE_API u64 toggle_bit (const u64 value, const u64 index)
 
template<typename T >
CORE_API bool ends_with (const T &s, const T &end)
 
template<typename T >
CORE_API bool starts_with (const T &s, const T &start)
 
template<typename T >
CORE_API bool is_digits (const T &s)
 
template<typename T >
CORE_API bool is_integer (const T &s)
 
template<typename T >
CORE_API bool is_floating_point (const T &s)
 
template<typename T >
CORE_API std::vector< T > split (const T &s, const char delim, bool obey_brackets=false)
 
template<typename T >
CORE_APIltrim (const T &s, const char *to_remove=" \t\r\n")
 
template<typename T >
CORE_APIrtrim (const T &s, const char *to_remove=" \t\r\n")
 
template<typename T >
CORE_APItrim (const T &s, const char *to_remove=" \t\r\n")
 
template<typename T >
CORE_APIreplace (const T &str, const T &search, const T &replace)
 
template<typename Iterator , class Transform >
CORE_API std::string join (const std::string &joiner, const Iterator &begin, const Iterator &end, const Transform &transform)
 
template<typename T , class Transform >
CORE_API std::string join (const std::string &joiner, const T &items, const Transform &transform)
 
template<typename T >
CORE_API std::string join (const std::string &joiner, const T &items)
 
template<typename T >
CORE_APIto_upper (const T &s)
 
template<typename T >
CORE_APIto_lower (const T &s)
 
template<typename T >
CORE_API u32 num_of_occurrences (const T &s, const T &substr)
 
template<typename T , template< typename, typename... > class Container, typename... Args>
CORE_API std::vector< T > to_vector (const Container< T, Args... > &container)
 
template<typename T , template< typename, typename... > class Container, typename... Args>
CORE_API std::set< T > to_set (const Container< T, Args... > &container)
 
template<typename T1 , typename T2 >
CORE_API bool is_subset (const T1 &subset, const T2 &superset)
 

Function Documentation

◆ clear_bit()

CORE_API u64 hal::utils::clear_bit ( const u64  value,
const u64  index 
)
inline

Clear a single bit of an integer to 0.

Parameters
[in]value- The integer value.
[in]index- The bit position.
Returns
The manipulated value.

Definition at line 122 of file utils.h.

◆ ends_with()

template<typename T >
CORE_API bool hal::utils::ends_with ( const T &  s,
const T &  end 
)

Checks whether a string ends with another string.

Parameters
[in]s- The string to analyze.
[in]end- The ending to check for.
Returns
True if s ends with end, false otherwise.

Definition at line 147 of file utils.h.

Referenced by hal::plugin_manager::has_valid_file_extension().

◆ file_exists()

CORE_API bool hal::utils::file_exists ( const std::string &  filename)

Check whether a file exists.

Parameters
[in]filename- The file to check.
Returns
True if file exists, false otherwise.

Definition at line 30 of file utils.cpp.

◆ folder_exists_and_is_accessible()

CORE_API bool hal::utils::folder_exists_and_is_accessible ( const std::filesystem::path &  path)

Check whether a directory exists and access rights are granted.

Parameters
[in]path- The directory to check.
Returns
True if path exists and is accessible, false otherwise.

Definition at line 36 of file utils.cpp.

◆ get_base_directory()

CORE_API std::filesystem::path hal::utils::get_base_directory ( )

Get the base path to the HAL installation.

  1. Use Environment Variable HAL_BASE_PATH
  2. If current executable is hal (not e.g. python3 interpreter) use it's path to determine base path.
  3. Try to find hal executable in path and use its base path.
Returns
The path.

Definition at line 99 of file utils.cpp.

References die, get_binary_directory(), and which().

Referenced by hal::core_utils_init(), get_config_directory(), get_library_directory(), get_share_directory(), and hal::TEST_F().

◆ get_binary_directory()

CORE_API std::filesystem::path hal::utils::get_binary_directory ( )

Get the path to the executable of HAL.

Returns
The path.

Definition at line 69 of file utils.cpp.

Referenced by hal::core_utils_init(), and get_base_directory().

◆ get_bit()

CORE_API u64 hal::utils::get_bit ( const u64  value,
const u64  index 
)
inline

Get a single bit of an integer value.

Parameters
[in]value- The integer value.
[in]index- The bit position.
Returns
The specified bit (0 or 1).

Definition at line 98 of file utils.h.

◆ get_config_directory()

CORE_API std::filesystem::path hal::utils::get_config_directory ( )

Get the path to the read-only global configuration directory of HAL.
The resulting path is relative to the binary directory.

Returns
The path.

Definition at line 163 of file utils.cpp.

References get_base_directory(), and get_first_directory_exists().

◆ get_default_log_directory()

CORE_API std::filesystem::path hal::utils::get_default_log_directory ( std::filesystem::path  source_file = "")

Get the path to the default directory for log files.
If an netlist source file is provided, the function returns the parent directory.

Parameters
[in]source_file- The netlist source file.
Returns
The path.

Definition at line 178 of file utils.cpp.

References get_user_share_directory().

Referenced by hal::core_utils_init().

◆ get_file()

CORE_API std::filesystem::path hal::utils::get_file ( std::string  file_name,
std::vector< std::filesystem::path >  path_hints 
)

Search for the given filename in all paths provided by the path hints (searched in order).

Parameters
[in]file_name- The filename to search for.
[in]path_hints- A vector of hints.
Returns
The first to match or an empty string if no match is found.

Definition at line 219 of file utils.cpp.

Referenced by hal::gate_library_manager::get_gate_library(), and hal::plugin_manager::get_plugin_path().

◆ get_first_directory_exists()

CORE_API std::filesystem::path hal::utils::get_first_directory_exists ( std::vector< std::filesystem::path >  path_hints)

Get the first directory out of all the path hints that exists (searched in order).

Parameters
[in]path_hints- A vector of hints.
Returns
The first existing path or an empty string if none of them exists.

Definition at line 206 of file utils.cpp.

Referenced by get_config_directory(), and get_share_directory().

◆ get_gate_library_directories()

CORE_API std::vector< std::filesystem::path > hal::utils::get_gate_library_directories ( )

Get the paths to the locations where gate libraries are searched.

Returns
A vector of paths.

Definition at line 185 of file utils.cpp.

References get_share_directory(), and get_user_share_directory().

Referenced by hal::core_utils_init(), hal::gate_library_manager::get_all_path(), hal::gate_library_manager::get_gate_library(), and hal::gate_library_manager::load_all().

◆ get_library_directory()

CORE_API std::filesystem::path hal::utils::get_library_directory ( )

Get the path to the shared and static libraries of HAL.
The resulting path is relative to the binary directory.

Returns
The path.

Definition at line 128 of file utils.cpp.

References get_base_directory().

Referenced by hal::core_utils_init(), and get_plugin_directories().

◆ get_open_source_licenses()

CORE_API std::string hal::utils::get_open_source_licenses ( )

Get the licenses of all embedded OpenSource Projects.

Returns
The open source licenses.

Definition at line 298 of file utils.cpp.

Referenced by hal::AboutDialog::AboutDialog().

◆ get_plugin_directories()

CORE_API std::vector< std::filesystem::path > hal::utils::get_plugin_directories ( )

Get the paths to the locations where plugins are searched.

Returns
A vector of paths.

Definition at line 194 of file utils.cpp.

References get_library_directory(), and get_user_share_directory().

Referenced by hal::core_utils_init().

◆ get_share_directory()

CORE_API std::filesystem::path hal::utils::get_share_directory ( )

Get the path to the shared objects of HAL.
The resulting path is relative to the binary directory.

Returns
The path.

Definition at line 148 of file utils.cpp.

References get_base_directory(), and get_first_directory_exists().

Referenced by hal::core_utils_init(), and get_gate_library_directories().

◆ get_unique_temp_directory()

CORE_API Result< std::filesystem::path > hal::utils::get_unique_temp_directory ( const std::string &  prefix = "",
const u32  max_attempts = 5 
)

Try to generate a unique temporary directory.

Parameters
[in]prefix- A prefix that is added in front of the unique identifier. Defaults to an empty string.
[in]max_attmeps- The maximum amount of attempts before the function fails. Defaults to 5.
Returns
OK and the created directory path on success, an error otherwise.

Definition at line 275 of file utils.cpp.

References ERR, and OK.

◆ get_user_config_directory()

CORE_API std::filesystem::path hal::utils::get_user_config_directory ( )

Get the path to the configuration directory of the user.

Returns
The path.

Definition at line 171 of file utils.cpp.

Referenced by hal::core_utils_init().

◆ get_user_share_directory()

CORE_API std::filesystem::path hal::utils::get_user_share_directory ( )

Get the path to shared objects and files provided by the user.

Returns
The path.

Definition at line 156 of file utils.cpp.

Referenced by hal::core_utils_init(), get_default_log_directory(), get_gate_library_directories(), and get_plugin_directories().

◆ is_digits()

template<typename T >
CORE_API bool hal::utils::is_digits ( const T &  s)

Checks whether a string contains only digits.

Parameters
[in]s- The string to analyze.
Returns
True if s contains only digits, false otherwise.

Definition at line 186 of file utils.h.

◆ is_floating_point()

template<typename T >
CORE_API bool hal::utils::is_floating_point ( const T &  s)

Checks whether a string represents a real number.

Parameters
[in]s- The string to analyze.
Returns
True if s contains a real number, false otherwise.

Definition at line 218 of file utils.h.

◆ is_integer()

template<typename T >
CORE_API bool hal::utils::is_integer ( const T &  s)

Checks whether a string represents an integer.

Parameters
[in]s- The string to analyze.
Returns
True if s contains an integer, false otherwise.

Definition at line 198 of file utils.h.

◆ is_subset()

template<typename T1 , typename T2 >
CORE_API bool hal::utils::is_subset ( const T1 &  subset,
const T2 &  superset 
)

Check whether one container is a subset of another container. The function checks whether all elements of the subset container are contained in the superset container.

Parameters
[in]subset- The container containing the possible subset.
[in]superset- The container containing the possible superset.
Returns
True if the subset container actually is a subset of the superset.

Definition at line 539 of file utils.h.

◆ join() [1/3]

template<typename Iterator , class Transform >
CORE_API std::string hal::utils::join ( const std::string &  joiner,
const Iterator &  begin,
const Iterator &  end,
const Transform &  transform 
)

Join all elements of a iterator range with a joiner-string. A transformation is applied to all elements of the collections before joining.

Parameters
[in]joiner- The string joining the elements.
[in]begin- The start iterator.
[in]end- The end iterator.
[in]transform- The transformation function.
Returns
The joined string.

Definition at line 412 of file utils.h.

Referenced by hal::dataflow::analyze(), join(), hal::ProgramOptions::parse(), hal::ProgramArguments::set_option(), and hal::dataflow::Result::write_txt().

◆ join() [2/3]

template<typename T >
CORE_API std::string hal::utils::join ( const std::string &  joiner,
const T &  items 
)

Join all elements of a collection with a joiner-string.

Parameters
[in]joiner- The string joining the elements.
[in]items- The collection of elements to join.
Returns
The joined string.

Definition at line 451 of file utils.h.

References join().

◆ join() [3/3]

template<typename T , class Transform >
CORE_API std::string hal::utils::join ( const std::string &  joiner,
const T &  items,
const Transform &  transform 
)

Join all elements of a collection with a joiner-string. A transformation is applied to all elements of the collections before joining.

Parameters
[in]joiner- The string joining the elements.
[in]items- The collection of elements to join.
[in]transform- The transformation function.
Returns
The joined string.

Definition at line 438 of file utils.h.

References join().

◆ ltrim()

template<typename T >
CORE_API T hal::utils::ltrim ( const T &  s,
const char *  to_remove = " \t\r\n" 
)

Remove any leading whitespace characters from the beginning of a string.

Parameters
[in]s- The string to trim.
[in]to_remove- The characters to remove.
Returns
The trimmed string.

Definition at line 314 of file utils.h.

References hal::T.

◆ num_of_occurrences()

template<typename T >
CORE_API u32 hal::utils::num_of_occurrences ( const T &  s,
const T &  substr 
)

Count occurences of a substring in a string.

Parameters
[in]s- The string containing the substring.
[in]substr- The substring.
Returns
The number of occurrences.

Definition at line 492 of file utils.h.

◆ replace()

template<typename T >
CORE_API T hal::utils::replace ( const T &  str,
const T &  search,
const T &  replace 
)

Replace all occurences of a substring within a string with another string.

Parameters
[in]str- The initial string.
[in]search- The targetted substring.
[in]replace- The replacement string.
Returns
String with replaced substrings.

Definition at line 382 of file utils.h.

References control::str, and hal::T.

◆ rtrim()

template<typename T >
CORE_API T hal::utils::rtrim ( const T &  s,
const char *  to_remove = " \t\r\n" 
)

Remove any trailing whitespace characters from the end of a string.

Parameters
[in]s- The string to trim.
[in]to_remove- The characters to remove.
Returns
The trimmed string.

Definition at line 336 of file utils.h.

References hal::T.

◆ set_bit()

CORE_API u64 hal::utils::set_bit ( const u64  value,
const u64  index 
)
inline

Set a single bit of an integer to 1.

Parameters
[in]value- The integer value.
[in]index- The bit position.
Returns
The manipulated value.

Definition at line 110 of file utils.h.

◆ split()

template<typename T >
CORE_API std::vector<T> hal::utils::split ( const T &  s,
const char  delim,
bool  obey_brackets = false 
)

Split a string into a vector of strings. The split delimiter can be specified by the user.
The delimiters are removed in the splitting process.
To avoid splitting within brackets, set obey_brackets to true.

Parameters
[in]s- The string to split.
[in]delim- The delimiter, indicating where to split.
[in]obey_brackets- Flag to indicate whether brackets are obeyed.
Returns
The vector of strings.

Definition at line 237 of file utils.h.

References hal::T.

Referenced by which().

◆ starts_with()

template<typename T >
CORE_API bool hal::utils::starts_with ( const T &  s,
const T &  start 
)

Checks whether a string begins with another string.

Parameters
[in]s- The string to analyze.
[in]start- The beginning to check for.
Returns
True if s begins with start, false otherwise.

Definition at line 167 of file utils.h.

Referenced by hal::dataflow::Result::create_modules(), and hal::TEST_F().

◆ to_lower()

template<typename T >
CORE_API T hal::utils::to_lower ( const T &  s)

Convert a string to lower case.

Parameters
[in]s- The string to convert.
Returns
The lower case string.

Definition at line 477 of file utils.h.

References hal::T.

Referenced by hal::netlist_parser_manager::can_parse(), hal::gate_library_parser_manager::register_parser(), hal::netlist_parser_manager::register_parser(), hal::gate_library_writer_manager::register_writer(), and hal::netlist_writer_manager::register_writer().

◆ to_set()

template<typename T , template< typename, typename... > class Container, typename... Args>
CORE_API std::set<T> hal::utils::to_set ( const Container< T, Args... > &  container)
inline

Turn a given iterable collection into a set.

Parameters
[in]container- The input collection.
Returns
The set containing all items copied from the collection.

Definition at line 525 of file utils.h.

◆ to_upper()

template<typename T >
CORE_API T hal::utils::to_upper ( const T &  s)

Convert a string to upper case.

Parameters
[in]s- The string to convert.
Returns
The upper case string.

Definition at line 463 of file utils.h.

References hal::T.

◆ to_vector()

template<typename T , template< typename, typename... > class Container, typename... Args>
CORE_API std::vector<T> hal::utils::to_vector ( const Container< T, Args... > &  container)
inline

Turn a given iterable collection into a vector.

Parameters
[in]container- The input collection.
Returns
The vector containing all items copied from the collection.

Definition at line 513 of file utils.h.

Referenced by hal::Gate::get_resolved_boolean_function(), hal::LUTTableModel::setBooleanFunction(), and hal::solve_fsm::solve_fsm_brute_force().

◆ toggle_bit()

CORE_API u64 hal::utils::toggle_bit ( const u64  value,
const u64  index 
)
inline

Toggle a single bit of an integer.

Parameters
[in]value- The integer value.
[in]index- The bit position.
Returns
The manipulated value.

Definition at line 134 of file utils.h.

◆ trim()

template<typename T >
CORE_API T hal::utils::trim ( const T &  s,
const char *  to_remove = " \t\r\n" 
)

Remove any leading and trailing whitespace characters from the beginning and the end of a string.

Parameters
[in]s- The string to trim.
[in]to_remove- The characters to remove.
Returns
The trimmed string.

Definition at line 358 of file utils.h.

References hal::T.

Referenced by hal::ProgramOptions::add(), hal::gate_library_parser_manager::register_parser(), hal::netlist_parser_manager::register_parser(), hal::gate_library_writer_manager::register_writer(), hal::netlist_writer_manager::register_writer(), hal::Gate::set_name(), hal::Module::set_name(), hal::Net::set_name(), and hal::Grouping::set_name().

◆ unordered_vector_erase()

template<typename T >
CORE_API bool hal::utils::unordered_vector_erase ( std::vector< T > &  vec,
element 
)
inline

Erase the first occurence of a given element from an unordered vector.

Parameters
[in]vec- The unordered vector.
[in]element- The element to delete.
Returns
true on success, false otherwise.

Definition at line 58 of file utils.h.

◆ vectors_have_same_content()

template<typename T >
bool hal::utils::vectors_have_same_content ( std::vector< T >  vec_1,
std::vector< T >  vec_2 
)

Check whether two vectors have the same content regardless of their order.

Parameters
[in]vec_1- First vector.
[in]vec_2- Second vector.
Returns
true if the vectors have the same content, false otherwise.

Definition at line 78 of file utils.h.

Referenced by hal::TEST_F().

◆ which()

CORE_API std::filesystem::path hal::utils::which ( const std::string &  name,
const std::string &  path = "" 
)

Locate an executable in the given path environment.

Parameters
[in]name- The name of the executable.
[in]path- The search path (defaults to PATH environment variable).
Returns
The path if an executable is found, an empty path otherwise.

Definition at line 242 of file utils.cpp.

References name, split(), and UNUSED.

Referenced by get_base_directory().

◆ wrapped_stoul()

CORE_API Result< u32 > hal::utils::wrapped_stoul ( const std::string &  s,
const u32  base = 10 
)

A safe wrapper around the std::stoul function that provides a Result<> for error handling instead of exceptions.

Parameters
[in]s- The string represntation of the number.
[in]base- The base of the integer, defaults to 10.
Returns
OK and an integer on success, an ERROR otherwise.

Definition at line 713 of file utils.cpp.

References ERR, and OK.

◆ wrapped_stoull()

CORE_API Result< u64 > hal::utils::wrapped_stoull ( const std::string &  s,
const u32  base = 10 
)

A safe wrapper around the std::stoull function that provides a Result<> for error handling instead of exceptions.

Parameters
[in]s- The string represntation of the number.
[in]base- The base of the integer, defaults to 10.
Returns
OK and an integer on success, an ERROR otherwise.

Definition at line 695 of file utils.cpp.

References ERR, and OK.