1 #ifndef _MADARA_UTILITY_INL_
2 #define _MADARA_UTILITY_INL_
9 #include "boost/crc.hpp"
15 #include "madara/Boost.h"
16 #include "boost/asio.hpp"
32 if (SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS))
35 if (priority > THREAD_PRIORITY_HIGHEST)
36 priority = THREAD_PRIORITY_HIGHEST;
38 if (SetThreadPriority(GetCurrentThread(), priority))
48 pthread_getschedparam(pthread_self(), &policy, &sch);
49 sch.sched_priority = priority;
50 if (0 == pthread_setschedparam(pthread_self(), SCHED_FIFO, &sch))
63 return input.substr(prefix.size());
69 for (std::string::iterator cur = input.begin(); cur != input.end(); ++cur)
78 for (std::string::iterator cur = input.begin(); cur != input.end(); ++cur)
91 for (std::string::iterator cur = input.begin(); cur != input.end(); ++cur)
99 static const auto detect_little_endian = []() {
102 static_assert(
sizeof(uint16_t) ==
sizeof(copy),
"uint16_t isn't 2 bytes!");
103 memcpy(copy, (
char*)&tester,
sizeof(copy));
107 static const bool ret = detect_little_endian();
112 const std::string delimiter,
bool strip_whitespace)
115 std::vector <std::string> splitters, tokens, pivot_list;
116 splitters.push_back (delimiter);
118 tokenizer (input, splitters, tokens, pivot_list);
120 if (strip_whitespace)
122 for (
size_t i = 0; i < tokens.size (); ++i)
139 value = ((value << 8) & 0xFF00FF00FF00FF00ULL) |
140 ((value >> 8) & 0x00FF00FF00FF00FFULL);
141 value = ((value << 16) & 0xFFFF0000FFFF0000ULL) |
142 ((value >> 16) & 0x0000FFFF0000FFFFULL);
143 return (value << 32) | (value >> 32);
158 value = ((value << 8) & 0xFF00FF00FF00FF00ULL) |
159 ((value >> 8) & 0x00FF00FF00FF00FFULL);
160 value = ((value << 16) & 0xFFFF0000FFFF0000ULL) |
161 ((value >> 16) & 0x0000FFFF0000FFFFULL);
162 return (value << 32) | ((value >> 32) & 0xFFFFFFFFULL);
176 value = ((value << 8) & 0xFF00FF00) | ((value >> 8) & 0xFF00FF);
177 return (value << 16) | (value >> 16);
192 value = ((value << 8) & 0xFF00FF00) | ((value >> 8) & 0xFF00FF);
193 return (value << 16) | ((value >> 16) & 0xFFFF);
208 return ((value << 8) & 0xFFFF) | (value >> 8);
223 return ((value << 8) & 0xFFFF) | (value >> 8);
238 memcpy(&value, &orig,
sizeof(value));
243 memcpy(&result, &value,
sizeof(result));
258 #ifndef MADARA_FEATURE_SIMTIME
261 return TimeValue(std::chrono::nanoseconds(SimTime::time()));
268 auto epoch = current_time.time_since_epoch();
269 return std::chrono::duration_cast<Duration>(epoch).count();
279 return std::chrono::duration_cast<Duration>(
285 return std::chrono::duration<double>(seconds);
301 return std::abs(value1 - value2) < epsilon;
306 if (FILE* file = fopen(filename.c_str(),
"r"))
321 if (_stat(path.c_str(), &info) != 0)
325 return (info.st_mode & _S_IFDIR) != 0;
328 if (stat(path.c_str(), &info) != 0)
332 return (info.st_mode & S_IFDIR) != 0;
344 int result = _mkdir(path.c_str());
347 int result = mkdir(path.c_str(), mode);
362 return 0 == _mkdir(path.c_str());
364 return 0 == mkdir(path.c_str(), mode);
376 std::size_t last = name.find_last_of(
"/\\");
378 if (last != std::string::npos)
379 return name.substr(0, last);
386 std::size_t last = name.find_last_of(
"/\\");
388 if (last == std::string::npos)
389 return name.substr(0);
392 return name.substr(last + 1);
397 unsigned int size = 0;
398 if (FILE* file = fopen(filename.c_str(),
"r"))
400 fseek(file, 0L, SEEK_END);
411 std::streampos current = input.tellg();
414 input.seekg(0, std::ios::end);
415 std::size_t size = input.tellg();
418 input.seekg(current, std::ios::beg);
425 boost::crc_32_type crc;
429 std::vector<char> block(max_block);
434 input.read(block.data(), max_block);
435 std::size_t bytes_read = input.gcount();
438 crc.process_bytes(block.data(), bytes_read);
442 return crc.checksum();
448 if (filename.find(
"../") != std::string::npos)
452 else if (filename.find(
"//") != std::string::npos)
456 else if (filename.find(
"~") != std::string::npos)
469 std::string str_crc = std::to_string((
unsigned long)crc);
471 std::string frag_file = filename +
".0" + frag_suffix;
480 ++i, frag_file = filename +
"." + std::to_string(i) + frag_suffix)
490 output.write((
char*)buffer, size);
491 delete[](
char*) buffer;
503 frag_file = filename +
".0" + frag_suffix;
507 ++i, frag_file = filename +
"." + std::to_string(i) + frag_suffix)
509 remove(frag_file.c_str());
518 if (delete_incomplete)
520 remove(filename.c_str());
529 size_t expected_size,
size_t fragment_size)
531 std::string str_crc = std::to_string((
unsigned long)crc);
533 std::string frag_file = filename +
".0" + frag_suffix;
537 size_t num_fragments = expected_size / fragment_size;
538 size_t actual_size = 0;
540 if (expected_size % fragment_size > 0)
549 for (
int i = 0; i < (int)num_fragments;
550 ++i, frag_file = filename +
"." + std::to_string(i) + frag_suffix)
562 output.write((
char*)buffer, size);
563 delete[](
char*) buffer;
573 const std::string& filename, uint32_t crc,
size_t expected_size,
574 int max_fragments,
size_t fragment_size)
576 std::vector<int64_t> result;
577 std::string str_crc = std::to_string((
unsigned long)crc);
579 std::string frag_file = filename +
".0" + frag_suffix;
583 size_t num_fragments = expected_size / fragment_size;
586 if (expected_size % fragment_size > 0)
592 "utility::get_file_missing_fragments: looking at file %s\n",
599 for (
int i = 0; i < (int)num_fragments;
600 ++i, frag_file = filename +
"." + std::to_string(i) + frag_suffix)
603 if (max_fragments >= 0 && result.size() >= (size_t)max_fragments)
616 "utility::get_file_missing_fragments: checking fragment %d\n", i);
621 output.write((
char*)buffer, size);
622 delete[](
char*) buffer;
627 "utility::get_file_missing_fragments: missing fragment %d\n", i);
629 result.push_back((int64_t)i);
642 if (prefix.length() <= input.length())
644 result = input.substr(0, prefix.length()) == prefix;
654 if (match.length() <= input.length())
657 input.substr(input.size() - match.length(), match.length()) == match;
666 return mask | values;
675 return (mask & values) > 0;
684 return mask & ~values;
701 bool (*comparator)(
const T& left,
const T& right))
704 for (
int child = root * 2 + 1; child <= end; child = root * 2 + 1)
709 if (comparator(input[child], input[swap]))
714 if (child <= end && comparator(input[child], input[swap]))
719 std::swap(input[root], input[swap]);
729 T* input,
int size,
bool (*comparator)(
const T& left,
const T& right))
732 for (
int start = (size - 2) / 2; start >= 0; --start)
734 sift_down(input, start, size - 1, comparator);
740 T* input,
int size,
bool (*comparator)(
const T& left,
const T& right))
742 heapify(input, size, comparator);
744 for (
int end = size - 1; end > 0; --end)
746 std::swap(input[end], input[0]);
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
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.
uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
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...
static const uint64_t nano_per
MADARA_EXPORT bool set_thread_priority(int priority=20)
Sets the thread priority in a FIFO scheme.
unsigned int file_size(const std::string &filename)
Returns the size of a file.
bool greater_compare(const T &left, const T &right)
Returns true if right < left.
static const uint64_t micro_per
std::string & lower(std::string &input)
Converts the string to lower.
T bitmask_add(T mask, T values)
Adds values to a bit mask.
bool file_exists(const std::string &filename)
Checks if a file exists.
static const uint64_t simtime_min_sleep
std::chrono::time_point< Clock > TimeValue
time point
MADARA_EXPORT bool ends_with(const std::string &input, const std::string &ending)
Check if input contains a pattern at the end.
std::chrono::nanoseconds Duration
default clock duration
T bitmask_remove(T mask, T values)
Removes values from a bit mask.
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.
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.
bool less_compare(const T &left, const T &right)
Returns true if left < right.
uint32_t file_crc(const std::string &filename, size_t max_block=1000000)
Returns the crc of a file.
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.
bool is_directory(const std::string &path)
Checks if a a path is a directory.
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.
TimeValue seconds_to_time(double seconds)
Returns seconds in double format as nanosecond since epoch.
bool recursive_mkdir(const std::string &path)
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.
MADARA_EXPORT std::string extract_filename(const std::string &name)
Extracts the file name of an absolute or relative path.
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.
std::string & upper(std::string &input)
Converts the string to upper.
bool bitmask_check(T mask, T values)
Returns true if mask contains values.
std::string & dds_topicify(std::string &input)
Changes periods to underscores in compliance with OpenSplice needs.
std::string strip_prefix(const std::string &input, const std::string &prefix)
Strips a prefix from an input string and returns the result.
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.
SecondsDuration seconds_to_seconds_duration(double seconds)
Returns seconds in double format as seconds duration.
int64_t seconds_to_nanoseconds(double seconds)
Returns seconds in nanoseconds.
int64_t get_time(void)
Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time inste...
bool approx_equal(double value1, double value2, double epsilon=0.0001)
Checks two doubles for approximate equality.
std::chrono::duration< double > SecondsDuration
default clock duration
static const uint64_t milli_per
MADARA_EXPORT std::string extract_path(const std::string &name)
Extracts the path of a filename.
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...
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
void heap_sort(T *input, int size, bool(*comparator)(const T &left, const T &right)=greater_compare< T >)
Sorts an array with heap sort.
int read_file(const std::string &filename, void *&buffer, size_t &size, bool add_zero_char)
Reads a file into a provided void pointer.
TimeValue add_seconds(const TimeValue &start, double seconds)
Returns an offset of a time by seconds in double format.
MADARA_EXPORT bool filename_has_redirect(const std::string &filename)
Checks the filename for abnormal redirects such as "..".
Duration seconds_to_duration(double seconds)
Returns seconds in double format as nanosecond duration.
Copyright(c) 2020 Galois.