gempyre
1.7.1
|
#include <ctime>
#include <chrono>
#include <vector>
#include <map>
#include <sstream>
#include <fstream>
#include <optional>
#include <variant>
#include <functional>
#include <algorithm>
#include <future>
#include <limits>
#include <iomanip>
#include <any>
#include <string_view>
Go to the source code of this file.
Classes | |
class | GempyreUtils::LogWriter |
Parent class for LogWriters. More... | |
struct | GempyreUtils::Error< E > |
Result is used like optional, but contains a fail reasoning. More... | |
struct | GempyreUtils::Result< R, E > |
similar as std::optional, but with error info (not 100% same, add delta if needed). More... | |
Macros | |
#define | gempyre_utils_assert(b) (b || GempyreUtils::do_fatal("Panic!", nullptr, __FILE__, __LINE__)) |
Release build assert. | |
#define | gempyre_utils_assert_x(b, x) (b || GempyreUtils::do_fatal(x, nullptr, __FILE__, __LINE__)) |
Release build assert with a message. | |
#define | gempyre_utils_assert_x_f(b, x, f) (b || GempyreUtils::do_fatal(x, f, __FILE__, __LINE__)) |
Release build assert with a message and a function executed before forced exit. | |
#define | gempyre_utils_fatal(x) GempyreUtils::do_fatal(x, nullptr, __FILE__, __LINE__) |
Forced exit with a message. | |
#define | gempyre_utils_fatal_f(x, f) GempyreUtils::do_fatal(x, f, __FILE__, __LINE__) |
Forced exit with a message and a function executed before forced exit. | |
#define | gempyre_utils_auto_clean(p, f) std::unique_ptr<std::remove_pointer<decltype(p)>::type, decltype(&f)> _ ## p (p, &f) |
RAII helper for pointer. | |
#define | gempyre_utils_auto_close(p, f) GempyreUtils::_Close<std::decay_t<decltype(p)>, decltype(&f)> _ ## p (p, &f) |
RAII helper for non-pointer. | |
Typedefs | |
using | GempyreUtils::ResultTrue = Result< std::true_type, std::string > |
the most simple Result that return | |
using | GempyreUtils::JsonType = std::variant< int, double, bool, std::string, std::nullptr_t, std::vector< std::any >, std::map< std::string, std::any >, std::unordered_map< std::string, std::any > > |
Json Type. | |
using | GempyreUtils::ParamList = std::vector< std::string > |
string vector of parameters. Used for parse_params. | |
using | GempyreUtils::Options = std::multimap< std::string, std::string > |
string - string value pairs of options. Used for parse_params. | |
using | GempyreUtils::Params = std::tuple< ParamList, Options > |
a tuple containing ParamList and Options. Used for parse_params. | |
Enumerations | |
enum class | GempyreUtils::LogLevel : int { None , Fatal , Error , Warning , Info , Debug , Debug_Trace } |
The LogLevel enum. More... | |
enum class | GempyreUtils::OS { OtherOs , MacOs , WinOs , LinuxOs , AndroidOs , RaspberryOs } |
OS id. | |
enum | GempyreUtils::AddressType : unsigned { Ipv4 = 0x1 , Ipv6 = 0x2 } |
address type | |
enum class | GempyreUtils::PathStyle { Native , Unix , Win } |
enum class | GempyreUtils::JsonMode { Compact , Pretty } |
How json represented as a string. | |
enum class | GempyreUtils::MapType { Map , UnorderedMap } |
Json dictionary conversion. | |
enum class | GempyreUtils::ArgType { NO_ARG , REQ_ARG , OPT_ARG } |
Option Argument type for parse_args. More... | |
Functions | |
UTILS_EX void | GempyreUtils::set_log_level (LogLevel level) |
Set current log level. More... | |
UTILS_EX LogLevel | GempyreUtils::log_level () |
Get current log level. More... | |
UTILS_EX std::string | GempyreUtils::to_str (LogLevel log_level) |
Log Level to string. More... | |
template<typename T , typename ... Args> | |
void | GempyreUtils::log (LogLevel level, const T &e, Args... args) |
Write a log line. More... | |
template<typename T , typename ... Args> | |
void | GempyreUtils::log_debug (const T &e, Args... args) |
Write a debug log. | |
template<typename R , typename E > | |
constexpr bool | GempyreUtils::operator== (const Result< R, E > &a, const Result< R, E > &b) |
defined if underlaying types defines required operators | |
template<typename R , typename E > | |
constexpr bool | GempyreUtils::operator!= (const Result< R, E > &a, const Result< R, E > &b) |
defined if underlaying types defines required operators | |
template<typename T , typename... A> | |
Result< T, std::string > | GempyreUtils::make_error (A &&... a) |
UTILS_EX std::string | GempyreUtils::qq (std::string_view s) |
make quoted More... | |
UTILS_EX std::string | GempyreUtils::chop (std::string_view s) |
remove newline from end of string More... | |
UTILS_EX std::string | GempyreUtils::chop (std::string_view s, std::string_view chopped) |
remove a string from end More... | |
UTILS_EX std::string | GempyreUtils::substitute (std::string_view str, std::string_view substring, std::string_view substitution) |
replace a substrings from a string More... | |
UTILS_EX std::string | GempyreUtils::remove_spaces (std::string_view str) |
remove More... | |
template<typename T > | |
std::optional< T > | GempyreUtils::parse (std::string_view source) |
parse string to value More... | |
template<class T > | |
std::string | GempyreUtils::to_low (const T &str) |
make lower case string More... | |
template<class T > | |
std::string | GempyreUtils::to_upper (const T &str) |
make upper case string More... | |
std::string_view | GempyreUtils::ltrim (std::string_view str) |
trim from left More... | |
std::string_view | GempyreUtils::rtrim (std::string_view str) |
trim from right More... | |
std::string_view | GempyreUtils::trim (std::string_view str) |
trim from left and right More... | |
template<typename T > | |
std::string | GempyreUtils::to_hex (T ival) |
Hex presentation of the value. More... | |
UTILS_EX int | GempyreUtils::levenshtein_distance (std::string_view s1, std::string_view s2) |
Get a levenshtein distance of strings. More... | |
UTILS_EX bool | GempyreUtils::is_valid_utf8 (std::string_view str) |
Test if a given string is a valid UTF-8 string. More... | |
template<class Container = std::vector<std::string>> | |
Container | GempyreUtils::split (std::string_view str, const char splitChar=' ') |
Split sting to container. More... | |
template<class T , typename K = typename T::key_type> | |
std::vector< K > | GempyreUtils::keys (const T &map) |
Get keys from map. More... | |
template<typename IT , typename Callable = DefaultJoiner<typename IT::value_type>, typename = std::enable_if_t<!std::is_pointer<IT>::value>> | |
std::string | GempyreUtils::join (const IT &begin, const IT &end, std::string_view joinChar="", const Callable &f=Callable{}) |
Join container values, try 1st if compiler can deduct types. More... | |
template<typename IT , typename Callable = DefaultJoiner<typename std::remove_pointer<IT>::type>, typename = std::enable_if_t<std::is_pointer<IT>::value>> | |
std::string | GempyreUtils::join (const IT begin, const IT end, std::string_view joinChar="", const Callable &f=Callable{}) |
Join container values, try 1st if compiler can deduct types. More... | |
template<typename T , typename Callable = DefaultJoiner<typename T::value_type>> | |
std::string | GempyreUtils::join (const T &t, std::string_view joinChar="", const Callable &f=Callable{}) |
Join container values, try 1st if compiler can deduct types. More... | |
template<typename IT > | |
IT | GempyreUtils::advanced (IT it, int distance) |
Const version of std::advance. | |
template<typename K , typename V > | |
std::optional< V > | GempyreUtils::get_value (const std::multimap< K, V > &map, const K &key, int index=0) |
Get a value from a multimap, especially helper for Parameter Options. | |
UTILS_EX std::string | GempyreUtils::hexify (std::string_view src, std::string_view pat) |
URL hexify string. More... | |
UTILS_EX std::string | GempyreUtils::unhexify (std::string_view src) |
un hexify hexified string More... | |
UTILS_EX OS | GempyreUtils::current_os () |
Get. | |
UTILS_EX std::vector< std::string > | GempyreUtils::ip_addresses (unsigned addressType) |
Try to resolve own ipaddresses. More... | |
UTILS_EX std::string | GempyreUtils::get_link (std::string_view fname) |
Get symbolic link source. | |
UTILS_EX bool | GempyreUtils::is_dir (std::string_view fname) |
Is dir. | |
UTILS_EX std::string | GempyreUtils::working_dir () |
Current source dir. | |
UTILS_EX std::string | GempyreUtils::home_dir () |
Current source dir. | |
UTILS_EX std::string | GempyreUtils::root_dir () |
Current root dir - 'C:\' or '/'. | |
UTILS_EX std::string | GempyreUtils::abs_path (std::string_view rpath) |
Absolute path. | |
UTILS_EX std::string | GempyreUtils::path_pop (std::string_view filename, int steps=1, PathStyle path_style=PathStyle::Native) |
Remove elements from path. More... | |
UTILS_EX std::vector< std::string > | GempyreUtils::entries (std::string_view dirname) |
Directory entries. | |
UTILS_EX std::optional< std::string > | GempyreUtils::read_process (std::string_view processName, const std::vector< std::string > ¶ms) |
Read stdout from the process - wait process to end. More... | |
UTILS_EX std::string | GempyreUtils::base_name (std::string_view filename, PathStyle path_style=PathStyle::Native) |
Base name. | |
UTILS_EX std::tuple< std::string, std::string > | GempyreUtils::split_name (std::string_view filename, PathStyle path_style=PathStyle::Native) |
Name and extension. | |
UTILS_EX std::string | GempyreUtils::temp_name () |
Generate unique name (prefer std::filesystem if available) | |
UTILS_EX std::string | GempyreUtils::host_name () |
Machine host name. | |
UTILS_EX std::optional< std::string > | GempyreUtils::system_env (std::string_view env) |
Read environment value. | |
UTILS_EX bool | GempyreUtils::is_hidden_entry (std::string_view filename) |
Is entry hidden. | |
UTILS_EX bool | GempyreUtils::is_executable (std::string_view filename) |
is executable | |
UTILS_EX SSIZE_T | GempyreUtils::file_size (std::string_view filename) |
File size. | |
UTILS_EX bool | GempyreUtils::rename (std::string_view of, std::string_view nf) |
Rename a file. | |
UTILS_EX void | GempyreUtils::remove_file (std::string_view filename) |
Delete file. | |
UTILS_EX bool | GempyreUtils::file_exists (std::string_view filename) |
Test if file with name exits. | |
UTILS_EX std::optional< std::string > | GempyreUtils::which (std::string_view filename) |
Try to find a executable from PATH. | |
UTILS_EX std::string | GempyreUtils::push_path (std::string_view path, std::string_view name) |
push name to path | |
template<class ... NAME> | |
std::string | GempyreUtils::push_path (std::string_view path, std::string_view name, NAME...names) |
Construct a path from parameters. | |
UTILS_EX int | GempyreUtils::execute (std::string_view prog, const std::vector< std::string_view > ¶meters) |
Execute a program. | |
template<class ... PARAM> | |
UTILS_EX int | GempyreUtils::execute (std::string_view prog, PARAM...parameters) |
Execute a program. | |
template<class T > | |
std::string | GempyreUtils::write_to_temp (const T &data) |
Write data to temp file. More... | |
template<class T > | |
std::vector< T > | GempyreUtils::slurp (std::string_view file, const size_t max=std::numeric_limits< size_t >::max()) |
Read a file data in one read. More... | |
UTILS_EX Result< std::string > | GempyreUtils::to_json_string (const std::any &any, JsonMode mode=JsonMode::Compact) |
Convert any type to json string, if possible. More... | |
UTILS_EX Result< std::any > | GempyreUtils::json_to_any (std::string_view str, MapType map_type=MapType::UnorderedMap) |
Concert json string to any type. More... | |
UTILS_EX ResultTrue | GempyreUtils::set_json_value (std::any &any, std::string_view path, JsonType &&value) |
Modifies a given data. More... | |
UTILS_EX ResultTrue | GempyreUtils::remove_json_value (std::any &any, std::string_view path) |
UTILS_EX Result< JsonType > | GempyreUtils::get_json_value (const std::any &any, std::string_view path) |
Return a value from path. More... | |
UTILS_EX ResultTrue | GempyreUtils::make_json_path (std::any &any, std::string_view path, const std::function< JsonType(std::string_view, std::string_view)> &f=[](auto, auto name) { return(GempyreUtils::parse< int >(name)) ? GempyreUtils::JsonType{std::vector< std::any >{}} :GempyreUtils::JsonType{std::unordered_map< std::string, std::any >{}};}) |
ensure that json path exits More... | |
UTILS_EX bool | GempyreUtils::is_available (int port) |
Check if port is free. | |
UTILS_EX std::string | GempyreUtils::base64_encode (const unsigned char *bytes, size_t sz) |
Base64 encode. | |
UTILS_EX std::string | GempyreUtils::base64_encode (const std::vector< uint8_t > &vec) |
Base64 encode. | |
UTILS_EX std::string | GempyreUtils::base64_encode (std::string_view str) |
Base64 encode. | |
UTILS_EX std::vector< uint8_t > | GempyreUtils::base64_decode (std::string_view data) |
Base64 decode. | |
UTILS_EX Params | GempyreUtils::parse_args (int argc, char *argv[], const std::initializer_list< std::tuple< std::string, char, ArgType >> &args) |
parse arguments More... | |
template<typename T > | |
T | GempyreUtils::option_or (const Options &opts, std::string_view key, const T &default_value) |
get option as type More... | |
gempyre_utils.h contains a collection utility functions used internally within Gempyre implementation and test applications. The are in API as they may be useful for any developer. Gempyre-Python do not wrap these as Python asset have similar functions in internal or pip packages.
|
strong |
|
strong |
The LogLevel enum.
|
strong |
UTILS_EX std::string GempyreUtils::chop | ( | std::string_view | s | ) |
remove newline from end of string
s | string |
UTILS_EX std::string GempyreUtils::chop | ( | std::string_view | s, |
std::string_view | chopped | ||
) |
remove a string from end
s | |
chopped |
UTILS_EX Result<JsonType> GempyreUtils::get_json_value | ( | const std::any & | any, |
std::string_view | path | ||
) |
Return a value from path.
any | |
path |
UTILS_EX std::string GempyreUtils::hexify | ( | std::string_view | src, |
std::string_view | pat | ||
) |
URL hexify string.
src | source |
pat | regex pattern that are separated in hexified sequences |
UTILS_EX std::vector<std::string> GempyreUtils::ip_addresses | ( | unsigned | addressType | ) |
Try to resolve own ipaddresses.
addressType |
UTILS_EX bool GempyreUtils::is_valid_utf8 | ( | std::string_view | str | ) |
Test if a given string is a valid UTF-8 string.
str |
std::string GempyreUtils::join | ( | const IT & | begin, |
const IT & | end, | ||
std::string_view | joinChar = "" , |
||
const Callable & | f = Callable{} |
||
) |
Join container values, try 1st if compiler can deduct types.
IT | container |
Callable | conversion |
begin | begin iterator |
end | end iterator |
joinChar | optional glue string |
f | optional transform function |
std::string GempyreUtils::join | ( | const IT | begin, |
const IT | end, | ||
std::string_view | joinChar = "" , |
||
const Callable & | f = Callable{} |
||
) |
Join container values, try 1st if compiler can deduct types.
IT | container |
Callable | conversion |
begin | begin iterator |
end | end iterator |
joinChar | optional glue string |
f | optional transform function |
std::string GempyreUtils::join | ( | const T & | t, |
std::string_view | joinChar = "" , |
||
const Callable & | f = Callable{} |
||
) |
Join container values, try 1st if compiler can deduct types.
IT | container |
Callable | conversion |
t | container |
joinChar | optional glue string |
f | optional transform function |
UTILS_EX Result<std::any> GempyreUtils::json_to_any | ( | std::string_view | str, |
MapType | map_type = MapType::UnorderedMap |
||
) |
Concert json string to any type.
str | |
map_type | tells if json dict is translated to std::map or std::unordered_map |
std::vector<K> GempyreUtils::keys | ( | const T & | map | ) |
Get keys from map.
T | map type |
K | key type, defaults to key type |
map |
UTILS_EX int GempyreUtils::levenshtein_distance | ( | std::string_view | s1, |
std::string_view | s2 | ||
) |
Get a levenshtein distance of strings.
s1 | |
s2 |
|
inline |
Write a log line.
level | log level of this message. |
e | param to print. |
args | optional more parameters to print. |
UTILS_EX LogLevel GempyreUtils::log_level | ( | ) |
Get current log level.
|
inline |
trim from left
str |
Result<T, std::string> GempyreUtils::make_error | ( | A &&... | a | ) |
T | |
...A |
...a |
UTILS_EX ResultTrue GempyreUtils::make_json_path | ( | std::any & | any, |
std::string_view | path, | ||
const std::function< JsonType(std::string_view, std::string_view)> & | f = [](auto, auto name) { return(GempyreUtils::parse< int >(name)) ? GempyreUtils::JsonType{std::vector< std::any >{}} :GempyreUtils::JsonType{std::unordered_map< std::string, std::any >{}};} |
||
) |
ensure that json path exits
any | |
path | |
f | - default builds empty dictionaries for strings and vectors for numbers |
T GempyreUtils::option_or | ( | const Options & | opts, |
std::string_view | key, | ||
const T & | default_value | ||
) |
get option as type
T |
opts | |
key | |
default_value | that is returned if not found or cannot interpreted as a requested type |
std::optional<T> GempyreUtils::parse | ( | std::string_view | source | ) |
parse string to value
T |
source |
UTILS_EX Params GempyreUtils::parse_args | ( | int | argc, |
char * | argv[], | ||
const std::initializer_list< std::tuple< std::string, char, ArgType >> & | args | ||
) |
parse arguments
argc | argc from main |
argv | argv from main |
args | Optional arguments, each tuple of 'long name', 'short name' and type |
UTILS_EX std::string GempyreUtils::path_pop | ( | std::string_view | filename, |
int | steps = 1 , |
||
PathStyle | path_style = PathStyle::Native |
||
) |
Remove elements from path.
filename | |
steps | - number of elements removed, default 1 |
path_style | - filesystem native, slash or backslash , default native |
UTILS_EX std::string GempyreUtils::qq | ( | std::string_view | s | ) |
UTILS_EX std::optional<std::string> GempyreUtils::read_process | ( | std::string_view | processName, |
const std::vector< std::string > & | params | ||
) |
Read stdout from the process - wait process to end.
processName | |
params | process parameters |
UTILS_EX ResultTrue GempyreUtils::remove_json_value | ( | std::any & | any, |
std::string_view | path | ||
) |
any | |
path |
UTILS_EX std::string GempyreUtils::remove_spaces | ( | std::string_view | str | ) |
remove
str | spaces from a string |
|
inline |
trim from right
str |
UTILS_EX ResultTrue GempyreUtils::set_json_value | ( | std::any & | any, |
std::string_view | path, | ||
JsonType && | value | ||
) |
Modifies a given data.
any | |
path | The / separated string. If item is vector, the value shall be index. If index more than vector size, the vector is expanded with invalid values. Note: If there are invalid values, to_json_string will fail. |
value | |
map_type |
UTILS_EX void GempyreUtils::set_log_level | ( | LogLevel | level | ) |
Set current log level.
level |
UTILS_EX std::string GempyreUtils::slurp | ( | std::string_view | file, |
const size_t | max = std::numeric_limits< size_t >::max() |
||
) |
Read a file data in one read.
Read a file in one read.
T | type of value packed in the returned vector. |
file | to read. |
max | maximum amount of data to read. |
some platforms needs std::string
Container GempyreUtils::split | ( | std::string_view | str, |
const char | splitChar = ' ' |
||
) |
UTILS_EX std::string GempyreUtils::substitute | ( | std::string_view | str, |
std::string_view | substring, | ||
std::string_view | substitution | ||
) |
replace a substrings from a string
str | original string |
substring | regular expression |
substitution | replacement |
std::string GempyreUtils::to_hex | ( | T | ival | ) |
Hex presentation of the value.
ival |
UTILS_EX Result<std::string> GempyreUtils::to_json_string | ( | const std::any & | any, |
JsonMode | mode = JsonMode::Compact |
||
) |
Convert any type to json string, if possible.
any | - assumed to be a type convertible to json: int, string, boolean, null, double, Please note that map type values should be string and value type is container, it must be must be wrapped in std::any - e.g. std::vector<std::any> vector_of_vectors {std::make_any<std::vector<int>>{1, 2, 3}}; |
std::string GempyreUtils::to_low | ( | const T & | str | ) |
make lower case string
str |
UTILS_EX std::string GempyreUtils::to_str | ( | LogLevel | log_level | ) |
Log Level to string.
log_level | that is converted to string. |
std::string GempyreUtils::to_upper | ( | const T & | str | ) |
make upper case string
str |
|
inline |
trim from left and right
str |
UTILS_EX std::string GempyreUtils::unhexify | ( | std::string_view | src | ) |
un hexify hexified string
src |
std::string GempyreUtils::write_to_temp | ( | const T & | data | ) |
Write data to temp file.
T |
data |