Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | #ifndef OSMOCORE_UTIL_H |
| 2 | #define OSMOCORE_UTIL_H |
| 3 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 4 | /*! \defgroup utils General-purpose utility functions |
| 5 | * @{ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 8 | /*! \file utils.h */ |
| 9 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 10 | /*! \brief Determine number of elements in an array of static size */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 11 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 12 | /*! \brief Return the maximum of two specified values */ |
Harald Welte | e9b744e | 2011-06-26 14:19:54 +0200 | [diff] [blame] | 13 | #define OSMO_MAX(a, b) (a) >= (b) ? (a) : (b) |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 14 | /*! \brief Return the minimum of two specified values */ |
Harald Welte | 430be84 | 2011-07-27 22:23:11 +0200 | [diff] [blame] | 15 | #define OSMO_MIN(a, b) (a) >= (b) ? (b) : (a) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 16 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 19 | /*! \brief A mapping between human-readable string and numeric value */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 20 | struct value_string { |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 21 | unsigned int value; /*!< \brief numeric value */ |
| 22 | const char *str; /*!< \brief human-readable string */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | const char *get_value_string(const struct value_string *vs, uint32_t val); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 26 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 27 | int get_string_value(const struct value_string *vs, const char *str); |
| 28 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 29 | char osmo_bcd2char(uint8_t bcd); |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 30 | /* only works for numbers in ascci */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 31 | uint8_t osmo_char2bcd(char c); |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 32 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 33 | int osmo_hexparse(const char *str, uint8_t *b, int max_len); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 34 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 35 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len); |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 36 | char *osmo_hexdump(const unsigned char *buf, int len); |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 37 | char *osmo_hexdump_nospc(const unsigned char *buf, int len); |
Sylvain Munaut | 4cfbae8 | 2011-11-13 23:04:00 +0100 | [diff] [blame] | 38 | char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__)); |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 39 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 40 | #define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1]; |
Holger Hans Peter Freyther | 52c07ca | 2011-01-16 17:37:27 +0100 | [diff] [blame] | 41 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 42 | void osmo_str2lower(char *out, const char *in); |
| 43 | void osmo_str2upper(char *out, const char *in); |
| 44 | |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 45 | #define OSMO_SNPRINTF_RET(ret, rem, offset, len) \ |
| 46 | do { \ |
| 47 | len += ret; \ |
| 48 | if (ret > rem) \ |
| 49 | ret = rem; \ |
| 50 | offset += ret; \ |
| 51 | rem -= ret; \ |
| 52 | } while (0) |
| 53 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 54 | /*! @} */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 55 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 56 | #endif |