misc.c
| 1 | #include <cutils/misc.h> |
| 2 | |
| 3 | void sleep_ms(unsigned int milliseconds) |
| 4 | { |
| 5 | #ifdef __unix__ |
| 6 | usleep(milliseconds * 1000); |
| 7 | #endif |
| 8 | #ifdef _WIN32 |
| 9 | Sleep(milliseconds); |
| 10 | #endif |
| 11 | } |
| 12 | |
| 13 | int strcmp_nocase(const char* s1, const char* s2) |
| 14 | { |
| 15 | const unsigned char* p1 = (const unsigned char*)s1; |
| 16 | const unsigned char* p2 = (const unsigned char*)s2; |
| 17 | unsigned char c1, c2; |
| 18 | |
| 19 | do |
| 20 | { |
| 21 | c1 = (unsigned char)tolower(*p1++); |
| 22 | c2 = (unsigned char)tolower(*p2++); |
| 23 | if (c1 == '\0') |
| 24 | return c1 - c2; |
| 25 | } |
| 26 | while (c1 == c2); |
| 27 | |
| 28 | return c1 - c2; |
| 29 | } |
| 30 | |
| 31 | int strncmp_nocase(const char* s1, const char* s2, size_t n) |
| 32 | { |
| 33 | const unsigned char* p1 = (const unsigned char*)s1; |
| 34 | const unsigned char* p2 = (const unsigned char*)s2; |
| 35 | unsigned char c1, c2; |
| 36 | |
| 37 | if(n == 0) |
| 38 | return 0; |
| 39 | |
| 40 | do |
| 41 | { |
| 42 | c1 = (unsigned char)tolower(*p1++); |
| 43 | c2 = (unsigned char)tolower(*p2++); |
| 44 | if (c1 == '\0') |
| 45 | return c1 - c2; |
| 46 | } |
| 47 | while (c1 == c2 && --n); |
| 48 | |
| 49 | return c1 - c2; |
| 50 | } |
| 51 | |
| 52 | #if __STDC_VERSION__ >= 199901L |
| 53 | #ifdef UINT32_MAX |
| 54 | uint32_t ntoh32(uint32_t const net) |
| 55 | { |
| 56 | uint8_t data[4]; |
| 57 | memcpy(&data, &net, sizeof(data)); |
| 58 | |
| 59 | return ((uint32_t) data[3] << 0) |
| 60 | | ((uint32_t) data[2] << 8) |
| 61 | | ((uint32_t) data[1] << 16) |
| 62 | | ((uint32_t) data[0] << 24); |
| 63 | } |
| 64 | #endif |
| 65 | #endif |
| 66 | |
| 67 | #if __STDC_VERSION__ >= 201112L |
| 68 | struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts) |
| 69 | { |
| 70 | struct timespec diff; |
| 71 | |
| 72 | if ((new_ts->tv_nsec - old_ts->tv_nsec) < 0) |
| 73 | { |
| 74 | diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec - 1; |
| 75 | diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec + 1000000000; |
| 76 | } else { |
| 77 | diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec; |
| 78 | diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec; |
| 79 | } |
| 80 | |
| 81 | return diff; |
| 82 | } |
| 83 | |
| 84 | struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2) |
| 85 | { |
| 86 | struct timespec res; |
| 87 | |
| 88 | if (999999999 - ts_1->tv_nsec < ts_2->tv_nsec) |
| 89 | { |
| 90 | res.tv_sec = ts_1->tv_sec + ts_2->tv_sec + 1; |
| 91 | res.tv_nsec = ts_1->tv_nsec + (ts_2->tv_nsec - 1000000000) ; |
| 92 | } else { |
| 93 | res.tv_sec = ts_1->tv_sec + ts_2->tv_sec; |
| 94 | res.tv_nsec = ts_1->tv_nsec + ts_2->tv_nsec; |
| 95 | } |
| 96 | |
| 97 | return res; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | max_uint_t timespec_ms(const struct timespec* ts) |
| 102 | { |
| 103 | return (max_uint_t)ts->tv_sec * 1000 + (max_uint_t)ts->tv_nsec/1000000; |
| 104 | } |
| 105 | #endif |
| 106 |