MADARA  3.4.1
Utility.h
Go to the documentation of this file.
1 #ifndef _MADARA_UTILITY_H_
2 #define _MADARA_UTILITY_H_
3 
4 #include <vector>
5 #include <string>
6 #include <cmath>
7 #include <limits.h>
8 #include <cstdlib>
9 #include <cstring>
10 #include <chrono>
11 #include "madara/MadaraExport.h"
12 #include "madara/utility/StdInt.h"
15 
16 namespace madara
17 {
18 namespace knowledge
19 {
20 class KnowledgeBase;
21 struct WaitSettings;
22 }
23 
24 namespace utility
25 {
27 typedef std::chrono::steady_clock Clock;
28 
30 typedef std::chrono::nanoseconds Duration;
31 
33 typedef std::chrono::duration<double> SecondsDuration;
34 
36 typedef std::chrono::time_point<Clock> TimeValue;
37 
43 MADARA_EXPORT bool set_thread_priority(int priority = 20);
44 
49 MADARA_EXPORT std::string get_version(void);
50 
59 MADARA_EXPORT uint32_t get_uint_version(
60  const std::string& str_version = get_version());
61 
67 MADARA_EXPORT std::string to_string_version(uint32_t version);
68 
78 MADARA_EXPORT size_t string_replace(std::string& source,
79  const std::string& old_phrase, const std::string& new_phrase,
80  bool replace_all = true);
81 
89 
97 
106 
113 MADARA_EXPORT std::string& strip_white_space(std::string& input);
114 
121 MADARA_EXPORT std::string& strip_comments(std::string& input);
122 
130 MADARA_EXPORT std::string& string_remove(std::string& input, char unwanted);
131 
139 MADARA_EXPORT std::string& strip_extra_white_space(std::string& input);
140 
150 std::string strip_prefix(const std::string& input, const std::string& prefix);
151 
159 MADARA_EXPORT int split_hostport_identifier(
160  const std::string& key, std::string& host, std::string& port);
161 
169 MADARA_EXPORT int merge_hostport_identifier(
170  std::string& key, const std::string& host, const std::string& port);
171 
179 MADARA_EXPORT int merge_hostport_identifier(
180  std::string& key, const std::string& host, unsigned short u_port);
181 
190 MADARA_EXPORT void tokenizer(const std::string& input,
191  const ::std::vector<std::string>& splitters,
192  ::std::vector<std::string>& tokens, ::std::vector<std::string>& pivot_list);
193 
199 MADARA_EXPORT std::string file_to_string(const std::string& filename);
200 
206 MADARA_EXPORT std::string extract_path(const std::string& name);
207 
213 MADARA_EXPORT std::string extract_filename(const std::string& name);
214 
220 MADARA_EXPORT bool filename_has_redirect(const std::string& filename);
221 
230 MADARA_EXPORT bool file_from_fragments(const std::string& filename,
231  uint32_t crc, bool delete_incomplete = true, bool delete_fragments = true);
232 
241 MADARA_EXPORT size_t get_file_progress(const std::string& filename,
242  uint32_t crc, size_t expected_size, size_t fragment_size = 60000);
243 
254 MADARA_EXPORT std::vector<int64_t> get_file_missing_fragments(
255  const std::string& filename, uint32_t crc, size_t expected_size,
256  int max_fragments = -1, size_t fragment_size = 60000);
257 
264 MADARA_EXPORT std::string expand_envs(const std::string& source);
265 
272 MADARA_EXPORT void strncpy_safe(char * dst, const char * src, size_t dst_size);
273 
282 char* get_var(const std::string& source, size_t cur, size_t& end);
283 
290 MADARA_EXPORT std::string clean_dir_name(const std::string& target);
291 
298 uint64_t endian_swap(uint64_t value);
299 
306 int64_t endian_swap(int64_t value);
307 
314 uint32_t endian_swap(uint32_t value);
315 
322 int32_t endian_swap(int32_t value);
323 
330 uint16_t endian_swap(uint16_t value);
331 
338 int16_t endian_swap(int16_t value);
339 
346 double endian_swap(double value);
347 
357 MADARA_EXPORT int read_file(const std::string& filename, void*& buffer,
358  size_t& size, bool add_zero_char = false);
359 
367 MADARA_EXPORT ssize_t write_file(
368  const std::string& filename, void* buffer, size_t size);
369 
377 std::vector <std::string> string_to_vector (const std::string & input,
378  const std::string delimiter = "\n", bool strip_whitespace = true);
379 
387 int64_t get_time(void);
388 
397 
402 TimeValue add_seconds(const TimeValue& start, double seconds);
403 
408 Duration seconds_to_duration(double seconds);
409 
414 int64_t seconds_to_nanoseconds(double seconds);
415 
421 
426 TimeValue seconds_to_time(double seconds);
427 
435 MADARA_EXPORT double rand_double(
436  double floor = 0.0, double ceiling = 1.0, bool set_seed_to_time = true);
437 
445 MADARA_EXPORT int64_t rand_int(int64_t floor = 0, int64_t ceiling = RAND_MAX,
446  bool set_seed_to_time = true);
447 
453 MADARA_EXPORT int64_t nearest_int(double input);
454 
461 MADARA_EXPORT bool begins_with(
462  const std::string& input, const std::string& prefix);
463 
470 MADARA_EXPORT bool ends_with(
471  const std::string& input, const std::string& ending);
472 
478 MADARA_EXPORT double sleep(double sleep_time);
479 
485 MADARA_EXPORT SecondsDuration sleep(const SecondsDuration& sleep_time);
486 
487 MADARA_EXPORT Duration sleep_until(uint64_t wake);
488 
489 MADARA_EXPORT Duration sleep_until(TimeValue wake);
490 
502 bool approx_equal(double value1, double value2, double epsilon = 0.0001);
503 
509 bool file_exists(const std::string& filename);
510 
516 bool is_directory(const std::string& path);
517 
523 unsigned int file_size(const std::string& filename);
524 
532 size_t file_size(std::ifstream& input);
533 
543 uint32_t file_crc(const std::string& filename, size_t max_block = 1000000);
544 
550 MADARA_EXPORT void safe_clear(std::vector<std::string> & strings);
551 
559 MADARA_EXPORT bool wait_true(knowledge::KnowledgeBase& knowledge,
560  const std::string& variable,
562 
570 MADARA_EXPORT bool wait_false(knowledge::KnowledgeBase& knowledge,
571  const std::string& variable,
576 template<typename T>
577 bool less_compare(const T& left, const T& right);
578 
582 template<typename T>
583 bool greater_compare(const T& left, const T& right);
584 
592 template<typename T>
593 void sift_down(T* input, int start, int end,
594  bool (*comparator)(const T& left, const T& right) = greater_compare<T>);
595 
602 template<typename T>
603 void heapify(T* input, int size,
604  bool (*comparator)(const T& left, const T& right) = greater_compare<T>);
605 
612 template<typename T>
613 void heap_sort(T* input, int size,
614  bool (*comparator)(const T& left, const T& right) = greater_compare<T>);
615 
622 template<typename T>
623 T bitmask_add(T mask, T values);
624 
631 template<typename T>
632 bool bitmask_check(T mask, T values);
633 
640 template<typename T>
641 T bitmask_remove(T mask, T values);
642 
643 std::pair<std::string, uint16_t> parse_address(std::string addr);
644 }
645 }
646 
647 #include "Utility.inl"
648 
649 #endif // _MADARA_UTILITY_H_
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
constexpr string_t string
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.
Definition: IteratorImpl.h:15
uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
Definition: Utility.inl:134
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...
Definition: Utility.cpp:85
std::string file_to_string(const std::string &filename)
Reads a file into a string.
Definition: Utility.cpp:324
MADARA_EXPORT bool set_thread_priority(int priority=20)
Sets the thread priority in a FIFO scheme.
Definition: Utility.inl:26
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.
Definition: Utility.cpp:302
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.
Definition: Utility.cpp:730
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...
Definition: Utility.cpp:811
unsigned int file_size(const std::string &filename)
Returns the size of a file.
Definition: Utility.inl:395
bool greater_compare(const T &left, const T &right)
Returns true if right < left.
Definition: Utility.inl:694
ssize_t write_file(const std::string &filename, void *buffer, size_t size)
Writes a file with provided contents.
Definition: Utility.cpp:487
std::string & strip_white_space(std::string &input)
Strip all whitespace.
Definition: Utility.cpp:124
std::string & string_remove(std::string &input, char unwanted)
Strips an unwanted character.
Definition: Utility.cpp:149
std::string & lower(std::string &input)
Converts the string to lower.
Definition: Utility.inl:89
int64_t rand_int(int64_t floor, int64_t ceiling, bool set_seed_to_time)
Returns a random integer between a floor and ceiling.
Definition: Utility.cpp:537
T bitmask_add(T mask, T values)
Adds values to a bit mask.
Definition: Utility.inl:664
bool file_exists(const std::string &filename)
Checks if a file exists.
Definition: Utility.inl:304
std::chrono::time_point< Clock > TimeValue
time point
Definition: Utility.h:36
MADARA_EXPORT bool ends_with(const std::string &input, const std::string &ending)
Check if input contains a pattern at the end.
Definition: Utility.inl:650
std::string to_string_version(uint32_t version)
Converts a MADARA uint32_t version number to human-readable.
Definition: Utility.cpp:64
double rand_double(double floor, double ceiling, bool set_seed_to_time)
Returns a random double between floor and ceiling.
Definition: Utility.cpp:520
std::chrono::nanoseconds Duration
default clock duration
Definition: Utility.h:30
T bitmask_remove(T mask, T values)
Removes values from a bit mask.
Definition: Utility.inl:682
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.
Definition: Utility.inl:700
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.
Definition: Utility.cpp:224
bool less_compare(const T &left, const T &right)
Returns true if left < right.
Definition: Utility.inl:688
uint32_t file_crc(const std::string &filename, size_t max_block=1000000)
Returns the crc of a file.
Definition: Utility.inl:423
double sleep(double sleep_time)
Sleeps for a certain amount of time.
Definition: Utility.cpp:555
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.
Definition: Utility.inl:466
bool is_directory(const std::string &path)
Checks if a a path is a directory.
Definition: Utility.inl:317
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.
Definition: Utility.inl:728
TimeValue seconds_to_time(double seconds)
Returns seconds in double format as nanosecond since epoch.
Definition: Utility.inl:288
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.
Definition: Utility.inl:111
MADARA_EXPORT std::string extract_filename(const std::string &name)
Extracts the file name of an absolute or relative path.
Definition: Utility.inl:384
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.
Definition: Utility.inl:528
char * get_var(const std::string &source, size_t cur, size_t &end)
grab an environment variable value (
Definition: Utility.cpp:383
std::string get_version(void)
Gets the MADARA version number.
Definition: Utility.cpp:23
std::string & upper(std::string &input)
Converts the string to upper.
Definition: Utility.inl:67
bool bitmask_check(T mask, T values)
Returns true if mask contains values.
Definition: Utility.inl:673
int64_t nearest_int(double input)
Rounds a double to the nearest integer.
Definition: Utility.cpp:543
std::string & dds_topicify(std::string &input)
Changes periods to underscores in compliance with OpenSplice needs.
Definition: Utility.inl:76
std::string strip_prefix(const std::string &input, const std::string &prefix)
Strips a prefix from an input string and returns the result.
Definition: Utility.inl:60
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.
Definition: Utility.inl:572
SecondsDuration seconds_to_seconds_duration(double seconds)
Returns seconds in double format as seconds duration.
Definition: Utility.inl:283
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.
Definition: Utility.cpp:170
std::string & strip_comments(std::string &input)
Strips all comments (single-line and multi-line).
Definition: Utility.cpp:194
int64_t seconds_to_nanoseconds(double seconds)
Returns seconds in nanoseconds.
Definition: Utility.inl:294
std::chrono::steady_clock Clock
default clock type
Definition: Utility.h:27
int64_t get_time(void)
Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time inste...
Definition: Utility.inl:265
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.
Definition: Utility.cpp:376
Duration sleep_until(TimeValue wake)
Definition: Utility.cpp:612
std::string expand_envs(const std::string &source)
Expand any environment variables in a string.
Definition: Utility.cpp:357
bool approx_equal(double value1, double value2, double epsilon=0.0001)
Checks two doubles for approximate equality.
Definition: Utility.inl:299
std::chrono::duration< double > SecondsDuration
default clock duration
Definition: Utility.h:33
std::pair< std::string, uint16_t > parse_address(std::string addr)
Definition: Utility.cpp:797
MADARA_EXPORT std::string extract_path(const std::string &name)
Extracts the path of a filename.
Definition: Utility.inl:374
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...
Definition: Utility.inl:256
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.
Definition: Utility.cpp:663
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
Definition: Utility.inl:638
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.
Definition: Utility.cpp:278
void heap_sort(T *input, int size, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Sorts an array with heap sort.
Definition: Utility.inl:739
int read_file(const std::string &filename, void *&buffer, size_t &size, bool add_zero_char)
Reads a file into a provided void pointer.
Definition: Utility.cpp:418
std::string clean_dir_name(const std::string &source)
Substitutes the appropriate directory delimiter, which may help with portability between operating sy...
Definition: Utility.cpp:396
TimeValue add_seconds(const TimeValue &start, double seconds)
Returns an offset of a time by seconds in double format.
Definition: Utility.inl:272
MADARA_EXPORT bool filename_has_redirect(const std::string &filename)
Checks the filename for abnormal redirects such as "..".
Definition: Utility.inl:445
uint32_t get_uint_version(const std::string &str_version)
Converts a string version to a uint32.
Definition: Utility.cpp:30
Duration seconds_to_duration(double seconds)
Returns seconds in double format as nanosecond duration.
Definition: Utility.inl:277
Copyright(c) 2020 Galois.
Encapsulates settings for a wait statement.
Definition: WaitSettings.h:25