1 #ifndef _MADARA_UTILITY_H_
2 #define _MADARA_UTILITY_H_
11 #include "madara/MadaraExport.h"
27 typedef std::chrono::steady_clock
Clock;
80 bool replace_all =
true);
191 const ::std::vector<std::string>& splitters,
192 ::std::vector<std::string>& tokens, ::std::vector<std::string>& pivot_list);
242 uint32_t crc,
size_t expected_size,
size_t fragment_size = 60000);
255 const std::string& filename, uint32_t crc,
size_t expected_size,
256 int max_fragments = -1,
size_t fragment_size = 60000);
272 MADARA_EXPORT
void strncpy_safe(
char * dst,
const char * src,
size_t dst_size);
358 size_t& size,
bool add_zero_char =
false);
368 const std::string& filename,
void* buffer,
size_t size);
378 const std::string delimiter =
"\n",
bool strip_whitespace =
true);
436 double floor = 0.0,
double ceiling = 1.0,
bool set_seed_to_time =
true);
445 MADARA_EXPORT int64_t
rand_int(int64_t floor = 0, int64_t ceiling = RAND_MAX,
446 bool set_seed_to_time =
true);
478 MADARA_EXPORT
double sleep(
double sleep_time);
502 bool approx_equal(
double value1,
double value2,
double epsilon = 0.0001);
550 MADARA_EXPORT
void safe_clear(std::vector<std::string> & strings);
593 void sift_down(T* input,
int start,
int end,
594 bool (*comparator)(
const T& left,
const T& right) = greater_compare<T>);
603 void heapify(T* input,
int size,
604 bool (*comparator)(
const T& left,
const T& right) = greater_compare<T>);
614 bool (*comparator)(
const T& left,
const T& right) = greater_compare<T>);
This class provides a distributed knowledge base to users.
Provides functions and classes for the distributed knowledge base.
MADARA_EXPORT void delete_fragments(FragmentMap &map)
Deletes fragments within a fragment map and clears the map.
Provides utility functions and classes for common tasks and needs.
uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
std::string & strip_extra_white_space(std::string &input)
Strip whitespace from front and end of string and also condense multiple whitespace into a single spa...
std::string file_to_string(const std::string &filename)
Reads a file into a string.
MADARA_EXPORT bool set_thread_priority(int priority=20)
Sets the thread priority in a FIFO scheme.
int merge_hostport_identifier(std::string &key, const std::string &host, const std::string &port)
Merges a host and port into a host:port key.
bool wait_false(knowledge::KnowledgeBase &knowledge, const std::string &variable, const knowledge::WaitSettings &settings)
Waits on a knowledge record to be false without needing KaRL language.
void safe_clear(std::vector< std::string > &strings)
Safely clear a vector of STL strings when an application has been compiled with a different version o...
unsigned int file_size(const std::string &filename)
Returns the size of a file.
bool greater_compare(const T &left, const T &right)
Returns true if right < left.
ssize_t write_file(const std::string &filename, void *buffer, size_t size)
Writes a file with provided contents.
std::string & strip_white_space(std::string &input)
Strip all whitespace.
std::string & string_remove(std::string &input, char unwanted)
Strips an unwanted character.
std::string & lower(std::string &input)
Converts the string to lower.
int64_t rand_int(int64_t floor, int64_t ceiling, bool set_seed_to_time)
Returns a random integer between a floor and ceiling.
T bitmask_add(T mask, T values)
Adds values to a bit mask.
bool file_exists(const std::string &filename)
Checks if a file exists.
std::chrono::time_point< Clock > TimeValue
time point
MADARA_EXPORT bool ends_with(const std::string &input, const std::string &ending)
Check if input contains a pattern at the end.
std::string to_string_version(uint32_t version)
Converts a MADARA uint32_t version number to human-readable.
double rand_double(double floor, double ceiling, bool set_seed_to_time)
Returns a random double between floor and ceiling.
std::chrono::nanoseconds Duration
default clock duration
T bitmask_remove(T mask, T values)
Removes values from a bit mask.
void sift_down(T *input, int start, int end, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Sifts elements down a heap according to a comparator.
void tokenizer(const std::string &input, const ::std::vector< std::string > &splitters, ::std::vector< std::string > &tokens, ::std::vector< std::string > &pivots)
Split a string into tokens.
bool less_compare(const T &left, const T &right)
Returns true if left < right.
uint32_t file_crc(const std::string &filename, size_t max_block=1000000)
Returns the crc of a file.
double sleep(double sleep_time)
Sleeps for a certain amount of time.
MADARA_EXPORT bool file_from_fragments(const std::string &filename, uint32_t crc, bool delete_incomplete=true, bool delete_fragments=true)
Builds a file from fragments that have the format: filename.
bool is_directory(const std::string &path)
Checks if a a path is a directory.
void heapify(T *input, int size, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Builds a heap out of an array of elements.
TimeValue seconds_to_time(double seconds)
Returns seconds in double format as nanosecond since epoch.
std::vector< std::string > string_to_vector(const std::string &input, const std::string delimiter="\n", bool strip_whitespace=true)
Splits a string into a vector of strings by delimiter.
MADARA_EXPORT std::string extract_filename(const std::string &name)
Extracts the file name of an absolute or relative path.
MADARA_EXPORT size_t get_file_progress(const std::string &filename, uint32_t crc, size_t expected_size, size_t fragment_size=60000)
Builds a file from fragments that have the format: filename.
char * get_var(const std::string &source, size_t cur, size_t &end)
grab an environment variable value (
std::string get_version(void)
Gets the MADARA version number.
std::string & upper(std::string &input)
Converts the string to upper.
bool bitmask_check(T mask, T values)
Returns true if mask contains values.
int64_t nearest_int(double input)
Rounds a double to the nearest integer.
std::string & dds_topicify(std::string &input)
Changes periods to underscores in compliance with OpenSplice needs.
std::string strip_prefix(const std::string &input, const std::string &prefix)
Strips a prefix from an input string and returns the result.
MADARA_EXPORT std::vector< int64_t > get_file_missing_fragments(const std::string &filename, uint32_t crc, size_t expected_size, int max_fragments=-1, size_t fragment_size=60000)
Attempts to builds a file from fragments that have the format: filename.
SecondsDuration seconds_to_seconds_duration(double seconds)
Returns seconds in double format as seconds duration.
size_t string_replace(std::string &source, const std::string &old_phrase, const std::string &new_phrase, bool replace_all)
Replaces an old phrase with a new phrase within a string.
std::string & strip_comments(std::string &input)
Strips all comments (single-line and multi-line).
int64_t seconds_to_nanoseconds(double seconds)
Returns seconds in nanoseconds.
std::chrono::steady_clock Clock
default clock type
int64_t get_time(void)
Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time inste...
MADARA_EXPORT void strncpy_safe(char *dst, const char *src, size_t dst_size)
Performs a strncpy in a way that will compile without warnings.
Duration sleep_until(TimeValue wake)
std::string expand_envs(const std::string &source)
Expand any environment variables in a string.
bool approx_equal(double value1, double value2, double epsilon=0.0001)
Checks two doubles for approximate equality.
std::chrono::duration< double > SecondsDuration
default clock duration
std::pair< std::string, uint16_t > parse_address(std::string addr)
MADARA_EXPORT std::string extract_path(const std::string &name)
Extracts the path of a filename.
TimeValue get_time_value(void)
Returns a time of day as a chrono time value If simtime feature is enabled, this may be simulation ti...
bool wait_true(knowledge::KnowledgeBase &knowledge, const std::string &variable, const knowledge::WaitSettings &settings)
Waits on a knowledge record to be true without needing KaRL language.
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
int split_hostport_identifier(const std::string &key, std::string &host, std::string &port)
Splits a key of host:port into a corresponding host and port.
void heap_sort(T *input, int size, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Sorts an array with heap sort.
int read_file(const std::string &filename, void *&buffer, size_t &size, bool add_zero_char)
Reads a file into a provided void pointer.
std::string clean_dir_name(const std::string &source)
Substitutes the appropriate directory delimiter, which may help with portability between operating sy...
TimeValue add_seconds(const TimeValue &start, double seconds)
Returns an offset of a time by seconds in double format.
MADARA_EXPORT bool filename_has_redirect(const std::string &filename)
Checks the filename for abnormal redirects such as "..".
uint32_t get_uint_version(const std::string &str_version)
Converts a string version to a uint32.
Duration seconds_to_duration(double seconds)
Returns seconds in double format as nanosecond duration.
Copyright(c) 2020 Galois.
Encapsulates settings for a wait statement.