Commit 1348b7c8 authored by Thomas Haller's avatar Thomas Haller

shared: move utils NM_UTILS_ENUM2STR*, NM_UTILS_FLAGS2STR*, and nm_utils_to_string_buffer_init()

These are just simple utility functions, not tied to NetworkManager
core. Move them to shared/nm-utils, so that they may be reused
otherwise.
parent a2dcdbe0
......@@ -115,6 +115,89 @@ nm_utils_strbuf_append (char **buf, gsize *len, const char *format, ...)
/*****************************************************************************/
char _nm_utils_to_string_buffer[];
void
nm_utils_to_string_buffer_init (char **buf, gsize *len)
{
if (!*buf) {
*buf = _nm_utils_to_string_buffer;
*len = sizeof (_nm_utils_to_string_buffer);
}
}
gboolean
nm_utils_to_string_buffer_init_null (gconstpointer obj, char **buf, gsize *len)
{
nm_utils_to_string_buffer_init (buf, len);
if (!obj) {
g_strlcpy (*buf, "(null)", *len);
return FALSE;
}
return TRUE;
}
/*****************************************************************************/
const char *
nm_utils_flags2str (const NMUtilsFlags2StrDesc *descs,
gsize n_descs,
unsigned flags,
char *buf,
gsize len)
{
gsize i;
char *p;
#if NM_MORE_ASSERTS > 10
nm_assert (descs);
nm_assert (n_descs > 0);
for (i = 0; i < n_descs; i++) {
gsize j;
nm_assert (descs[i].name && descs[i].name[0]);
for (j = 0; j < i; j++)
nm_assert (descs[j].flag != descs[i].flag);
}
#endif
nm_utils_to_string_buffer_init (&buf, &len);
if (!len)
return buf;
buf[0] = '\0';
p = buf;
if (!flags) {
for (i = 0; i < n_descs; i++) {
if (!descs[i].flag) {
nm_utils_strbuf_append_str (&p, &len, descs[i].name);
break;
}
}
return buf;
}
for (i = 0; flags && i < n_descs; i++) {
if ( descs[i].flag
&& NM_FLAGS_ALL (flags, descs[i].flag)) {
flags &= ~descs[i].flag;
if (buf[0] != '\0')
nm_utils_strbuf_append_c (&p, &len, ',');
nm_utils_strbuf_append_str (&p, &len, descs[i].name);
}
}
if (flags) {
if (buf[0] != '\0')
nm_utils_strbuf_append_c (&p, &len, ',');
nm_utils_strbuf_append (&p, &len, "0x%x", flags);
}
return buf;
};
/*****************************************************************************/
/**
* _nm_utils_ip4_prefix_to_netmask:
* @prefix: a CIDR prefix
......
......@@ -212,6 +212,74 @@ gint _nm_utils_ascii_str_to_bool (const char *str,
/*****************************************************************************/
extern char _nm_utils_to_string_buffer[2096];
void nm_utils_to_string_buffer_init (char **buf, gsize *len);
gboolean nm_utils_to_string_buffer_init_null (gconstpointer obj, char **buf, gsize *len);
/*****************************************************************************/
typedef struct {
unsigned flag;
const char *name;
} NMUtilsFlags2StrDesc;
#define NM_UTILS_FLAGS2STR(f, n) { .flag = f, .name = ""n, }
#define _NM_UTILS_FLAGS2STR_DEFINE(scope, fcn_name, flags_type, ...) \
scope const char * \
fcn_name (flags_type flags, char *buf, gsize len) \
{ \
static const NMUtilsFlags2StrDesc descs[] = { \
__VA_ARGS__ \
}; \
G_STATIC_ASSERT (sizeof (flags_type) <= sizeof (unsigned)); \
return nm_utils_flags2str (descs, G_N_ELEMENTS (descs), flags, buf, len); \
};
#define NM_UTILS_FLAGS2STR_DEFINE(fcn_name, flags_type, ...) \
_NM_UTILS_FLAGS2STR_DEFINE (, fcn_name, flags_type, __VA_ARGS__)
#define NM_UTILS_FLAGS2STR_DEFINE_STATIC(fcn_name, flags_type, ...) \
_NM_UTILS_FLAGS2STR_DEFINE (static, fcn_name, flags_type, __VA_ARGS__)
const char *nm_utils_flags2str (const NMUtilsFlags2StrDesc *descs,
gsize n_descs,
unsigned flags,
char *buf,
gsize len);
/*****************************************************************************/
#define NM_UTILS_ENUM2STR(v, n) (void) 0; case v: s = ""n""; break; (void) 0
#define NM_UTILS_ENUM2STR_IGNORE(v) (void) 0; case v: break; (void) 0
#define _NM_UTILS_ENUM2STR_DEFINE(scope, fcn_name, lookup_type, int_fmt, ...) \
scope const char * \
fcn_name (lookup_type val, char *buf, gsize len) \
{ \
nm_utils_to_string_buffer_init (&buf, &len); \
if (len) { \
const char *s = NULL; \
switch (val) { \
(void) 0, \
__VA_ARGS__ \
(void) 0; \
}; \
if (s) \
g_strlcpy (buf, s, len); \
else \
g_snprintf (buf, len, "(%"int_fmt")", val); \
} \
return buf; \
}
#define NM_UTILS_ENUM2STR_DEFINE(fcn_name, lookup_type, ...) \
_NM_UTILS_ENUM2STR_DEFINE (, fcn_name, lookup_type, "d", __VA_ARGS__)
#define NM_UTILS_ENUM2STR_DEFINE_STATIC(fcn_name, lookup_type, ...) \
_NM_UTILS_ENUM2STR_DEFINE (static, fcn_name, lookup_type, "d", __VA_ARGS__)
/*****************************************************************************/
#define _nm_g_slice_free_fcn_define(mem_size) \
static inline void \
_nm_g_slice_free_fcn_##mem_size (gpointer mem_block) \
......
......@@ -1831,87 +1831,6 @@ nm_match_spec_join (GSList *specs)
/*****************************************************************************/
char _nm_utils_to_string_buffer[];
void
nm_utils_to_string_buffer_init (char **buf, gsize *len)
{
if (!*buf) {
*buf = _nm_utils_to_string_buffer;
*len = sizeof (_nm_utils_to_string_buffer);
}
}
gboolean
nm_utils_to_string_buffer_init_null (gconstpointer obj, char **buf, gsize *len)
{
nm_utils_to_string_buffer_init (buf, len);
if (!obj) {
g_strlcpy (*buf, "(null)", *len);
return FALSE;
}
return TRUE;
}
const char *
nm_utils_flags2str (const NMUtilsFlags2StrDesc *descs,
gsize n_descs,
unsigned flags,
char *buf,
gsize len)
{
gsize i;
char *p;
#if NM_MORE_ASSERTS > 10
nm_assert (descs);
nm_assert (n_descs > 0);
for (i = 0; i < n_descs; i++) {
gsize j;
nm_assert (descs[i].name && descs[i].name[0]);
for (j = 0; j < i; j++)
nm_assert (descs[j].flag != descs[i].flag);
}
#endif
nm_utils_to_string_buffer_init (&buf, &len);
if (!len)
return buf;
buf[0] = '\0';
p = buf;
if (!flags) {
for (i = 0; i < n_descs; i++) {
if (!descs[i].flag) {
nm_utils_strbuf_append_str (&p, &len, descs[i].name);
break;
}
}
return buf;
}
for (i = 0; flags && i < n_descs; i++) {
if ( descs[i].flag
&& NM_FLAGS_ALL (flags, descs[i].flag)) {
flags &= ~descs[i].flag;
if (buf[0] != '\0')
nm_utils_strbuf_append_c (&p, &len, ',');
nm_utils_strbuf_append_str (&p, &len, descs[i].name);
}
}
if (flags) {
if (buf[0] != '\0')
nm_utils_strbuf_append_c (&p, &len, ',');
nm_utils_strbuf_append (&p, &len, "0x%x", flags);
}
return buf;
};
/*****************************************************************************/
char *
nm_utils_new_vlan_name (const char *parent_iface, guint32 vlan_id)
{
......
......@@ -212,72 +212,6 @@ NMMatchSpecMatchType nm_match_spec_config (const GSList *specs,
GSList *nm_match_spec_split (const char *value);
char *nm_match_spec_join (GSList *specs);
extern char _nm_utils_to_string_buffer[2096];
void nm_utils_to_string_buffer_init (char **buf, gsize *len);
gboolean nm_utils_to_string_buffer_init_null (gconstpointer obj, char **buf, gsize *len);
/*****************************************************************************/
typedef struct {
unsigned flag;
const char *name;
} NMUtilsFlags2StrDesc;
#define NM_UTILS_FLAGS2STR(f, n) { .flag = f, .name = ""n, }
#define _NM_UTILS_FLAGS2STR_DEFINE(scope, fcn_name, flags_type, ...) \
scope const char * \
fcn_name (flags_type flags, char *buf, gsize len) \
{ \
static const NMUtilsFlags2StrDesc descs[] = { \
__VA_ARGS__ \
}; \
G_STATIC_ASSERT (sizeof (flags_type) <= sizeof (unsigned)); \
return nm_utils_flags2str (descs, G_N_ELEMENTS (descs), flags, buf, len); \
};
#define NM_UTILS_FLAGS2STR_DEFINE(fcn_name, flags_type, ...) \
_NM_UTILS_FLAGS2STR_DEFINE (, fcn_name, flags_type, __VA_ARGS__)
#define NM_UTILS_FLAGS2STR_DEFINE_STATIC(fcn_name, flags_type, ...) \
_NM_UTILS_FLAGS2STR_DEFINE (static, fcn_name, flags_type, __VA_ARGS__)
const char *nm_utils_flags2str (const NMUtilsFlags2StrDesc *descs,
gsize n_descs,
unsigned flags,
char *buf,
gsize len);
/*****************************************************************************/
#define NM_UTILS_ENUM2STR(v, n) (void) 0; case v: s = ""n""; break; (void) 0
#define NM_UTILS_ENUM2STR_IGNORE(v) (void) 0; case v: break; (void) 0
#define _NM_UTILS_ENUM2STR_DEFINE(scope, fcn_name, lookup_type, int_fmt, ...) \
scope const char * \
fcn_name (lookup_type val, char *buf, gsize len) \
{ \
nm_utils_to_string_buffer_init (&buf, &len); \
if (len) { \
const char *s = NULL; \
switch (val) { \
(void) 0, \
__VA_ARGS__ \
(void) 0; \
}; \
if (s) \
g_strlcpy (buf, s, len); \
else \
g_snprintf (buf, len, "(%"int_fmt")", val); \
} \
return buf; \
}
#define NM_UTILS_ENUM2STR_DEFINE(fcn_name, lookup_type, ...) \
_NM_UTILS_ENUM2STR_DEFINE (, fcn_name, lookup_type, "d", __VA_ARGS__)
#define NM_UTILS_ENUM2STR_DEFINE_STATIC(fcn_name, lookup_type, ...) \
_NM_UTILS_ENUM2STR_DEFINE (static, fcn_name, lookup_type, "d", __VA_ARGS__)
/*****************************************************************************/
const char *nm_utils_get_ip_config_method (NMConnection *connection,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment