54 ::clock_gettime(CLOCK_MONOTONIC, &t);
59 struct timespec t { 0, 0 };
60 ::clock_gettime(CLOCK_REALTIME, &t);
65 struct timespec t { 0, 0 };
66 ::clock_gettime(CLOCK_MONOTONIC, &t);
67 return static_cast<uint64_t
>( t.tv_sec ) *
MilliPerOne +
72 struct timespec t { 0, 0 };
73 ::clock_gettime(CLOCK_REALTIME, &t);
74 return static_cast<uint64_t
>( t.tv_sec );
82 std::time_t t0 =
static_cast<std::time_t
>(
tv_sec);
84 if(
nullptr == ::gmtime_r( &t0, &tm_0 ) ) {
85 return "1970-01-01T00:00:00.0Z";
92 size_t p = ::strftime(b,
sizeof(b),
"%Y-%m-%dT%H:%M:%S", &tm_0);
93 if( 0 < p && p <
sizeof(b)-1 ) {
94 const size_t remaining =
sizeof(b) - p;
96 ::snprintf(b+p, remaining,
".%09" PRIi64
"Z",
tv_nsec);
98 ::snprintf(b+p, remaining,
"Z");
101 return std::string(b);
108 ts.tv_sec =
static_cast<decltype(ts.tv_sec)
>(td_ms/
MilliPerOne);
109 ts.tv_nsec = td_ns_0;
112 res = ::clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts);
113 }
while( ignore_irq && EINTR == res );
117 struct timespec ts = relative_time.to_timespec();
120 res = ::clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts);
121 }
while( ignore_irq && EINTR == res );
130 struct timespec ts = absolute_time.to_timespec();
133 res = ::clock_nanosleep(monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME,
134 TIMER_ABSTIME, &ts, &ts);
135 }
while( ignore_irq && EINTR == res );
144 return sleep_until( now + relative_time, monotonic, ignore_irq );
151 bool overflow =
false;
156 return sleep_until( atime, monotonic, ignore_irq );
160#if defined(__linux__) && defined(__GLIBC__)
169 pthread_mutex_t *__restrict __mutex,
171 const struct timespec *__restrict __abstime)
172 __nonnull ((1, 2, 4)) __attribute__((weak));
174 static
bool __jau__has_pthread_cond_clockwait() noexcept {
176 ::fprintf(stderr,
"INFO: jau::has_pthread_cond_clockwait: %d\n", r);
180 static bool r = __jau__has_pthread_cond_clockwait();
186 pthread_mutex_t * __mutex,
188 const struct timespec * __abstime) {
203 return std::cv_status::no_timeout;
206 struct timespec ts = absolute_time.to_timespec();
210 monotonic ? CLOCK_MONOTONIC : CLOCK_REALTIME, &ts);
212 ::pthread_cond_timedwait(cv.native_handle(), lock.mutex()->native_handle(), &ts);
216 return now < absolute_time ? std::cv_status::no_timeout : std::cv_status::timeout;
219std::cv_status
jau::wait_for(std::condition_variable& cv, std::unique_lock<std::mutex>& lock,
const fraction_timespec& relative_time,
const bool monotonic)
noexcept {
221 return std::cv_status::no_timeout;
224 return wait_until(cv, lock, now + relative_time, monotonic);
227std::cv_status
jau::wait_for(std::condition_variable& cv, std::unique_lock<std::mutex>& lock,
const fraction_i64& relative_time,
const bool monotonic)
noexcept {
229 return std::cv_status::no_timeout;
231 bool overflow =
false;
234 return std::cv_status::timeout;
236 return wait_until(cv, lock, atime, monotonic);
241: msg_( std::move( type.append(
" @ ").append(file).append(
":").append(
std::to_string(line)).append(
": ").append(m) ) ),
245 what_.append(
"\nNative backtrace:\n");
246 what_.append(backtrace_);
251 const size_t cstr_len = ::strnlen(
reinterpret_cast<const char*
>(buffer), cstr_max_len);
252 return std::string(
reinterpret_cast<const char*
>(buffer), cstr_len);
256 s.erase(s.begin(),
std::find_if(s.begin(), s.end(), [](
int ch) {
257 return !std::isspace(ch);
260 return !std::isspace(ch);
270std::vector<std::string>
jau::split_string(
const std::string& str,
const std::string& separator)
noexcept {
271 std::vector<std::string> res;
273 while( p0 != std::string::npos && p0 < str.size() ) {
274 size_t p1 = str.find(separator, p0);
275 res.push_back(str.substr(p0, p1));
276 if( p1 != std::string::npos ) {
277 p1 += separator.length();
285 std::transform(s.begin(), s.end(), s.begin(),
286 [](
unsigned char c){ return std::tolower(c); });
299 if( uuid16_le_octet_index > 14 ) {
300 std::string msg(
"uuid16_le_octet_index ");
302 msg.append(
", not within [0..14]");
322 offset = 15 - 1 - uuid16_le_octet_index;
324 offset = uuid16_le_octet_index;
334 if( uuid32_le_octet_index > 12 ) {
335 std::string msg(
"uuid32_le_octet_index ");
337 msg.append(
", not within [0..12]");
357 offset = 15 - 3 - uuid32_le_octet_index;
359 offset = uuid32_le_octet_index;
371 const size_t bsz = 1024;
375 nchars = vsnprintf(&str[0], bsz, format, ap);
383 const size_t bsz = std::min<size_t>(nchars+1, str.max_size()+1);
386 nchars = vsnprintf(&str[0], bsz, format, ap);
394 va_start (args, format);
402 if(
'0' <= c && c <=
'9') {
405 if(
'A' <= c && c <=
'F') {
408 if(
'a' <= c && c <=
'f') {
414size_t jau::hexStringBytes(std::vector<uint8_t>& out,
const std::string& hexstr,
const bool lsbFirst,
const bool checkLeading0x)
noexcept {
417size_t jau::hexStringBytes(std::vector<uint8_t>& out,
const uint8_t hexstr[],
const size_t hexstr_len,
const bool lsbFirst,
const bool checkLeading0x)
noexcept {
419 if( checkLeading0x && hexstr_len >= 2 && hexstr[0] ==
'0' && hexstr[1] ==
'x' ) {
430 const size_t hexlen_in = hexstr_len - offset;
431 const size_t bsize = hexlen_in / 2;
436 if( 0 < hexlen_in % 2 ) {
438 const size_t idx = ( lsb*i + msb*(bsize-1-i) ) * 2;
441 out.push_back(
static_cast<uint8_t
>( l ) );
448 for (; i < bsize; ++i) {
449 const size_t idx = ( lsb*i + msb*(bsize-1-i) ) * 2;
452 if( 0 <= h && 0 <= l ) {
453 out.push_back(
static_cast<uint8_t
>( (h << 4) + l ) );
467 const bool lsbFirst,
const bool lowerCase)
noexcept
472 if(
nullptr == data ) {
478 const uint8_t *
const bytes =
static_cast<const uint8_t*
>(data);
481 str.reserve(length * 2 +1);
482 for (
nsize_t j = 0; j < length; j++) {
483 const int v = bytes[offset+j] & 0xFF;
484 str.push_back(hex_array[v >> 4]);
485 str.push_back(hex_array[v & 0x0F]);
489 str.reserve(2 + length * 2 +1);
495 const int v = bytes[offset+j] & 0xFF;
496 str.push_back(hex_array[v >> 4]);
497 str.push_back(hex_array[v & 0x0F]);
503std::string&
jau::byteHexString(std::string& dest,
const uint8_t value,
const bool lowerCase)
noexcept
507 if( 2 > dest.capacity() - dest.size() ) {
508 dest.reserve(dest.size()+2);
510 const int v = value & 0xFF;
511 dest.push_back(hex_array[v >> 4]);
512 dest.push_back(hex_array[v & 0x0F]);
549bool jau::to_integer(
long long & result,
const char * str,
size_t str_len,
const char limiter,
const char *limiter_pos) {
550 static constexpr const bool _debug =
false;
551 char *endptr =
nullptr;
552 if(
nullptr == limiter_pos ) {
553 limiter_pos = str + str_len;
556 const long long num = std::strtoll(str, &endptr, 10);
559 if constexpr ( _debug ) {
560 INFO_PRINT(
"Value under- or overflow occurred, value %lld in: '%s', errno %d %s", num, str, errno, strerror(errno));
564 if(
nullptr == endptr || endptr == str ) {
566 if constexpr ( _debug ) {
567 INFO_PRINT(
"Value no digits consumed @ idx %d, %p == start, in: '%s'", endptr-str, endptr, str);
571 if( endptr < limiter_pos ) {
572 while( endptr < limiter_pos && ::isspace(*endptr) ) {
576 if( *endptr != limiter || endptr != limiter_pos ) {
578 if constexpr ( _debug ) {
579 INFO_PRINT(
"Value end not '%c' @ idx %d, %p != %p, in: %p '%s' len %zd", limiter, endptr-str, endptr, limiter_pos, str, str, str_len);
587bool jau::to_integer(
long long & result,
const std::string& str,
const char limiter,
const char *limiter_pos) {
588 return to_integer(result, str.c_str(), str.size(), limiter, limiter_pos);
592 static constexpr const bool _debug =
false;
593 const char * str =
const_cast<const char*
>(value.c_str());
594 const size_t str_len = value.length();
595 const char *divptr =
nullptr;
597 divptr = std::strstr(str,
"/");
598 if(
nullptr == divptr ) {
599 if constexpr ( _debug ) {
606 if( !
to_integer(num, str, str_len,
'/', divptr) ) {
611 if( !
to_integer(denom, divptr+1, str_len-(divptr-str)-1,
'\0', str + str_len) ) {
616 if( ! ( min_allowed <= temp && temp <= max_allowed ) ) {
618 if constexpr ( _debug ) {
619 INFO_PRINT(
"Numerator out of range, not %s <= %s <= %s, in: '%s'", min_allowed.to_string().c_str(), temp.
to_string().c_str(), max_allowed.to_string().c_str(), str);
static const char * HEX_ARRAY_BIG
static snsize_t hexCharByte_(const uint8_t c)
static constexpr const uint64_t MilliPerOne
static constexpr const uint64_t NanoPerMilli
static int pthread_cond_clockwait(pthread_cond_t *__cond, pthread_mutex_t *__mutex, os_clockid_t __clock_id, const struct timespec *__abstime)
static bool jau_has_pthread_cond_clockwait() noexcept
static const char * HEX_ARRAY_LOW
constexpr const uint64_t NanoPerOne
ExceptionBase(std::string type, std::string const &m, const char *file, int line) noexcept
std::string to_string(const bool show_double=false) const noexcept
Returns a string representation of this fraction.
constexpr InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
Like std::find_if() of 'algorithm'.
constexpr bool is_big_endian() noexcept
Evaluates true if platform is running in big endian mode, i.e.
constexpr bool is_defined_endian(const endian_t &v) noexcept
Evaluates true if the given endian is defined, i.e.
constexpr bool is_little_or_big_endian() noexcept
Evaluates true if platform is running in little or big endian mode, i.e.
endian_t
Endian identifier, indicating endianess of all scalar types.
lb_endian_t
Simplified reduced endian type only covering little- and big-endian.
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
const uint8_t * cast_char_ptr_to_uint8(const char *s) noexcept
@ pdp
Identifier for DEC PDP-11, aka ENDIAN_LITTLE_WORD.
@ undefined
Undetermined endian.
@ native
Identifier for native platform type, one of the above.
@ little
Identifier for little endian.
@ honeywell
Identifier for Honeywell 316, aka ENDIAN_BIG_WORD.
@ big
Identifier for big endian.
@ little
Identifier for little endian, equivalent to endian::little.
@ big
Identifier for big endian, equivalent to endian::big.
std::string to_string(const alphabet &v) noexcept
bool to_fraction_i64(fraction_i64 &result, const std::string &value, const fraction_i64 &min_allowed, const fraction_i64 &max_allowed) noexcept
Stores the fraction_i64 value of the given string value in format <num>/<denom>, which may contain wh...
fraction_timespec getWallClockTime() noexcept
Returns current wall-clock real-time since Unix Epoch 00:00:00 UTC on 1970-01-01.
fraction_timespec getMonotonicTime() noexcept
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
target_type
func::target_type identifier for the target function delegate_t<R, A...> object, exposed by jau::func...
@ null
Denotes a func::null_target_t.
@ lambda
Denotes a func::lambda_target_t.
@ capval
Denotes a func::capval_target_t.
@ capref
Denotes a func::capref_target_t.
@ member
Denotes a func::member_target_t.
@ free
Denotes a func::free_target_t.
@ std
Denotes a func::std_target_t.
@ ylambda
Denotes a func::ylambda_target_t.
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
uint128dp_t merge_uint128(uint16_t const uuid16, uint128dp_t const &base_uuid, nsize_t const uuid16_le_octet_index)
Merge the given 'uuid16' into a 'base_uuid' copy at the given little endian 'uuid16_le_octet_index' p...
int_fast32_t snsize_t
Natural 'ssize_t' alternative using int_fast32_t as its natural sized type.
math_error_t
Error types as specified by C++ Math Error Handling
std::string to_string(const math_error_t v) noexcept
Returns std::string representation of math_error_t.
@ underflow
See FE_UNDERFLOW.
@ overflow
See FE_OVERFLOW.
@ div_by_zero
See FE_DIVBYZERO.
std::string & toLowerInPlace(std::string &s) noexcept
std::string format_string(const char *format,...) noexcept
Returns a string according to printf() formatting rules and variable number of arguments following th...
std::string trim(const std::string &s) noexcept
trim copy
std::string bytesHexString(const void *data, const nsize_t offset, const nsize_t length, const bool lsbFirst, const bool lowerCase=true) noexcept
Produce a hexadecimal string representation of the given byte values.
void trimInPlace(std::string &s) noexcept
trim in place
std::string get_string(const uint8_t *buffer, nsize_t const buffer_len, nsize_t const max_len) noexcept
Returns a C++ String taken from buffer with maximum length of min(max_len, max_len).
std::string & byteHexString(std::string &dest, const uint8_t value, const bool lowerCase) noexcept
Produce a hexadecimal string representation of the given byte value.
std::string vformat_string(const char *format, va_list ap) noexcept
Returns a string according to vprintf() formatting rules using va_list instead of a variable number o...
bool to_integer(long long &result, const std::string &str, const char limiter='\0', const char *limiter_pos=nullptr)
std::vector< std::string > split_string(const std::string &str, const std::string &separator) noexcept
Split given string str at separator into the resulting std::vector excluding the separator sequence .
std::string toLower(const std::string &s) noexcept
size_t hexStringBytes(std::vector< uint8_t > &out, const std::string &hexstr, const bool lsbFirst, const bool checkLeading0x) noexcept
Converts a given hexadecimal string representation into a byte vector.
constexpr const jau::fraction_timespec zero(0, 0)
jau::fraction_timespec zero is { 0, 0 }
constexpr const jau::fraction_i64 zero(0l, 1lu)
zero is 0/1
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
std::string get_backtrace(const bool skip_anon_frames, const jau::snsize_t max_frames=-1, const jau::snsize_t skip_frames=1) noexcept
Returns a de-mangled backtrace string separated by newline excluding this function.
std::cv_status wait_for(std::condition_variable &cv, std::unique_lock< std::mutex > &lock, const fraction_timespec &relative_time, const bool monotonic=true) noexcept
wait_for causes the current thread to block until the condition variable is notified,...
uint64_t getWallClockSeconds() noexcept
Returns current wall-clock system time of day in seconds since Unix Epoch 00:00:00 UTC on 1 January 1...
void INFO_PRINT(const char *format,...) noexcept
Use for unconditional informal messages, prefix '[elapsed_time] Info: '.
std::cv_status wait_until(std::condition_variable &cv, std::unique_lock< std::mutex > &lock, const fraction_timespec &absolute_time, const bool monotonic=true) noexcept
wait_until causes the current thread to block until the condition variable is notified,...
bool sleep_until(const fraction_timespec &absolute_time, const bool monotonic=true, const bool ignore_irq=true) noexcept
sleep_until causes the current thread to block until the specific time is reached.
bool sleep(const fraction_timespec &relative_time, const bool ignore_irq=true) noexcept
sleep using high precision monotonic timer, useful for one-shot delays (only).
bool milli_sleep(uint64_t td_ms, const bool ignore_irq=true) noexcept
millisecond sleep using high precision monotonic timer, useful for one-shot delays (only).
uint64_t getCurrentMilliseconds() noexcept
Returns current monotonic time in milliseconds.
bool sleep_for(const fraction_timespec &relative_time, const bool monotonic=true, const bool ignore_irq=true) noexcept
sleep_for causes the current thread to block until a specific amount of time has passed.
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...
int64_t tv_nsec
Nanoseconds component with its absolute value in range [0..1'000'000'000[ or [0..1'000'000'000),...
int64_t tv_sec
Seconds component, with its absolute value in range [0..inf[ or [0..inf), sharing same sign with tv_n...
std::string to_iso8601_string() const noexcept
Convenience string conversion interpreted since Unix Epoch in UTC to ISO 8601 YYYY-mm-ddTHH:MM:SS....
Support aligned memory transfer from and to potentially unaligned memory.
A 128-bit packed uint8_t data array.