gempyre  1.7.1
gempyre_utils.h File Reference
#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>
Include dependency graph for gempyre_utils.h:

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 > &params)
 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 > &parameters)
 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 >
GempyreUtils::option_or (const Options &opts, std::string_view key, const T &default_value)
 get option as type More...
 

Detailed Description

wqe

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.

Enumeration Type Documentation

◆ ArgType

enum GempyreUtils::ArgType
strong

Option Argument type for parse_args.

Enumerator
REQ_ARG 

Option does not have an argument.

OPT_ARG 

Option has an argument.

Option may have an argument

◆ LogLevel

enum GempyreUtils::LogLevel : int
strong

The LogLevel enum.

Enumerator
Fatal 

All logs disabled.

Error 

Execution ends here.

Warning 

Something is wrong, Default.

Info 

At least developer should be worried.

Debug 

Something developer should know.

Debug_Trace 

What is going on.

What is going on, and telling it floods output and impacts performance

◆ PathStyle

File Utils

Function Documentation

◆ chop() [1/2]

UTILS_EX std::string GempyreUtils::chop ( std::string_view  s)

remove newline from end of string

Parameters
sstring
Returns
string

◆ chop() [2/2]

UTILS_EX std::string GempyreUtils::chop ( std::string_view  s,
std::string_view  chopped 
)

remove a string from end

Parameters
s
chopped
Returns
string

◆ get_json_value()

UTILS_EX Result<JsonType> GempyreUtils::get_json_value ( const std::any &  any,
std::string_view  path 
)

Return a value from path.

Parameters
any
path
Returns
a value, if not found, the error return the closest path

◆ hexify()

UTILS_EX std::string GempyreUtils::hexify ( std::string_view  src,
std::string_view  pat 
)

URL hexify string.

Parameters
srcsource
patregex pattern that are separated in hexified sequences
Returns
string

◆ ip_addresses()

UTILS_EX std::vector<std::string> GempyreUtils::ip_addresses ( unsigned  addressType)

Try to resolve own ipaddresses.

Parameters
addressType
Returns
list of addresses

◆ is_valid_utf8()

UTILS_EX bool GempyreUtils::is_valid_utf8 ( std::string_view  str)

Test if a given string is a valid UTF-8 string.

Parameters
str
Returns
validity

◆ join() [1/3]

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.

Template Parameters
ITcontainer
Callableconversion
Parameters
beginbegin iterator
endend iterator
joinCharoptional glue string
foptional transform function
Returns
string

◆ join() [2/3]

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.

Template Parameters
ITcontainer
Callableconversion
Parameters
beginbegin iterator
endend iterator
joinCharoptional glue string
foptional transform function
Returns
string

◆ join() [3/3]

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.

Template Parameters
ITcontainer
Callableconversion
Parameters
tcontainer
joinCharoptional glue string
foptional transform function
Returns
string

◆ json_to_any()

UTILS_EX Result<std::any> GempyreUtils::json_to_any ( std::string_view  str,
MapType  map_type = MapType::UnorderedMap 
)

Concert json string to any type.

Parameters
str
map_typetells if json dict is translated to std::map or std::unordered_map
Returns

◆ keys()

template<class T , typename K = typename T::key_type>
std::vector<K> GempyreUtils::keys ( const T &  map)

Get keys from map.

Template Parameters
Tmap type
Kkey type, defaults to key type
Parameters
map
Returns
vector

◆ levenshtein_distance()

UTILS_EX int GempyreUtils::levenshtein_distance ( std::string_view  s1,
std::string_view  s2 
)

Get a levenshtein distance of strings.

Parameters
s1
s2
Returns
their distance

◆ log()

template<typename T , typename ... Args>
void GempyreUtils::log ( LogLevel  level,
const T &  e,
Args...  args 
)
inline

Write a log line.

Parameters
levellog level of this message.
eparam to print.
argsoptional more parameters to print.

◆ log_level()

UTILS_EX LogLevel GempyreUtils::log_level ( )

Get current log level.

Returns
current log level.

◆ ltrim()

std::string_view GempyreUtils::ltrim ( std::string_view  str)
inline

trim from left

Parameters
str
Returns
trimmed string view

◆ make_error()

template<typename T , typename... A>
Result<T, std::string> GempyreUtils::make_error ( A &&...  a)
Template Parameters
T
...A
Parameters
...a
Returns

◆ make_json_path()

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

Parameters
any
path
f- default builds empty dictionaries for strings and vectors for numbers
Returns

◆ option_or()

template<typename T >
T GempyreUtils::option_or ( const Options opts,
std::string_view  key,
const T &  default_value 
)

get option as type

Template Parameters
T
Parameters
opts
key
default_valuethat is returned if not found or cannot interpreted as a requested type
Returns

◆ parse()

template<typename T >
std::optional<T> GempyreUtils::parse ( std::string_view  source)

parse string to value

Template Parameters
T
Parameters
source
Returns
value

◆ parse_args()

UTILS_EX Params GempyreUtils::parse_args ( int  argc,
char *  argv[],
const std::initializer_list< std::tuple< std::string, char, ArgType >> &  args 
)

parse arguments

Parameters
argcargc from main
argvargv from main
argsOptional arguments, each tuple of 'long name', 'short name' and type
Returns
tuple of ParamList and Options, where ParamList is vector of positional parameters and Options is map of options and their possible values.

◆ path_pop()

UTILS_EX std::string GempyreUtils::path_pop ( std::string_view  filename,
int  steps = 1,
PathStyle  path_style = PathStyle::Native 
)

Remove elements from path.

Parameters
filename
steps- number of elements removed, default 1
path_style- filesystem native, slash or backslash , default native
Returns
- remaining path

◆ qq()

UTILS_EX std::string GempyreUtils::qq ( std::string_view  s)

make quoted

String Utils

Returns
quoted string

◆ read_process()

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.

Parameters
processName
paramsprocess parameters
Returns
optional parameters

◆ remove_json_value()

UTILS_EX ResultTrue GempyreUtils::remove_json_value ( std::any &  any,
std::string_view  path 
)
Parameters
any
path
Returns

◆ remove_spaces()

UTILS_EX std::string GempyreUtils::remove_spaces ( std::string_view  str)

remove

Parameters
strspaces from a string
Returns
string

◆ rtrim()

std::string_view GempyreUtils::rtrim ( std::string_view  str)
inline

trim from right

Parameters
str
Returns
trimmed string view

◆ set_json_value()

UTILS_EX ResultTrue GempyreUtils::set_json_value ( std::any &  any,
std::string_view  path,
JsonType &&  value 
)

Modifies a given data.

Parameters
any
pathThe / 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
Returns
The modified data or if path is not found, the closest path

◆ set_log_level()

UTILS_EX void GempyreUtils::set_log_level ( LogLevel  level)

Set current log level.

Parameters
level

◆ slurp()

template<class T >
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.

Template Parameters
Ttype of value packed in the returned vector.
Parameters
fileto read.
maxmaximum amount of data to read.
Returns
Vector of data read
string containing file

some platforms needs std::string

◆ split()

template<class Container = std::vector<std::string>>
Container GempyreUtils::split ( std::string_view  str,
const char  splitChar = ' ' 
)

Split sting to container.

Container Utils

Parameters
str
splitChar
Returns
container

◆ substitute()

UTILS_EX std::string GempyreUtils::substitute ( std::string_view  str,
std::string_view  substring,
std::string_view  substitution 
)

replace a substrings from a string

Parameters
stroriginal string
substringregular expression
substitutionreplacement
Returns
string

◆ to_hex()

template<typename T >
std::string GempyreUtils::to_hex ( ival)

Hex presentation of the value.

Parameters
ival
Returns
string

◆ to_json_string()

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.

Parameters
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}};
Returns
json string.

◆ to_low()

template<class T >
std::string GempyreUtils::to_low ( const T &  str)

make lower case string

Parameters
str
Returns
string

◆ to_str()

UTILS_EX std::string GempyreUtils::to_str ( LogLevel  log_level)

Log Level to string.

Parameters
log_levelthat is converted to string.
Returns
std::string

◆ to_upper()

template<class T >
std::string GempyreUtils::to_upper ( const T &  str)

make upper case string

Parameters
str
Returns
string

◆ trim()

std::string_view GempyreUtils::trim ( std::string_view  str)
inline

trim from left and right

Parameters
str
Returns
trimmed string view

◆ unhexify()

UTILS_EX std::string GempyreUtils::unhexify ( std::string_view  src)

un hexify hexified string

Parameters
src
Returns
string

◆ write_to_temp()

template<class T >
std::string GempyreUtils::write_to_temp ( const T &  data)

Write data to temp file.

Template Parameters
T
Parameters
data
Returns
filename where data was written to.