Skip to content

Commit

Permalink
* Added initial batch mode (#112)
Browse files Browse the repository at this point in the history
* Added two new utility functions to aid with time related activities and splitting file contents into std::vector
  • Loading branch information
jredmondson committed Oct 4, 2018
1 parent 91a0bfa commit beba467
Show file tree
Hide file tree
Showing 3 changed files with 420 additions and 159 deletions.
16 changes: 16 additions & 0 deletions include/madara/utility/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ MADARA_EXPORT int read_file(const std::string& filename, void*& buffer,
MADARA_EXPORT ssize_t write_file(
const std::string& filename, void* buffer, size_t size);

/**
* Splits a string into a vector of strings by delimiter.
* @see utility::tokenizer, which does the tokenization.
* @param input the text to parse
* @param delimiter the delimiter to use as a splitter
* @param strip_whitespace if true, remove whitespace around tokens
**/
std::vector <std::string> string_to_vector (const std::string & input,
const std::string delimiter = "\n", bool strip_whitespace = true);

/**
* Returns a time of day in nanoseconds
* If simtime feature is enabled, this may be simulation time instead of
Expand Down Expand Up @@ -389,6 +399,12 @@ TimeValue add_seconds(const TimeValue& start, double seconds);
**/
Duration seconds_to_duration(double seconds);

/**
* Returns seconds in nanoseconds
* @return nanoseconds that can be added to another timestamp
**/
int64_t seconds_to_nanoseconds(double seconds);

/**
* Returns seconds in double format as seconds duration
* @return duration for usage with TimeValues
Expand Down
26 changes: 26 additions & 0 deletions include/madara/utility/Utility.inl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ inline bool endian_is_little()
return ret;
}

inline std::vector <std::string> string_to_vector (const std::string & input,
const std::string delimiter, bool strip_whitespace)
{
// split by the delimiter
std::vector <std::string> splitters, tokens, pivot_list;
splitters.push_back (delimiter);

tokenizer (input, splitters, tokens, pivot_list);

if (strip_whitespace)
{
for (size_t i = 0; i < tokens.size (); ++i)
{
strip_extra_white_space (tokens[i]);
}
}

return tokens;
}

/**
* Converts a host format uint64_t into big endian
**/
Expand Down Expand Up @@ -271,6 +291,12 @@ inline TimeValue seconds_to_time(double seconds)
return TimeValue(seconds_to_duration(seconds));
}


inline int64_t seconds_to_nanoseconds(double seconds)
{
return seconds_to_duration (seconds).count ();
}

inline bool approx_equal(double value1, double value2, double epsilon)
{
return std::abs(value1 - value2) < epsilon;
Expand Down

0 comments on commit beba467

Please sign in to comment.