[Spice-devel] [spice-common] utils: Add genum -> string helpers
Christophe Fergeau
cfergeau at redhat.com
Fri Nov 23 09:45:18 UTC 2018
On Thu, Nov 22, 2018 at 04:08:45PM -0500, Frediano Ziglio wrote:
> >
> > This makes it easier to convert enums registered with glib type system
> > to string in order to print them at runtime.
> >
> > Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
> > ---
> > common/Makefile.am | 2 ++
> > common/meson.build | 2 ++
> > common/utils.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++
> > common/utils.h | 33 ++++++++++++++++++++++++
> > 4 files changed, 101 insertions(+)
> > create mode 100644 common/utils.c
> > create mode 100644 common/utils.h
> >
> > diff --git a/common/Makefile.am b/common/Makefile.am
> > index da0d941..197e419 100644
> > --- a/common/Makefile.am
> > +++ b/common/Makefile.am
> > @@ -50,6 +50,8 @@ libspice_common_la_SOURCES = \
> > rop3.h \
> > snd_codec.c \
> > snd_codec.h \
> > + utils.c \
> > + utils.h \
> > verify.h \
> > $(NULL)
> >
> > diff --git a/common/meson.build b/common/meson.build
> > index 6ac55dc..8f22310 100644
> > --- a/common/meson.build
> > +++ b/common/meson.build
> > @@ -35,6 +35,8 @@ spice_common_sources = [
> > 'rop3.h',
> > 'snd_codec.c',
> > 'snd_codec.h',
> > + 'utils.c',
> > + 'utils.h',
> > 'verify.h'
> > ]
> >
> > diff --git a/common/utils.c b/common/utils.c
> > new file mode 100644
> > index 0000000..768eeac
> > --- /dev/null
> > +++ b/common/utils.c
> > @@ -0,0 +1,64 @@
> > +/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
>
> Really OT: somebody uses these?
No idea :)
>
> > +/*
> > + Copyright (C) 2010, 2011, 2018 Red Hat, Inc.
> > +
> > + This library is free software; you can redistribute it and/or
> > + modify it under the terms of the GNU Lesser General Public
> > + License as published by the Free Software Foundation; either
> > + version 2.1 of the License, or (at your option) any later version.
> > +
> > + This library is distributed in the hope that it will be useful,
> > + but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> > + Lesser General Public License for more details.
> > +
> > + You should have received a copy of the GNU Lesser General Public
> > + License along with this library; if not, see
> > <http://www.gnu.org/licenses/>.
> > +*/
> > +
>
> I don't know if it is just SPICE server, don't we require to include config.h
> in all .c files?
Yes, I forgot, added now.
>
> > +#include "utils.h"
> > +
> > +/* These 2 functions come from
> > + * libvirt-glib/libvirt-gconfig/libvirt-gconfig-helpers.c
> > + * Copyright (C) 2010, 2011 Red Hat, Inc.
> > + * LGPLv2.1+ licensed */
> > +
> > +const char *
> > +spice_genum_get_nick(GType enum_type, gint value)
> > +{
> > + GEnumClass *enum_class;
> > + GEnumValue *enum_value;
> > +
> > + g_return_val_if_fail(G_TYPE_IS_ENUM(enum_type), NULL);
> > +
> > + enum_class = g_type_class_ref(enum_type);
> > + enum_value = g_enum_get_value(enum_class, value);
> > + g_type_class_unref(enum_class);
> > +
> > + if (enum_value != NULL) {
> > + return enum_value->value_nick;
> > + }
> > +
> > + g_return_val_if_reached(NULL);
> > +}
> > +
> > +int
> > +spice_genum_get_value(GType enum_type, const char *nick,
> > + gint default_value)
> > +{
> > + GEnumClass *enum_class;
> > + GEnumValue *enum_value;
> > +
> > + g_return_val_if_fail(G_TYPE_IS_ENUM(enum_type), default_value);
> > + g_return_val_if_fail(nick != NULL, default_value);
> > +
> > + enum_class = g_type_class_ref(enum_type);
> > + enum_value = g_enum_get_value_by_nick(enum_class, nick);
> > + g_type_class_unref(enum_class);
> > +
> > + if (enum_value != NULL) {
> > + return enum_value->value;
> > + }
> > +
> > + g_return_val_if_reached(default_value);
> > +}
> > diff --git a/common/utils.h b/common/utils.h
> > new file mode 100644
> > index 0000000..395f102
> > --- /dev/null
> > +++ b/common/utils.h
> > @@ -0,0 +1,33 @@
> > +/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
> > +/*
> > + Copyright (C) 2010, 2011, 2018 Red Hat, Inc.
> > +
> > + This library is free software; you can redistribute it and/or
> > + modify it under the terms of the GNU Lesser General Public
> > + License as published by the Free Software Foundation; either
> > + version 2.1 of the License, or (at your option) any later version.
> > +
> > + This library is distributed in the hope that it will be useful,
> > + but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> > + Lesser General Public License for more details.
> > +
> > + You should have received a copy of the GNU Lesser General Public
> > + License along with this library; if not, see
> > <http://www.gnu.org/licenses/>.
> > +*/
> > +
> > +#ifndef __UTILS_H
> > +#define __UTILS_H
> > +
>
> I don't really like much identifier reserved by C99 to be used.
> It seems that most of spice-common uses _H_SPICE_ prefix (which is
> actually reserved too). H_SPICE_UTILS_whatever sounds reasonable.
Ok, at some point we should make these guards consistent in spice-common ;)
Christophe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://lists.freedesktop.org/archives/spice-devel/attachments/20181123/9fa9d78a/attachment.sig>
More information about the Spice-devel
mailing list