From 3418c22250e9eb734cec97ccc7d1d7188397c4e6 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 3 Nov 2017 06:49:07 +0100 Subject: [PATCH] Port _g_enum_value_to_nick() to C --- src/Makefile.am | 2 ++ src/matrix-types.vala | 21 --------------- src/utils.c | 60 +++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 30 ++++++++++++++++++++++ vapi/c-api.vapi | 4 +++ 5 files changed, 96 insertions(+), 21 deletions(-) create mode 100644 src/utils.c create mode 100644 src/utils.h diff --git a/src/Makefile.am b/src/Makefile.am index 9cefb0a..11f007f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -115,6 +115,7 @@ bin_PROGRAMS = test-api-client test-client INST_H_SRC_FILES = \ matrix-c-types.h \ + utils.h \ $(NULL) INST_H_BUILT_FILES = \ @@ -134,6 +135,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-event-types.c \ matrix-version.c \ matrix-c-types.c \ + utils.c \ matrix-enumtypes.c \ $(INST_H_SRC_FILES) \ $(NULL) diff --git a/src/matrix-types.vala b/src/matrix-types.vala index 9d152a4..3b772f4 100644 --- a/src/matrix-types.vala +++ b/src/matrix-types.vala @@ -308,25 +308,4 @@ namespace Matrix { return null; } } - - private string? - _g_enum_value_to_nick(Type enum_type, - int value, - bool convert_dashes = true) - { - EnumClass enum_class = (EnumClass)enum_type.class_ref(); - unowned EnumValue? enum_val = enum_class.get_value(value); - - if (enum_val != null) { - var nick = enum_val.value_nick; - - if (convert_dashes) { - return nick.replace("-", "_"); - } - - return nick; - } else { - return null; - } - } } diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..1a5dd27 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,60 @@ +/* + * This file is part of matrix-glib-sdk + * + * matrix-glib-sdk 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 3 of the License, or (at your option) any later version. + * + * matrix-glib-sdk 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 matrix-glib-sdk. If not, see + * . + */ + +#include "utils.h" + +// GTK-Doc looking comments in this file intentionally do not begin with a double star, as the +// functions here are internal + +/* + * _matrix_g_enum_to_string: + * + * @enum_type: a #GEnumType + * @value: an integer value registered in @enum_type + * @convert_dashes: if non-NUL, dashes in the value nick will be converted to this char + * + * Get the value nick (lower case, dash-separated name) of an enum, like matrix-error-none. If + * @convert_dashes is not NUL (`'\0'`), dashes will be converted to that char. + * + * Returns: (transfer full) the converted enum name, which must be freed + */ +gchar * +_matrix_g_enum_to_string(GType enum_type, gint value, gboolean convert_dashes) +{ + GEnumClass *enum_class = g_type_class_ref(enum_type); + GEnumValue *enum_value = g_enum_get_value(enum_class, value); + gchar *nick = NULL; + + if (value) { + nick = g_strdup(enum_value->value_nick); + + if (convert_dashes) { + gchar *a; + + for (a = nick; *a; a++) { + if (*a == '-') { + *a = '_'; + } + } + } + } + + g_type_class_unref(enum_class); + + return nick; +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..dc3c496 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,30 @@ +/* + * This file is part of matrix-glib-sdk + * + * matrix-glib-sdk 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 3 of the License, or (at your option) any later version. + * + * matrix-glib-sdk 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 matrix-glib-sdk. If not, see + * . + */ + +#ifndef __MATRIX_GLIB_SDK_UTILS_H__ +# define __MATRIX_GLIB_SDK_UTILS_H__ + +# include + +G_BEGIN_DECLS + +gchar *_matrix_g_enum_to_string(GType enum_type, gint value, gboolean convert_dashes); + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_UTILS_H__ */ diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index 1d5236d..9046e33 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -204,6 +204,10 @@ namespace Matrix { ANSWER; } + /* Utilities */ + [CCode (cheader_filename = "utils.h", cname = "_matrix_g_enum_to_string")] + public string? _g_enum_value_to_nick(GLib.Type enum_type, int value, bool convert_dashes = true); + /** * The major version number of the Matrix.org GLib SDK. */