MADARA  3.1.8
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 "ace/SOCK_Dgram.h"
7 #include "ace/High_Res_Timer.h"
8 #include "madara/MADARA_export.h"
11 
12 
13 namespace madara
14 {
15  namespace knowledge
16  {
17  class KnowledgeBase;
18  struct WaitSettings;
19  }
20 
21  namespace utility
22  {
27  MADARA_Export std::string get_version (void);
28 
33  MADARA_Export uint32_t get_uint_version (void);
34 
40  MADARA_Export std::string to_string_version (uint32_t version);
41 
51  MADARA_Export size_t string_replace (std::string & source,
52  const std::string & old_phrase,
53  const std::string & new_phrase,
54  bool replace_all = true);
55 
62  MADARA_Export std::string & upper (std::string & input);
63 
70  MADARA_Export std::string & lower (std::string & input);
71 
79  MADARA_Export std::string & dds_topicify (std::string & input);
80 
87  MADARA_Export std::string & strip_white_space (std::string & input);
88 
95  MADARA_Export std::string & strip_comments (std::string & input);
96 
104  MADARA_Export std::string & string_remove (std::string & input,
105  char unwanted);
106 
114  MADARA_Export std::string & strip_extra_white_space (
115  std::string & input);
116 
126  MADARA_Export std::string strip_prefix (
127  const std::string & input, const std::string & prefix);
128 
136  MADARA_Export int split_hostport_identifier (const std::string & key,
137  std::string & host, std::string & port);
138 
146  MADARA_Export int merge_hostport_identifier (std::string & key,
147  const std::string & host, const std::string & port);
148 
156  MADARA_Export int merge_hostport_identifier (std::string & key,
157  const std::string & host, unsigned short u_port);
158 
167  MADARA_Export void tokenizer (const std::string & input,
168  const ::std::vector< std::string> & splitters,
169  ::std::vector< std::string> & tokens,
170  ::std::vector< std::string> & pivot_list);
171 
180  MADARA_Export int bind_to_ephemeral_port (ACE_SOCK_Dgram & socket,
181  std::string & host, unsigned short & port,
182  bool increase_until_bound = true);
183 
189  MADARA_Export std::string file_to_string (const std::string & filename);
190 
196  MADARA_Export std::string extract_path (const std::string & name);
197 
203  MADARA_Export std::string extract_filename (const std::string & name);
204 
211  MADARA_Export std::string expand_envs (
212  const std::string & source);
213 
222  char * get_var (const std::string & source, size_t cur, size_t & end);
223 
230  MADARA_Export std::string clean_dir_name (const std::string & target);
231 
238  MADARA_Export uint64_t endian_swap (uint64_t value);
239 
246  MADARA_Export int64_t endian_swap (int64_t value);
247 
254  MADARA_Export uint32_t endian_swap (uint32_t value);
255 
262  MADARA_Export int32_t endian_swap (int32_t value);
263 
270  MADARA_Export uint16_t endian_swap (uint16_t value);
271 
278  MADARA_Export int16_t endian_swap (int16_t value);
279 
286  MADARA_Export double endian_swap (double value);
287 
297  MADARA_Export int read_file (const std::string & filename,
298  void *& buffer, size_t & size, bool add_zero_char = false);
299 
307  MADARA_Export ssize_t write_file (const std::string & filename,
308  void * buffer, size_t size);
309 
314  MADARA_Export int64_t get_time (void);
315 
323  MADARA_Export double rand_double (double floor = 0.0,
324  double ceiling = 1.0, bool set_seed_to_time = true);
325 
333  MADARA_Export int64_t rand_int (int64_t floor = 0,
334  int64_t ceiling = RAND_MAX, bool set_seed_to_time = true);
335 
341  MADARA_Export int64_t nearest_int (double input);
342 
349  MADARA_Export bool begins_with (const std::string & input,
350  const std::string & prefix);
351 
358  MADARA_Export bool ends_with (const std::string & input,
359  const std::string & ending);
360 
366  MADARA_Export double sleep (double sleep_time);
367 
373  MADARA_Export ACE_Time_Value sleep (const ACE_Time_Value & sleep_time);
374 
386  MADARA_Export bool approx_equal (
387  double value1, double value2, double epsilon = 0.0001);
388 
394  MADARA_Export bool file_exists (const std::string & filename);
395 
401  MADARA_Export unsigned int file_size (const std::string & filename);
402 
410  MADARA_Export bool wait_true (knowledge::KnowledgeBase & knowledge,
411  const std::string & variable,
412  const knowledge::WaitSettings & settings =
414 
415 
416 
424  MADARA_Export bool wait_false (knowledge::KnowledgeBase & knowledge,
425  const std::string & variable,
426  const knowledge::WaitSettings & settings =
431  template <typename T>
432  bool less_compare (const T & left, const T & right);
433 
437  template <typename T>
438  bool greater_compare (const T & left, const T & right);
439 
447  template <typename T>
448  void sift_down (T * input, int start, int end,
449  bool (*comparator) (const T & left, const T & right) =
450  greater_compare<T>);
451 
458  template <typename T>
459  void heapify (T * input, int size,
460  bool (*comparator) (const T & left, const T & right) =
461  greater_compare<T>);
462 
469  template <typename T>
470  void heap_sort (T * input, int size,
471  bool (*comparator) (const T & left, const T & right) =
472  greater_compare<T>);
473 
480  template <typename T>
481  T bitmask_add (T mask, T values);
482 
489  template <typename T>
490  bool bitmask_check (T mask, T values);
491 
498  template <typename T>
499  T bitmask_remove (T mask, T values);
500 
501  }
502 }
503 
504 #include "Utility.inl"
505 
506 #endif // _MADARA_UTILITY_H_
MADARA_Export int64_t rand_int(int64_t floor=0, int64_t ceiling=RAND_MAX, bool set_seed_to_time=true)
Returns a random integer between a floor and ceiling.
Definition: Utility.cpp:835
MADARA_Export int merge_hostport_identifier(std::string &key, const std::string &host, unsigned short u_port)
Merges a host and port into a host:port key.
Definition: Utility.cpp:386
MADARA_Export bool wait_false(knowledge::KnowledgeBase &knowledge, const std::string &variable, const knowledge::WaitSettings &settings=knowledge::WaitSettings())
Waits on a knowledge record to be false without needing KaRL language.
Definition: Utility.cpp:1016
bool bitmask_check(T mask, T values)
Returns true if mask contains values.
Definition: Utility.inl:46
MADARA_Export int bind_to_ephemeral_port(ACE_SOCK_Dgram &socket, std::string &host, unsigned short &port, bool increase_until_bound=true)
Binds to an ephemeral port.
Definition: Utility.cpp:397
MADARA_Export std::string get_version(void)
Gets the MADARA version number.
Definition: Utility.cpp:25
MADARA_Export int read_file(const std::string &filename, void *&buffer, size_t &size, bool add_zero_char=false)
Reads a file into a provided void pointer.
Definition: Utility.cpp:747
MADARA_Export int64_t get_time(void)
Returns a time of day in nanoseconds.
Definition: Utility.cpp:805
T bitmask_add(T mask, T values)
Adds values to a bit mask.
Definition: Utility.inl:37
MADARA_Export 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.cpp:92
MADARA_Export unsigned int file_size(const std::string &filename)
Returns the size of a file.
Definition: Utility.cpp:908
MADARA_Export bool wait_true(knowledge::KnowledgeBase &knowledge, const std::string &variable, const knowledge::WaitSettings &settings=knowledge::WaitSettings())
Waits on a knowledge record to be true without needing KaRL language.
Definition: Utility.cpp:922
MADARA_Export ACE_Time_Value sleep(const ACE_Time_Value &sleep_time)
Sleeps for a certain amount of time.
Definition: Utility.cpp:873
MADARA_Export std::string & lower(std::string &input)
Converts the string to lower.
Definition: Utility.cpp:127
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:77
MADARA_Export uint32_t get_uint_version(void)
Gets the MADARA version number.
Definition: Utility.cpp:35
MADARA_Export 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:347
MADARA_Export int64_t nearest_int(double input)
Rounds a double to the nearest integer.
Definition: Utility.cpp:843
MADARA_Export std::string & upper(std::string &input)
Converts the string to upper.
Definition: Utility.cpp:101
bool greater_compare(const T &left, const T &right)
Returns true if right < left.
Definition: Utility.inl:70
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:118
MADARA_Export std::string extract_path(const std::string &name)
Extracts the path of a filename.
Definition: Utility.cpp:483
MADARA_Export double rand_double(double floor=0.0, double ceiling=1.0, bool set_seed_to_time=true)
Returns a random double between floor and ceiling.
Definition: Utility.cpp:815
MADARA_Export ssize_t write_file(const std::string &filename, void *buffer, size_t size)
Writes a file with provided contents.
Definition: Utility.cpp:779
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
MADARA_Export std::string & dds_topicify(std::string &input)
Changes periods to underscores in compliance with OpenSplice needs.
Definition: Utility.cpp:112
MADARA_Export std::string & strip_white_space(std::string &input)
Strips all whitespace characters from a string.
Definition: Utility.cpp:181
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:106
static constexpr struct madara::knowledge::tags::string_t string
MADARA_Export double endian_swap(double value)
Converts a host format double precision into big endian.
Definition: Utility.cpp:728
MADARA_Export std::string & strip_extra_white_space(std::string &input)
Strips whitespace from front and end of string and also condenses multiple whitespace into a single s...
Definition: Utility.cpp:139
T bitmask_remove(T mask, T values)
Removes values from a bit mask.
Definition: Utility.inl:55
MADARA_Export std::string to_string_version(uint32_t version)
Converts a MADARA uint32_t version number to human-readable.
Definition: Utility.cpp:71
MADARA_Export std::string & string_remove(std::string &input, char unwanted)
Strips an unwanted character.
Definition: Utility.cpp:209
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:14
MADARA_Export std::string expand_envs(const std::string &source)
Expands environment variables referenced in the string.
Definition: Utility.cpp:524
Provides functions and classes for the distributed knowledge base.
MADARA_Export std::string file_to_string(const std::string &filename)
Reads a file into a string.
Definition: Utility.cpp:450
MADARA_Export bool file_exists(const std::string &filename)
Checks if a file exists.
Definition: Utility.cpp:894
bool less_compare(const T &left, const T &right)
Returns true if left < right.
Definition: Utility.inl:63
MADARA_Export size_t string_replace(std::string &source, const std::string &old_phrase, const std::string &new_phrase, bool replace_all=true)
Replaces an old phrase with a new phrase within a string.
Definition: Utility.cpp:232
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:7
MADARA_Export std::string extract_filename(const std::string &name)
Extracts the file name of an absolute or relative path.
Definition: Utility.cpp:503
Encapsulates settings for a wait statement.
Definition: WaitSettings.h:23
MADARA_Export void tokenizer(const std::string &input, const ::std::vector< std::string > &splitters,::std::vector< std::string > &tokens,::std::vector< std::string > &pivot_list)
Splits an input string into tokens.
Definition: Utility.cpp:291
MADARA_Export bool approx_equal(double value1, double value2, double epsilon=0.0001)
Checks two doubles for approximate equality.
Definition: Utility.cpp:887
char * get_var(const std::string &source, size_t cur, size_t &end)
Helper function for madara::utility::expand_envs which retrieves an environment variable.
Definition: Utility.cpp:545
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:21
MADARA_Export std::string & strip_comments(std::string &input)
Strips all comments (single-line and multi-line).
Definition: Utility.cpp:259
MADARA_Export std::string clean_dir_name(const std::string &target)
Substitutes the appropriate directory delimiter, which may help with portability between operating sy...
Definition: Utility.cpp:560