24 #include "madara/Version.h" 27 __DATE__ +
" at " + __TIME__;
35 unsigned char version_buffer[4] = {0,0,0,0};
36 uint32_t * return_value = (uint32_t *) version_buffer;
37 unsigned int major, minor, revision;
38 char version_delimiter;
40 std::stringstream buffer;
43 buffer << str_version;
48 buffer >> version_delimiter;
53 buffer >> version_delimiter;
60 version_buffer[1] = (
unsigned char)major;
61 version_buffer[2] = (
unsigned char)minor;
62 version_buffer[3] = (
unsigned char)revision;
70 std::stringstream new_version;
71 unsigned char * version_ptr = (
unsigned char *)&version;
72 unsigned int major, minor, revision;
74 major = (
unsigned int)version_ptr[1];
75 minor = (
unsigned int)version_ptr[2];
76 revision = (
unsigned int)version_ptr[3];
82 new_version << revision;
84 return new_version.str ();
92 std::string::iterator cur = input.begin ();
95 for (std::string::iterator eval = cur;
96 eval != input.end (); ++eval)
99 if (*eval !=
' ' && *eval !=
'\t' && *eval !=
'\n' && *eval !=
'\r')
115 if (cur != input.end ())
118 if (*cur !=
' ' && *cur !=
'\t' && *cur !=
'\n' && *cur !=
'\r')
123 if (cur != input.end ())
124 input.erase (cur, input.end ());
134 std::string::iterator cur = input.begin ();
136 for (std::string::iterator eval = cur;
137 eval != input.end (); ++eval)
140 if (*eval !=
' ' && *eval !=
'\t' && *eval !=
'\n' && *eval !=
'\r')
148 if (cur != input.end ())
149 input.erase (cur, input.end ());
162 std::string::iterator cur = input.begin ();
164 for (std::string::iterator eval = cur;
165 eval != input.end (); ++eval)
168 if (*eval != unwanted)
176 if (cur != input.end ())
177 input.erase (cur, input.end ());
189 size_t replacements = 0;
191 if (old_phrase !=
"")
193 for (std::string::size_type i = source.find (old_phrase, 0);
194 i != source.npos; i = source.find (old_phrase, i))
196 source.replace (i, old_phrase.size (), new_phrase);
197 i += new_phrase.size ();
212 std::stringstream source, dest;
214 std::vector <std::string> splitters;
215 splitters.resize (2);
222 while (std::getline (source, cur))
224 std::vector <std::string> tokens;
225 std::vector <std::string> pivots;
226 tokenizer (cur, splitters, tokens, pivots);
243 const ::std::vector< std::string> & splitters,
244 ::std::vector< std::string> & tokens,
245 ::std::vector< std::string> & pivots)
247 std::string::size_type last = 0;
248 std::string::size_type cur = 0;
252 for (; cur < input.size (); ++cur)
254 for (std::string::size_type i = 0; i < splitters.size (); ++i)
257 if (splitters[i].size () > 0)
260 if (input[cur] == splitters[i][0])
262 std::string::size_type checker = cur;
263 std::string::size_type j = 1;
265 checker < input.size () && j < splitters[i].size () &&
266 input[checker] == splitters[i][j];
270 if (j == splitters[i].size ())
273 pivots.push_back (input.substr (cur, j));
275 if (cur - last >= splitters[i].size () - 1)
276 tokens.push_back (input.substr (last, cur - last));
278 tokens.push_back (
"");
292 tokens.push_back (input.substr (last, cur - last));
302 std::string::size_type delim = key.rfind (
':');
303 delim = delim == key.npos ? key.rfind (
'@') : delim;
306 if (delim == key.npos)
315 host = key.substr (0, delim);
316 port = key.substr (delim + 1, key.size () - delim);
340 std::stringstream port_stream;
341 port_stream << u_port;
350 std::stringstream buffer;
352 std::ifstream file (filename.c_str ());
357 std::getline (file, line);
364 while (std::getline (file, line))
365 buffer <<
"\n" << line;
371 "utility::file_to_string:" \
372 " failed to open file: %s\n", filename.c_str());
375 return buffer.str ();
382 std::string::size_type start = 0;
383 for (std::string::size_type i = 0; i < name.size (); ++i)
386 if (name[i] ==
'/' || name[i] ==
'\\')
390 if (i != name.size () - 1)
396 return name.substr (0, start);
402 std::string::size_type start = 0;
403 for (std::string::size_type i = 0; i < name.size (); ++i)
406 if (name[i] ==
'/' || name[i] ==
'\\')
410 if (i != name.size () - 1)
416 return name.substr (start, name.size () - start);
423 std::stringstream buffer;
425 for (
size_t i = 0; i < source.size (); ++i)
428 if (source[i] ==
'$' && i + 3 < source.size ())
430 char * value =
get_var (source, i+2, i);
437 return buffer.str ();
443 size_t cur,
size_t & end)
445 for (end = cur; end < source.size (); ++end)
447 if (source[end] ==
')' || source[end] ==
'}')
449 return getenv (source.substr (cur, end - cur).c_str ());
453 return getenv (source.substr (cur,source.size () - cur).c_str ());
461 #define REPLACE_THIS '/' 462 #define REPLACE_WITH '\\' 464 #define REPLACE_THIS '\\' 465 #define REPLACE_WITH '/' 470 for (std::string::iterator i = target.begin (); i != target.end(); ++i)
481 void *& buffer,
size_t & size,
bool add_zero_char)
492 file.seekg (0, file.end);
493 size = (size_t) file.tellg ();
494 file.seekg (0, file.beg);
498 "utility::read_file:" 499 " reading %d bytes from %s\n",
500 (
int)size, filename.c_str ());
507 buffer =
new unsigned char [size];
511 file.read ((
char *)buffer, size);
515 "utility::read_file:" 516 " successfully read %d bytes from %s\n",
517 (
int)size, filename.c_str ());
521 unsigned char * zeroed = (
unsigned char *)buffer;
522 zeroed[size - 1] = 0;
532 catch (
const std::exception& e)
535 "utility::read_file:" 536 " exception: %s\n", e.what());
548 void * buffer,
size_t size)
558 if (file.write ((
char *)buffer, size))
565 "utility::write_file:" 566 " wrote %d bytes to %s\n", (
int)actual, filename.c_str ());
568 catch(
const std::exception& e)
571 "utility::write_file:" 572 " exception: %s\n", e.what());
583 if (set_seed_to_time)
589 double position_in_range = ((double)rand()) / ((
double)RAND_MAX);
592 return (position_in_range * (ceiling - floor)) + floor;
594 return (position_in_range * (floor - ceiling)) + ceiling;
599 bool set_seed_to_time)
601 double random =
rand_double ((
double)floor, (
double)ceiling, set_seed_to_time);
608 int change = input >= 0 ? 1 : -1;
609 int64_t left = (int64_t)input;
610 int64_t right = (int64_t)input + change;
612 if (input - left < -input + right)
622 period =
sleep (period);
624 return period.count ();
633 #ifdef MADARA_FEATURE_SIMTIME 635 double rate = SimTime::rate ();
637 dilated_time /= rate;
638 target += std::chrono::duration_cast <
Duration> (dilated_time);
640 target += std::chrono::duration_cast <
Duration> (sleep_time);
643 while (current < target)
645 std::this_thread::sleep_until(target);
649 return current - start;
671 "utility::wait_true:" \
672 " variable returned %s\n",
676 while (!last_value.
is_true () &&
680 "utility::wait_true:" \
681 " last value didn't result in success\n");
695 "utility::wait_true:" \
696 " waiting on %s\n", variable.c_str ());
698 last_value = knowledge.
get (ref, settings);
701 "utility::wait_true:" \
702 " completed eval to get %s\n",
703 last_value.to_string ().c_str ());
709 "utility::wait_true:" \
710 " Evaluate did not succeed. Timeout occurred\n",
739 "utility::wait_false:" \
740 " variable returned %s\n",
741 last_value.to_string ().c_str ());
744 while (last_value.is_true () &&
748 "utility::wait_false:" 749 " last value didn't result in success\n");
763 "utility::wait_false:" 764 " waiting on %s\n", variable.c_str ());
769 "utility::wait_false:" 770 " completed eval to get %s\n",
771 last_value.to_string ().c_str ());
777 "utility::wait_false:" 778 " Evaluate did not succeed. Timeout occurred\n",
779 last_value.to_string ().c_str ());
786 return last_value.is_true ();
791 size_t colon_pos = addr.find(
':');
792 if (colon_pos == std::string::npos || colon_pos >= addr.size() - 1) {
796 int16_t port = std::atoi(addr.c_str() + colon_pos + 1);
797 addr.resize(colon_pos);
798 return {std::move(addr), port};
This class encapsulates an entry in a KnowledgeBase.
bool is_true(void) const
Checks to see if the record is true.
double max_wait_time
Maximum time to wait for an expression to become true (in seconds)
std::string extract_filename(const std::string &name)
Extracts the file name of an absolute or relative path.
std::chrono::duration< double > SecondsDuration
default clock duration
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.
std::pair< std::string, uint16_t > parse_address(std::string addr)
madara::knowledge::KnowledgeRecord KnowledgeRecord
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 & strip_comments(std::string &input)
Strips all comments (single-line and multi-line).
int64_t get_time(void)
Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time inste...
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 pre_print_statement
Statement to print before evaluations.
SecondsDuration seconds_to_seconds_duration(double seconds)
Returns seconds in double format as seconds duration.
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.
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
std::string extract_path(const std::string &name)
Extracts the path of a filename.
std::chrono::time_point< Clock > TimeValue
time point
Optimized reference to a variable within the knowledge base.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Retrieves a knowledge value.
std::string post_print_statement
Statement to print after evaluations.
double sleep(double sleep_time)
Sleeps for a certain amount of time.
bool is_done(void) const
Checks to see if max duration is finished.
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...
Enforces a periodic epoch.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
std::string to_string_version(uint32_t version)
Converts a MADARA uint32_t version number to human-readable.
This class provides a distributed knowledge base to users.
int64_t nearest_int(double input)
Rounds a double to the nearest integer.
std::string file_to_string(const std::string &filename)
Reads a file into a string.
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.
uint32_t get_uint_version(void)
Gets the MADARA version number.
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.
double poll_frequency
Frequency to poll an expression for truth (in seconds)
std::string & strip_white_space(std::string &input)
Strip all whitespace.
std::string clean_dir_name(const std::string &source)
Substitutes the appropriate directory delimiter, which may help with portability between operating sy...
double rand_double(double floor, double ceiling, bool set_seed_to_time)
Returns a random double between floor and ceiling.
std::string expand_envs(const std::string &source)
Expand any environment variables in a string.
int64_t rand_int(int64_t floor, int64_t ceiling, bool set_seed_to_time)
Returns a random integer between a floor and ceiling.
Provides utility functions and classes for common tasks and needs.
Provides functions and classes for the distributed knowledge base.
std::string & string_remove(std::string &input, char unwanted)
Strips an unwanted character.
Copyright (c) 2015 Carnegie Mellon University.
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.
Encapsulates settings for a wait statement.
std::string get_version(void)
Gets the MADARA version number.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
ssize_t write_file(const std::string &filename, void *buffer, size_t size)
Writes a file with provided contents.
void sleep_until_next(void)
Sleeps until the next epoch.
char * get_var(const std::string &source, size_t cur, size_t &end)
grab an environment variable value (
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
std::chrono::nanoseconds Duration
default clock duration
void print(unsigned int level=0) const
Prints all knowledge variables and values in the context.
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.
void wait_for_change(void)
Wait for a change to happen to the context (e.g., from transports)