misc.h
Raw
1#ifndef MISC_H
2#define MISC_H
3
4#ifdef __unix__
5 #ifndef _BSD_SOURCE
6 #define _BSD_SOURCE 1
7 #endif
8 #ifndef _DEFAULT_SOURCE
9 #define _DEFAULT_SOURCE 1
10 #endif
11 #include <unistd.h>
12#endif
13#ifdef _WIN32
14 #include <windows.h>
15#endif
16
17#include <string.h>
18#include <stdlib.h>
19#include <cutils/common.h>
20#include <ctype.h>
21
22#if __STDC_VERSION__ >= 199901L
23 #include <stdint.h>
24 typedef uintmax_t max_uint_t;
25 #ifdef UINT32_MAX
26 uint32_t ntoh32(uint32_t const net);
27 #endif
28 HEDLEY_INLINE
29 static void memqswap_stack(void* item1, void* item2, size_t length)
30 {
31 unsigned char tmp[length];
32 memcpy(tmp, item1, length);
33 memcpy(item1, item2, length);
34 memcpy(item2, tmp, length);
35 }
36#else
37 typedef unsigned long max_uint_t;
38#endif
39
40HEDLEY_INLINE
41static void memswap(void* item1, void* item2, size_t length)
42{
43 unsigned char *item1_tmp, *item2_tmp;
44 unsigned char tmp;
45
46 item1_tmp = item1;
47 item2_tmp = item2;
48
49 while(length--)
50 {
51 tmp = item1_tmp[length];
52 item1_tmp[length] = item2_tmp[length];
53 item2_tmp[length] = tmp;
54 }
55}
56
57HEDLEY_INLINE
58static void memqswap(void* item1, void* item2, void* tmp, size_t length)
59{
60 memcpy(tmp, item1, length);
61 memcpy(item1, item2, length);
62 memcpy(item2, tmp, length);
63}
64
65#if __STDC_VERSION__ >= 201112L
66 #include <time.h>
67#endif
68
69void sleep_ms(unsigned int milliseconds);
70int strcmp_nocase(const char* s1, const char* s2);
71int strncmp_nocase(const char* s1, const char* s2, size_t n);
72
73#if __STDC_VERSION__ >= 201112L
74 struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts);
75 struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2);
76 max_uint_t timespec_ms(const struct timespec* ts);
77#endif
78
79#endif /* MISC_H */
80