25 #include "madara/Version.h"
27 return version +
std::string(
" compiled on ") + __DATE__ +
" at " + __TIME__;
32 unsigned char version_buffer[4] = {0, 0, 0, 0};
33 uint32_t* return_value = (uint32_t*)version_buffer;
34 unsigned int major, minor, revision;
35 char version_delimiter;
37 std::stringstream buffer;
40 buffer << str_version;
45 buffer >> version_delimiter;
50 buffer >> version_delimiter;
57 version_buffer[1] = (
unsigned char)major;
58 version_buffer[2] = (
unsigned char)minor;
59 version_buffer[3] = (
unsigned char)revision;
66 std::stringstream new_version;
67 unsigned char* version_ptr = (
unsigned char*)&version;
68 unsigned int major, minor, revision;
70 major = (
unsigned int)version_ptr[1];
71 minor = (
unsigned int)version_ptr[2];
72 revision = (
unsigned int)version_ptr[3];
78 new_version << revision;
80 return new_version.str();
87 std::string::iterator cur = input.begin();
90 for (std::string::iterator eval = cur; eval != input.end(); ++eval)
93 if (*eval !=
' ' && *eval !=
'\t' && *eval !=
'\n' && *eval !=
'\r')
109 if (cur != input.end())
112 if (*cur !=
' ' && *cur !=
'\t' && *cur !=
'\n' && *cur !=
'\r')
117 if (cur != input.end())
118 input.erase(cur, input.end());
126 std::string::iterator cur = input.begin();
128 for (std::string::iterator eval = cur; eval != input.end(); ++eval)
131 if (*eval !=
' ' && *eval !=
'\t' && *eval !=
'\n' && *eval !=
'\r')
139 if (cur != input.end())
140 input.erase(cur, input.end());
151 std::string::iterator cur = input.begin();
153 for (std::string::iterator eval = cur; eval != input.end(); ++eval)
156 if (*eval != unwanted)
164 if (cur != input.end())
165 input.erase(cur, input.end());
174 size_t replacements = 0;
176 if (old_phrase !=
"")
178 for (std::string::size_type i = source.find(old_phrase, 0);
179 i != source.npos; i = source.find(old_phrase, i))
181 source.replace(i, old_phrase.size(), new_phrase);
182 i += new_phrase.size();
196 std::stringstream source, dest;
198 std::vector<std::string> splitters;
206 while (std::getline(source, cur))
208 std::vector<std::string> tokens;
209 std::vector<std::string> pivots;
210 tokenizer(cur, splitters, tokens, pivots);
225 const ::std::vector<std::string>& splitters,
226 ::std::vector<std::string>& tokens, ::std::vector<std::string>& pivots)
228 std::string::size_type last = 0;
229 std::string::size_type cur = 0;
233 for (; cur < input.size(); ++cur)
235 for (std::string::size_type i = 0; i < splitters.size(); ++i)
238 if (splitters[i].size() > 0)
241 if (input[cur] == splitters[i][0])
243 std::string::size_type checker = cur;
244 std::string::size_type j = 1;
245 for (++checker; checker < input.size() && j < splitters[i].size() &&
246 input[checker] == splitters[i][j];
251 if (j == splitters[i].size())
254 pivots.push_back(input.substr(cur, j));
256 if (cur - last >= splitters[i].size() - 1)
257 tokens.push_back(input.substr(last, cur - last));
259 tokens.push_back(
"");
273 tokens.push_back(input.substr(last, cur - last));
282 std::string::size_type delim = key.rfind(
':');
283 delim = delim == key.npos ? key.rfind(
'@') : delim;
286 if (delim == key.npos)
295 host = key.substr(0, delim);
296 port = key.substr(delim + 1, key.size() - delim);
318 std::stringstream port_stream;
319 port_stream << u_port;
327 std::stringstream buffer;
329 std::ifstream file(filename.c_str());
334 std::getline(file, line);
341 while (std::getline(file, line))
342 buffer <<
"\n" << line;
348 "utility::file_to_string:"
349 " failed to open file: %s\n",
359 std::stringstream buffer;
361 for (
size_t i = 0; i < source.size(); ++i)
364 if (source[i] ==
'$' && i + 3 < source.size())
366 char* value =
get_var(source, i + 2, i);
376 MADARA_EXPORT
void strncpy_safe(
char * dst,
const char * src,
size_t dst_size)
378 strncpy(dst, src, dst_size);
379 dst[dst_size - 1] = 0;
385 for (end = cur; end < source.size(); ++end)
387 if (source[end] ==
')' || source[end] ==
'}')
389 return getenv(source.substr(cur, end - cur).c_str());
393 return getenv(source.substr(cur, source.size() - cur).c_str());
400 #define REPLACE_THIS '/'
401 #define REPLACE_WITH '\\'
403 #define REPLACE_THIS '\\'
404 #define REPLACE_WITH '/'
409 for (std::string::iterator i = target.begin(); i != target.end(); ++i)
432 file.seekg(0, file.end);
433 size = (size_t)file.tellg();
434 file.seekg(0, file.beg);
437 "utility::read_file:"
438 " reading %d bytes from %s\n",
439 (
int)size, filename.c_str());
446 buffer =
new unsigned char[size];
450 file.read((
char*)buffer, size);
453 "utility::read_file:"
454 " successfully read %d bytes from %s\n",
455 (
int)size, filename.c_str());
459 unsigned char* zeroed = (
unsigned char*)buffer;
460 zeroed[size - 1] = 0;
471 catch (
const std::exception& e)
474 "utility::read_file:"
497 if (file.write((
char*)buffer, size))
504 "utility::write_file:"
505 " wrote %d bytes to %s\n",
506 (
int)actual, filename.c_str());
508 catch (
const std::exception& e)
511 "utility::write_file:"
520 double rand_double(
double floor,
double ceiling,
bool set_seed_to_time)
523 if (set_seed_to_time)
529 double position_in_range = ((double)rand()) / ((
double)RAND_MAX);
532 return (position_in_range * (ceiling - floor)) + floor;
534 return (position_in_range * (floor - ceiling)) + ceiling;
537 int64_t
rand_int(int64_t floor, int64_t ceiling,
bool set_seed_to_time)
539 double random =
rand_double((
double)floor, (
double)ceiling, set_seed_to_time);
545 int change = input >= 0 ? 1 : -1;
546 int64_t left = (int64_t)input;
547 int64_t right = (int64_t)input + change;
549 if (input - left < -input + right)
558 period =
sleep(period);
560 return period.count();
565 #ifdef MADARA_FEATURE_SIMTIME
569 using TVal = std::chrono::time_point<Clock, SecondsDuration>;
572 TVal target = start + sleep_time;
577 #ifndef MADARA_FEATURE_SIMTIME
580 double rate = utility::SimTime::rate();
583 std::cerr << (target - current).count() <<
" " << rate <<
" " <<
584 actual_dur.count() << std::endl;
586 TVal actual_current = Clock::now();
587 TVal actual_target = actual_current + actual_dur;
588 TVal max_target = actual_current + max_sleep;
591 start.time_since_epoch().count() <<
" " <<
592 target.time_since_epoch().count() <<
" " <<
593 current.time_since_epoch().count() <<
" " <<
594 SimTime::time() <<
" " <<
595 actual_target.time_since_epoch().count() <<
" " <<
596 max_target.time_since_epoch().count() << std::endl;
598 if (actual_target < max_target)
609 return current - start;
614 #ifdef MADARA_FEATURE_SIMTIME
615 static const Duration max_sleep{10000000};
623 #ifndef MADARA_FEATURE_SIMTIME
627 std::cerr << (target - current).count() <<
" " << rate <<
" " <<
628 actual_dur.count() << std::endl;
634 TimeValue max_target = actual_current + max_sleep;
637 start.time_since_epoch().count() <<
" " <<
638 target.time_since_epoch().count() <<
" " <<
639 current.time_since_epoch().count() <<
" " <<
640 SimTime::time() <<
" " <<
641 actual_target.time_since_epoch().count() <<
" " <<
642 max_target.time_since_epoch().count() << std::endl;
644 if (actual_target < max_target)
655 return current - start;
679 "utility::wait_true:"
680 " variable returned %s\n",
684 while (!last_value.
is_true() &&
688 "utility::wait_true:"
689 " last value didn't result in success\n");
703 "utility::wait_true:"
707 last_value =
knowledge.get(ref, settings);
710 "utility::wait_true:"
711 " completed eval to get %s\n",
718 "utility::wait_true:"
719 " Evaluate did not succeed. Timeout occurred\n",
746 "utility::wait_false:"
747 " variable returned %s\n",
755 "utility::wait_false:"
756 " last value didn't result in success\n");
770 "utility::wait_false:"
777 "utility::wait_false:"
778 " completed eval to get %s\n",
785 "utility::wait_false:"
786 " Evaluate did not succeed. Timeout occurred\n",
799 size_t colon_pos = addr.find(
':');
800 if (colon_pos == std::string::npos || colon_pos >= addr.size() - 1)
805 int16_t port = std::atoi(addr.c_str() + colon_pos + 1);
806 addr.resize(colon_pos);
807 return {std::move(addr), port};
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
madara::knowledge::KnowledgeRecord KnowledgeRecord
This class provides a distributed knowledge base to users.
This class encapsulates an entry in a KnowledgeBase.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
bool is_true(void) const
Checks to see if the record is true.
Optimized reference to a variable within the knowledge base.
Enforces a periodic epoch.
bool is_done(void) const
Checks to see if max duration is finished.
void sleep_until_next(void)
Sleeps until the next epoch.
Provides functions and classes for the distributed knowledge base.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
Provides utility functions and classes for common tasks and needs.
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.
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...
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.
int64_t rand_int(int64_t floor, int64_t ceiling, bool set_seed_to_time)
Returns a random integer between a floor and ceiling.
std::chrono::time_point< Clock > TimeValue
time point
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
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.
double sleep(double sleep_time)
Sleeps for a certain amount of time.
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.
int64_t nearest_int(double input)
Rounds a double to the nearest integer.
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).
Duration sleep_until(uint64_t wake)
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.
std::chrono::duration< double > SecondsDuration
default clock duration
std::pair< std::string, uint16_t > parse_address(std::string addr)
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.
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.
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...
uint32_t get_uint_version(const std::string &str_version)
Converts a string version to a uint32.
Copyright(c) 2020 Galois.
std::string post_print_statement
Statement to print after evaluations.
std::string pre_print_statement
Statement to print before evaluations.
Encapsulates settings for a wait statement.
double max_wait_time
Maximum time to wait for an expression to become true (in seconds)
double poll_frequency
Frequency to poll an expression for truth (in seconds)