2010-02-20 19:34:29 +00:00
|
|
|
#ifndef OSMOCORE_UTIL_H
|
|
|
|
#define OSMOCORE_UTIL_H
|
|
|
|
|
2011-08-17 12:19:27 +00:00
|
|
|
/*! \defgroup utils General-purpose utility functions
|
|
|
|
* @{
|
2011-08-16 21:26:52 +00:00
|
|
|
*/
|
|
|
|
|
2011-08-17 12:19:27 +00:00
|
|
|
/*! \file utils.h */
|
|
|
|
|
2011-08-16 21:26:52 +00:00
|
|
|
/*! \brief Determine number of elements in an array of static size */
|
2010-02-20 19:34:29 +00:00
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
2011-08-16 21:26:52 +00:00
|
|
|
/*! \brief Return the maximum of two specified values */
|
2011-06-26 12:19:54 +00:00
|
|
|
#define OSMO_MAX(a, b) (a) >= (b) ? (a) : (b)
|
2011-08-16 21:26:52 +00:00
|
|
|
/*! \brief Return the minimum of two specified values */
|
2011-07-27 20:23:11 +00:00
|
|
|
#define OSMO_MIN(a, b) (a) >= (b) ? (b) : (a)
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2010-03-01 20:58:31 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2011-08-16 21:26:52 +00:00
|
|
|
/*! \brief A mapping between human-readable string and numeric value */
|
2010-03-01 20:58:31 +00:00
|
|
|
struct value_string {
|
2011-08-16 21:26:52 +00:00
|
|
|
unsigned int value; /*!< \brief numeric value */
|
|
|
|
const char *str; /*!< \brief human-readable string */
|
2010-03-01 20:58:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *get_value_string(const struct value_string *vs, uint32_t val);
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2010-03-01 20:58:31 +00:00
|
|
|
int get_string_value(const struct value_string *vs, const char *str);
|
|
|
|
|
2011-05-07 10:43:08 +00:00
|
|
|
char osmo_bcd2char(uint8_t bcd);
|
2010-03-04 09:50:32 +00:00
|
|
|
/* only works for numbers in ascci */
|
2011-05-07 10:43:08 +00:00
|
|
|
uint8_t osmo_char2bcd(char c);
|
2010-03-01 20:58:31 +00:00
|
|
|
|
2011-05-07 10:43:08 +00:00
|
|
|
int osmo_hexparse(const char *str, uint8_t *b, int max_len);
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2011-08-17 12:19:27 +00:00
|
|
|
char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
|
2011-05-07 10:43:08 +00:00
|
|
|
char *osmo_hexdump(const unsigned char *buf, int len);
|
2011-11-10 22:03:18 +00:00
|
|
|
char *osmo_hexdump_nospc(const unsigned char *buf, int len);
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2011-05-07 10:43:08 +00:00
|
|
|
#define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1];
|
2011-01-16 16:37:27 +00:00
|
|
|
|
2011-02-18 19:37:04 +00:00
|
|
|
void osmo_str2lower(char *out, const char *in);
|
|
|
|
void osmo_str2upper(char *out, const char *in);
|
|
|
|
|
2011-03-28 17:24:22 +00:00
|
|
|
#define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
|
|
|
|
do { \
|
|
|
|
len += ret; \
|
|
|
|
if (ret > rem) \
|
|
|
|
ret = rem; \
|
|
|
|
offset += ret; \
|
|
|
|
rem -= ret; \
|
|
|
|
} while (0)
|
|
|
|
|
2011-08-17 12:19:27 +00:00
|
|
|
/*! }@ */
|
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
#endif
|