blob: 03861d789931d4908389b6a3db230e7a9f58e8cf [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001#ifndef OSMOCORE_UTIL_H
2#define OSMOCORE_UTIL_H
3
Harald Welte8598f182011-08-17 14:19:27 +02004/*! \defgroup utils General-purpose utility functions
5 * @{
Harald Weltebd598e32011-08-16 23:26:52 +02006 */
7
Harald Welte8598f182011-08-17 14:19:27 +02008/*! \file utils.h */
9
Harald Weltebd598e32011-08-16 23:26:52 +020010/*! \brief Determine number of elements in an array of static size */
Harald Welteec8b4502010-02-20 20:34:29 +010011#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Harald Weltebd598e32011-08-16 23:26:52 +020012/*! \brief Return the maximum of two specified values */
Holger Hans Peter Freyther08b28622012-08-15 17:02:59 +020013#define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
Harald Weltebd598e32011-08-16 23:26:52 +020014/*! \brief Return the minimum of two specified values */
Holger Hans Peter Freyther08b28622012-08-15 17:02:59 +020015#define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
Harald Welteec8b4502010-02-20 20:34:29 +010016
Harald Welted284cd92010-03-01 21:58:31 +010017#include <stdint.h>
18
Harald Weltebd598e32011-08-16 23:26:52 +020019/*! \brief A mapping between human-readable string and numeric value */
Harald Welted284cd92010-03-01 21:58:31 +010020struct value_string {
Harald Weltebd598e32011-08-16 23:26:52 +020021 unsigned int value; /*!< \brief numeric value */
22 const char *str; /*!< \brief human-readable string */
Harald Welted284cd92010-03-01 21:58:31 +010023};
24
25const char *get_value_string(const struct value_string *vs, uint32_t val);
Harald Weltebd598e32011-08-16 23:26:52 +020026
Harald Welted284cd92010-03-01 21:58:31 +010027int get_string_value(const struct value_string *vs, const char *str);
28
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020029char osmo_bcd2char(uint8_t bcd);
Harald Weltea73e2f92010-03-04 10:50:32 +010030/* only works for numbers in ascci */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020031uint8_t osmo_char2bcd(char c);
Harald Welted284cd92010-03-01 21:58:31 +010032
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020033int osmo_hexparse(const char *str, uint8_t *b, int max_len);
Harald Weltebd598e32011-08-16 23:26:52 +020034
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020035char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
Harald Welte8598f182011-08-17 14:19:27 +020036char *osmo_hexdump(const unsigned char *buf, int len);
Sylvain Munautff23d242011-11-10 23:03:18 +010037char *osmo_hexdump_nospc(const unsigned char *buf, int len);
Sylvain Munaut4cfbae82011-11-13 23:04:00 +010038char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
Harald Welte3eba9912010-07-30 10:37:29 +020039
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020040#define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1];
Holger Hans Peter Freyther52c07ca2011-01-16 17:37:27 +010041
Harald Welte28222962011-02-18 20:37:04 +010042void osmo_str2lower(char *out, const char *in);
43void osmo_str2upper(char *out, const char *in);
44
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +020045#define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
46do { \
47 len += ret; \
48 if (ret > rem) \
49 ret = rem; \
50 offset += ret; \
51 rem -= ret; \
52} while (0)
53
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020054/*! @} */
Harald Welte8598f182011-08-17 14:19:27 +020055
Harald Welteec8b4502010-02-20 20:34:29 +010056#endif