blob: a1a18e3028d50eb631c6ed3d7cb131700da147ed [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001#ifndef OSMOCORE_UTIL_H
2#define OSMOCORE_UTIL_H
3
4#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Harald Weltee9b744e2011-06-26 14:19:54 +02005#define OSMO_MAX(a, b) (a) >= (b) ? (a) : (b)
Harald Welte430be842011-07-27 22:23:11 +02006#define OSMO_MIN(a, b) (a) >= (b) ? (b) : (a)
Harald Welteec8b4502010-02-20 20:34:29 +01007
Harald Welted284cd92010-03-01 21:58:31 +01008#include <stdint.h>
9
10struct value_string {
11 unsigned int value;
12 const char *str;
13};
14
15const char *get_value_string(const struct value_string *vs, uint32_t val);
16int get_string_value(const struct value_string *vs, const char *str);
17
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020018char osmo_bcd2char(uint8_t bcd);
Harald Weltea73e2f92010-03-04 10:50:32 +010019/* only works for numbers in ascci */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020020uint8_t osmo_char2bcd(char c);
Harald Welted284cd92010-03-01 21:58:31 +010021
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020022int osmo_hexparse(const char *str, uint8_t *b, int max_len);
23char *osmo_hexdump(const unsigned char *buf, int len);
24char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len);
25char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
Harald Welte3eba9912010-07-30 10:37:29 +020026
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020027#define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1];
Holger Hans Peter Freyther52c07ca2011-01-16 17:37:27 +010028
Harald Welte28222962011-02-18 20:37:04 +010029void osmo_str2lower(char *out, const char *in);
30void osmo_str2upper(char *out, const char *in);
31
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +020032#define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
33do { \
34 len += ret; \
35 if (ret > rem) \
36 ret = rem; \
37 offset += ret; \
38 rem -= ret; \
39} while (0)
40
Harald Welteec8b4502010-02-20 20:34:29 +010041#endif