/** * Author......: See docs/credits.txt * License.....: MIT */ #include "common.h" #include "types.h" #include "shared.h" bool is_power_of_2 (const u32 v) { return (v && !(v & (v - 1))); } u32 get_random_num (const u32 min, const u32 max) { if (min == max) return (min); return ((rand () % (max - min)) + min); } u32 mydivc32 (const u32 dividend, const u32 divisor) { u32 quotient = dividend / divisor; if (dividend % divisor) quotient++; return quotient; } u64 mydivc64 (const u64 dividend, const u64 divisor) { u64 quotient = dividend / divisor; if (dividend % divisor) quotient++; return quotient; } void naive_replace (char *s, const u8 key_char, const u8 replace_char) { const size_t len = strlen (s); for (size_t in = 0; in < len; in++) { const u8 c = s[in]; if (c == key_char) { s[in] = replace_char; } } } void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_char) { char s_escaped[1024] = { 0 }; size_t s_escaped_max = sizeof (s_escaped); const size_t len = strlen (s); for (size_t in = 0, out = 0; in < len; in++, out++) { const u8 c = s[in]; if (c == key_char) { s_escaped[out] = escape_char; out++; } if (out == s_escaped_max - 2) break; s_escaped[out] = c; } strncpy (s, s_escaped, s_max - 1); } void hc_sleep_ms (const int msec) { #if defined (_WIN) Sleep (msec); #else usleep (msec * 1000); #endif } void hc_sleep (const int sec) { #if defined (_WIN) Sleep (sec * 1000); #else sleep (sec); #endif }