a1a18e3028d50eb631c6ed3d7cb131700da147ed
[osmocom-bb.git] / include / osmocom / core / utils.h
1 #ifndef OSMOCORE_UTIL_H
2 #define OSMOCORE_UTIL_H
3
4 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
5 #define OSMO_MAX(a, b) (a) >= (b) ? (a) : (b)
6 #define OSMO_MIN(a, b) (a) >= (b) ? (b) : (a)
7
8 #include <stdint.h>
9
10 struct value_string {
11         unsigned int value;
12         const char *str;
13 };
14
15 const char *get_value_string(const struct value_string *vs, uint32_t val);
16 int get_string_value(const struct value_string *vs, const char *str);
17
18 char osmo_bcd2char(uint8_t bcd);
19 /* only works for numbers in ascci */
20 uint8_t osmo_char2bcd(char c);
21
22 int osmo_hexparse(const char *str, uint8_t *b, int max_len);
23 char *osmo_hexdump(const unsigned char *buf, int len);
24 char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len);
25 char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
26
27 #define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1];
28
29 void osmo_str2lower(char *out, const char *in);
30 void osmo_str2upper(char *out, const char *in);
31
32 #define OSMO_SNPRINTF_RET(ret, rem, offset, len)                \
33 do {                                                            \
34         len += ret;                                             \
35         if (ret > rem)                                          \
36                 ret = rem;                                      \
37         offset += ret;                                          \
38         rem -= ret;                                             \
39 } while (0)
40
41 #endif