libosmocore  0.9.3
Osmocom core library
include/osmocom/core/utils.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include <osmocom/core/backtrace.h>
00004 #include <osmocom/core/talloc.h>
00005 
00013 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
00014 
00015 #define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
00016 
00017 #define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
00018 
00019 #include <stdint.h>
00020 
00022 struct value_string {
00023         unsigned int value;     
00024         const char *str;        
00025 };
00026 
00027 const char *get_value_string(const struct value_string *vs, uint32_t val);
00028 
00029 int get_string_value(const struct value_string *vs, const char *str);
00030 
00031 char osmo_bcd2char(uint8_t bcd);
00032 /* only works for numbers in ascci */
00033 uint8_t osmo_char2bcd(char c);
00034 
00035 int osmo_hexparse(const char *str, uint8_t *b, int max_len);
00036 
00037 char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
00038 char *osmo_hexdump(const unsigned char *buf, int len);
00039 char *osmo_hexdump_nospc(const unsigned char *buf, int len);
00040 char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
00041 
00042 #define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
00043 
00044 void osmo_str2lower(char *out, const char *in);
00045 void osmo_str2upper(char *out, const char *in);
00046 
00047 #define OSMO_SNPRINTF_RET(ret, rem, offset, len)                \
00048 do {                                                            \
00049         len += ret;                                             \
00050         if (ret > rem)                                          \
00051                 ret = rem;                                      \
00052         offset += ret;                                          \
00053         rem -= ret;                                             \
00054 } while (0)
00055 
00056 #define OSMO_ASSERT(exp)    \
00057         if (!(exp)) { \
00058                 fprintf(stderr, "Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \
00059                 osmo_generate_backtrace(); \
00060                 abort(); \
00061         }
00062 
00063 static inline void osmo_talloc_replace_string(void *ctx, char **dst, char *newstr)
00064 {
00065         if (*dst)
00066                 talloc_free(*dst);
00067         *dst = talloc_strdup(ctx, newstr);
00068 }
00069