MADARA  3.2.3
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 
55  MADARA_EXPORT uint32_t get_uint_version (void);
56 
62  MADARA_EXPORT std::string to_string_version (uint32_t version);
63 
73  MADARA_EXPORT size_t string_replace (std::string & source,
74  const std::string & old_phrase,
75  const std::string & new_phrase,
76  bool replace_all = true);
77 
84  std::string & upper (std::string & input);
85 
92  std::string & lower (std::string & input);
93 
102 
110 
117  MADARA_EXPORT std::string & strip_comments (std::string & input);
118 
126  MADARA_EXPORT std::string & string_remove (std::string & input,
127  char unwanted);
128 
136  MADARA_EXPORT std::string & strip_extra_white_space (
137  std::string & input);
138 
149  const std::string & input, const std::string & prefix);
150 
158  MADARA_EXPORT int split_hostport_identifier (const std::string & key,
159  std::string & host, std::string & port);
160 
168  MADARA_EXPORT int merge_hostport_identifier (std::string & key,
169  const std::string & host, const std::string & port);
170 
178  MADARA_EXPORT int merge_hostport_identifier (std::string & key,
179  const std::string & host, unsigned short u_port);
180 
189  MADARA_EXPORT void tokenizer (const std::string & input,
190  const ::std::vector< std::string> & splitters,
191  ::std::vector< std::string> & tokens,
192  ::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 
221  MADARA_EXPORT std::string expand_envs (
222  const std::string & source);
223 
232  char * get_var (const std::string & source, size_t cur, size_t & end);
233 
240  MADARA_EXPORT std::string clean_dir_name (const std::string & target);
241 
248  uint64_t endian_swap (uint64_t value);
249 
256  int64_t endian_swap (int64_t value);
257 
264  uint32_t endian_swap (uint32_t value);
265 
272  int32_t endian_swap (int32_t value);
273 
280  uint16_t endian_swap (uint16_t value);
281 
288  int16_t endian_swap (int16_t value);
289 
296  double endian_swap (double value);
297 
307  MADARA_EXPORT int read_file (const std::string & filename,
308  void *& buffer, size_t & size, bool add_zero_char = false);
309 
317  MADARA_EXPORT ssize_t write_file (const std::string & filename,
318  void * buffer, size_t size);
319 
327  int64_t get_time (void);
328 
336  TimeValue get_time_value (void);
337 
342  TimeValue add_seconds (const TimeValue & start, double seconds);
343 
348  Duration seconds_to_duration (double seconds);
349 
354  SecondsDuration seconds_to_seconds_duration (double seconds);
355 
360  TimeValue seconds_to_time (double seconds);
361 
369  MADARA_EXPORT double rand_double (double floor = 0.0,
370  double ceiling = 1.0, bool set_seed_to_time = true);
371 
379  MADARA_EXPORT int64_t rand_int (int64_t floor = 0,
380  int64_t ceiling = RAND_MAX, bool set_seed_to_time = true);
381 
387  MADARA_EXPORT int64_t nearest_int (double input);
388 
395  MADARA_EXPORT bool begins_with (const std::string & input,
396  const std::string & prefix);
397 
404  MADARA_EXPORT bool ends_with (const std::string & input,
405  const std::string & ending);
406 
412  MADARA_EXPORT double sleep (double sleep_time);
413 
419  MADARA_EXPORT SecondsDuration sleep (const SecondsDuration & sleep_time);
420 
432  bool approx_equal (
433  double value1, double value2, double epsilon = 0.0001);
434 
440  bool file_exists (const std::string & filename);
441 
447  unsigned int file_size (const std::string & filename);
448 
456  MADARA_EXPORT bool wait_true (knowledge::KnowledgeBase & knowledge,
457  const std::string & variable,
458  const knowledge::WaitSettings & settings =
460 
461 
462 
470  MADARA_EXPORT bool wait_false (knowledge::KnowledgeBase & knowledge,
471  const std::string & variable,
472  const knowledge::WaitSettings & settings =
477  template <typename T>
478  bool less_compare (const T & left, const T & right);
479 
483  template <typename T>
484  bool greater_compare (const T & left, const T & right);
485 
493  template <typename T>
494  void sift_down (T * input, int start, int end,
495  bool (*comparator) (const T & left, const T & right) =
496  greater_compare<T>);
497 
504  template <typename T>
505  void heapify (T * input, int size,
506  bool (*comparator) (const T & left, const T & right) =
507  greater_compare<T>);
508 
515  template <typename T>
516  void heap_sort (T * input, int size,
517  bool (*comparator) (const T & left, const T & right) =
518  greater_compare<T>);
519 
526  template <typename T>
527  T bitmask_add (T mask, T values);
528 
535  template <typename T>
536  bool bitmask_check (T mask, T values);
537 
544  template <typename T>
545  T bitmask_remove (T mask, T values);
546 
547  std::pair<std::string, uint16_t> parse_address (std::string addr);
548  }
549 }
550 
551 #include "Utility.inl"
552 
553 #endif // _MADARA_UTILITY_H_
std::string extract_filename(const std::string &name)
Extracts the file name of an absolute or relative path.
Definition: Utility.cpp:400
std::chrono::duration< double > SecondsDuration
default clock duration
Definition: Utility.h:33
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:242
bool bitmask_check(T mask, T values)
Returns true if mask contains values.
Definition: Utility.inl:356
TimeValue add_seconds(const TimeValue &start, double seconds)
Returns an offset of a time by seconds in double format.
Definition: Utility.inl:261
std::pair< std::string, uint16_t > parse_address(std::string addr)
Definition: Utility.cpp:789
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:480
std::string & strip_comments(std::string &input)
Strips all comments (single-line and multi-line).
Definition: Utility.cpp:210
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:253
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:90
SecondsDuration seconds_to_seconds_duration(double seconds)
Returns seconds in double format as seconds duration.
Definition: Utility.inl:272
MADARA_EXPORT bool set_thread_priority(int priority=20)
Sets the thread priority in a FIFO scheme.
Definition: Utility.inl:17
T bitmask_add(T mask, T values)
Adds values to a bit mask.
Definition: Utility.inl:347
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:721
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:52
unsigned int file_size(const std::string &filename)
Returns the size of a file.
Definition: Utility.inl:303
Duration seconds_to_duration(double seconds)
Returns seconds in double format as nanosecond duration.
Definition: Utility.inl:266
std::string & lower(std::string &input)
Converts the string to lower.
Definition: Utility.inl:87
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:387
std::string extract_path(const std::string &name)
Extracts the path of a filename.
Definition: Utility.cpp:380
std::chrono::time_point< Clock > TimeValue
time point
Definition: Utility.h:36
std::chrono::steady_clock Clock
default clock type
Definition: Utility.h:27
std::string & upper(std::string &input)
Converts the string to upper.
Definition: Utility.inl:61
bool greater_compare(const T &left, const T &right)
Returns true if right < left.
Definition: Utility.inl:380
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:428
double sleep(double sleep_time)
Sleeps for a certain amount of time.
Definition: Utility.cpp:619
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:243
static struct madara::knowledge::tags::string_t string
std::string to_string_version(uint32_t version)
Converts a MADARA uint32_t version number to human-readable.
Definition: Utility.cpp:68
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
int64_t nearest_int(double input)
Rounds a double to the nearest integer.
Definition: Utility.cpp:606
std::string file_to_string(const std::string &filename)
Reads a file into a string.
Definition: Utility.cpp:347
std::string & dds_topicify(std::string &input)
Changes periods to underscores in compliance with OpenSplice needs.
Definition: Utility.inl:72
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:298
uint32_t get_uint_version(void)
Gets the MADARA version number.
Definition: Utility.cpp:32
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:653
std::string & strip_white_space(std::string &input)
Strip all whitespace.
Definition: Utility.cpp:132
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:416
double endian_swap(double value)
Converts a host format double precision into big endian.
Definition: Utility.inl:218
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:457
TimeValue seconds_to_time(double seconds)
Returns seconds in double format as nanosecond since epoch.
Definition: Utility.inl:277
T bitmask_remove(T mask, T values)
Removes values from a bit mask.
Definition: Utility.inl:365
double rand_double(double floor, double ceiling, bool set_seed_to_time)
Returns a random double between floor and ceiling.
Definition: Utility.cpp:580
std::string expand_envs(const std::string &source)
Expand any environment variables in a string.
Definition: Utility.cpp:421
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:598
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:14
Provides functions and classes for the distributed knowledge base.
bool file_exists(const std::string &filename)
Checks if a file exists.
Definition: Utility.inl:289
bool less_compare(const T &left, const T &right)
Returns true if left < right.
Definition: Utility.inl:373
std::string & string_remove(std::string &input, char unwanted)
Strips an unwanted character.
Definition: Utility.cpp:160
Copyright (c) 2015 Carnegie Mellon University.
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
Definition: Utility.inl:317
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:323
Encapsulates settings for a wait statement.
Definition: WaitSettings.h:23
std::string get_version(void)
Gets the MADARA version number.
Definition: Utility.cpp:22
ssize_t write_file(const std::string &filename, void *buffer, size_t size)
Writes a file with provided contents.
Definition: Utility.cpp:547
bool approx_equal(double value1, double value2, double epsilon=0.0001)
Checks two doubles for approximate equality.
Definition: Utility.inl:282
char * get_var(const std::string &source, size_t cur, size_t &end)
grab an environment variable value (
Definition: Utility.cpp:442
std::chrono::nanoseconds Duration
default clock duration
Definition: Utility.h:30
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:331
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:183