dodo
0.0.1
A C++ library to create containerized Linux services
|
Go to the documentation of this file.
23 #ifndef common_util_hpp
24 #define common_util_hpp
33 #include "buildenv.hpp"
36 #include <yaml-cpp/yaml.h>
37 #include <openssl/ssl.h>
38 #include <openssl/err.h>
42 void dumpBinaryData( std::ostream &out,
const std::string &s,
size_t width );
61 stop_ = std::chrono::high_resolution_clock::now() - std::chrono::hours(24);
62 start_ = std::chrono::high_resolution_clock::now();
70 stop_ = std::chrono::high_resolution_clock::now() - std::chrono::hours(24);
71 start_ = std::chrono::high_resolution_clock::now();
94 std::chrono::duration<double> diff;
96 diff = std::chrono::high_resolution_clock::now() -
start_;
105 std::chrono::time_point<std::chrono::high_resolution_clock>
start_;
107 std::chrono::time_point<std::chrono::high_resolution_clock>
stop_;
118 bool fileReadInt(
const std::string &file,
int &i );
127 return (
double)t2.tv_sec - (double)t1.tv_sec + ( (
double)t2.tv_usec - (double)t1.tv_usec ) / 1.0E6;
136 inline std::vector<std::string>
split(
const std::string &src,
char delimiter =
' ' ) {
137 std::vector<std::string> result;
138 std::istringstream is(src);
140 while ( getline( is, s, delimiter ) ) {
141 result.push_back( s );
157 inline std::vector<std::string>
escapedSplit(
const std::string &src, std::set<char> escape,
char delimiter =
' ' ) {
158 enum State { stInit, stEscape, stInput };
159 std::vector<std::string> result;
160 std::stringstream tmp;
161 State state = stInit;
162 for (
auto c : src ) {
163 if ( escape.find(c) != escape.end() ) {
165 }
else if ( c == delimiter ) {
166 if ( state != stEscape ) {
167 result.push_back( tmp.str() );
178 if ( tmp.str().length() > 0 ) result.push_back( tmp.str() );
219 std::vector<std::string>
fileReadStrings(
const std::string &filename );
228 std::vector<std::string>
fileReadStrings(
const std::string &filename,
const std::regex& exp );
279 std::string
escapeJSON(
const std::string &s );
289 template <
typename T> T
YAML_read_key(
const YAML::Node &node,
const std::string& key );
300 const std::string& key,
301 const T& default_value );
std::vector< std::string > split(const std::string &src, char delimiter=' ')
Split a string into substrings.
std::string escapeJSON(const std::string &s)
Escape a JSOn string.
bool fileReadInt(const std::string &file, int &i)
Read from a file, expecting it to contain a (signed) int.
std::string fileReadString(const std::string &filename)
Read the file as a single string.
std::vector< std::string > escapedSplit(const std::string &src, std::set< char > escape, char delimiter=' ')
Split a string into substrings by delimiter - unless the delimiter is escaped.
bool directoryExists(const std::string &path)
Return true when the directory exists.
std::chrono::time_point< std::chrono::high_resolution_clock > start_
Start time.
size_t writeSSLErrors(std::ostream &out, char terminator)
Write OpenSSL errors occurred in this thread to ostream, and clear their error state.
void start()
Start the stopwatch.
StopWatch()
Construct a StopWatch.
std::chrono::time_point< std::chrono::high_resolution_clock > stop_
Stop time.
void dumpBinaryData(std::ostream &out, const std::string &s, size_t width)
Dump binary data to a std::ostream.
double stop()
Stop the stopwatch.
bool availableFileSpace(const std::string &path, size_t &avail)
Return true when the free space could be determined, and set in avail.
std::string bio2String(BIO *bio)
Convert the data contents of an OpenSSL BIO to a std::string.
T YAML_read_key_default(const YAML::Node &node, const std::string &key, const T &default_value)
Template function to check existence and read YAML values of arbitrary type.
size_t getFileSize(const std::string &path)
Return the size of the file.
std::string getSSLErrors(char terminator)
Get all OpenSSL errors as a single string, and clear their error state.
double getSecondDiff(struct timeval &t1, struct timeval &t2)
Return difference in seconds as a double.
Common and utility interfaces.
T YAML_read_key(const YAML::Node &node, const std::string &key)
Template function to check existence and read YAML values of arbitrary type.
double getElapsedSeconds() const
Return the number of seconds between.
std::vector< std::string > fileReadStrings(const std::string &filename)
Read the file as vector of strings.
std::string formatDateTimeUTC(const struct timeval &tv)
Return a datetime string in UTC (2020-07-01T20:14:36.442929Z)
bool fileReadAccess(const std::string &path)
Return true when the file exists and the calling user has read access.
bool directoryWritable(const std::string &path)
Return true when the directory exists and is writable to the caller.
double restart()
Stop the stopwatch, start the Stopwatch again and return the elapsed time since previous start.