PhotosZoomEvent photos_utils_get_zoom_event (GVariant *dictionary) { GEnumClass *zoom_event_class = NULL; GEnumValue *event_value; PhotosZoomEvent ret_val = PHOTOS_ZOOM_EVENT_NONE; const gchar *event_str; g_return_val_if_fail (dictionary != NULL, PHOTOS_ZOOM_EVENT_NONE); g_return_val_if_fail (g_variant_is_of_type (dictionary, G_VARIANT_TYPE_VARDICT), PHOTOS_ZOOM_EVENT_NONE); if (!g_variant_lookup (dictionary, "event", "&s", &event_str)) goto out; zoom_event_class = G_ENUM_CLASS (g_type_class_ref (PHOTOS_TYPE_ZOOM_EVENT)); event_value = g_enum_get_value_by_nick (zoom_event_class, event_str); if (event_value == NULL) event_value = g_enum_get_value_by_name (zoom_event_class, event_str); if (event_value == NULL) goto out; ret_val = (PhotosZoomEvent) event_value->value; out: g_clear_pointer (&zoom_event_class, g_type_class_unref); g_return_val_if_fail (ret_val != PHOTOS_ZOOM_EVENT_NONE, PHOTOS_ZOOM_EVENT_NONE); return ret_val; }
static gboolean enum_from_string (GType type, const gchar *string, gint *enum_value) { GEnumClass *eclass; GEnumValue *ev; gchar *endptr; gint value; gboolean retval = TRUE; g_return_val_if_fail (G_TYPE_IS_ENUM (type), 0); g_return_val_if_fail (string != NULL, 0); value = strtoul (string, &endptr, 0); if (endptr != string) /* parsed a number */ *enum_value = value; else { eclass = g_type_class_ref (type); ev = g_enum_get_value_by_name (eclass, string); if (!ev) ev = g_enum_get_value_by_nick (eclass, string); if (ev) *enum_value = ev->value; else retval = FALSE; g_type_class_unref (eclass); } return retval; }
static void _xfdashboard_gvalue_transform_string_enum(const GValue *inSourceValue, GValue *ioDestValue) { GEnumClass *enumClass; GEnumValue *enumValue; const gchar *value; /* Reference enum class to keep it alive for transformation */ enumClass=g_type_class_ref(G_VALUE_TYPE(ioDestValue)); /* Get value to convert */ value=(const gchar*)inSourceValue->data[0].v_pointer; /* Get enum value either by name or by nickname (whatever matches first) */ enumValue=g_enum_get_value_by_name(enumClass, value); if(!enumValue) enumValue=g_enum_get_value_by_nick(enumClass, value); /* Set value if enum could be found otherwise set 0 */ if(enumValue) ioDestValue->data[0].v_int=enumValue->value; else { ioDestValue->data[0].v_int=0; g_debug("Cannot get value for unknown enum '%s' for type %s", value, g_type_name(G_VALUE_TYPE(ioDestValue))); } /* Release allocated resources */ g_type_class_unref(enumClass); }
gint asCEnum(USER_OBJECT_ s_enum, GType etype) { GEnumClass *eclass = g_type_class_ref(etype); GEnumValue *evalue = NULL; gint eval = 0; if (IS_INTEGER(s_enum) || IS_NUMERIC(s_enum)) { evalue = g_enum_get_value(eclass, asCInteger(s_enum)); } else if (IS_CHARACTER(s_enum)) { const gchar* ename = asCString(s_enum); evalue = g_enum_get_value_by_name(eclass, ename); if (!evalue) evalue = g_enum_get_value_by_nick(eclass, ename); if (!evalue) evalue = g_enum_get_value(eclass, atoi(ename)); } if (!evalue) { PROBLEM "Could not parse enum value %s", asCString(s_enum) ERROR; } else eval = evalue->value; return(eval); }
static void thunar_column_model_load_visible_columns (ThunarColumnModel *column_model) { GEnumClass *klass; GEnumValue *value; gchar **visible_columns; gchar *tmp; gint n; /* determine the list of visible columns from the preferences */ g_object_get (G_OBJECT (column_model->preferences), "last-details-view-visible-columns", &tmp, NULL); visible_columns = g_strsplit (tmp, ",", -1); g_free (tmp); /* reset the visible columns for the model */ for (n = 0; n < THUNAR_N_VISIBLE_COLUMNS; ++n) column_model->visible[n] = FALSE; /* mark all columns included in the list as visible */ klass = g_type_class_ref (THUNAR_TYPE_COLUMN); for (n = 0; visible_columns[n] != NULL; ++n) { /* determine the enum value from the string */ value = g_enum_get_value_by_name (klass, visible_columns[n]); if (G_LIKELY (value != NULL && value->value < THUNAR_N_VISIBLE_COLUMNS)) column_model->visible[value->value] = TRUE; } g_type_class_unref (klass); /* the name column is always visible */ column_model->visible[THUNAR_COLUMN_NAME] = TRUE; /* release the list of visible columns */ g_strfreev (visible_columns); }
int go_conf_load_enum (GOConfNode *node, gchar const *key, GType t, int default_val) { int res; gchar *val_str = go_conf_load_string (node, key); gboolean use_default = TRUE; if (NULL != val_str) { GEnumClass *enum_class = G_ENUM_CLASS (g_type_class_ref (t)); GEnumValue *enum_value = g_enum_get_value_by_nick (enum_class, val_str); if (NULL == enum_value) enum_value = g_enum_get_value_by_name (enum_class, val_str); if (NULL != enum_value) { use_default = FALSE; res = enum_value->value; } else { g_warning ("Unknown value '%s' for %s", val_str, key); } g_type_class_unref (enum_class); g_free (val_str); } if (use_default) { d (g_warning ("Using default value '%d'", default_val)); return default_val; } return res; }
static void parse_params (const gchar *str, gint *width, gint *ellipsize_at, PangoEllipsizeMode *ellipsize, PangoWrapMode *wrap) { gchar **strings; gchar **str2; gint i; GEnumClass *eclass; GEnumValue *ev; strings = g_strsplit (str, ",", -1); for (i = 0; strings[i]; i++) { str2 = g_strsplit (strings[i], "=", -1); if (strcmp (str2[0], "width") == 0) *width = (gint) g_ascii_strtoll (str2[1], NULL, 10); else if (strcmp (str2[0], "ellipsize-at") == 0) *ellipsize_at = (gint) g_ascii_strtoll (str2[1], NULL, 10); else if (strcmp (str2[0], "ellipsize") == 0) { eclass = g_type_class_ref (PANGO_TYPE_ELLIPSIZE_MODE); ev = g_enum_get_value_by_name (eclass, str2[1]); if (!ev) ev = g_enum_get_value_by_nick (eclass, str2[1]); if (ev) *ellipsize = ev->value; g_type_class_unref (eclass); } else if (strcmp (str2[0], "wrap") == 0) { eclass = g_type_class_ref (PANGO_TYPE_WRAP_MODE); ev = g_enum_get_value_by_name (eclass, str2[1]); if (!ev) ev = g_enum_get_value_by_nick (eclass, str2[1]); if (ev) *wrap = ev->value; g_type_class_unref (eclass); } g_strfreev (str2); } g_strfreev (strings); }
/** * pyg_enum_get_value: * @enum_type: the GType of the flag. * @obj: a Python object representing the flag value * @val: a pointer to the location to store the integer representation of the flag. * * Converts a Python object to the integer equivalent. The conversion * will depend on the type of the Python object. If the object is an * integer, it is passed through directly. If it is a string, it will * be treated as a full or short enum name as defined in the GType. * * Returns: 0 on success or -1 on failure */ gint pyg_enum_get_value(GType enum_type, PyObject *obj, gint *val) { GEnumClass *eclass = NULL; gint res = -1; g_return_val_if_fail(val != NULL, -1); if (!obj) { *val = 0; res = 0; } else if (PYGLIB_PyLong_Check(obj)) { *val = PYGLIB_PyLong_AsLong(obj); res = 0; if (PyObject_TypeCheck(obj, &PyGEnum_Type) && ((PyGEnum *) obj)->gtype != enum_type) { g_warning("expected enumeration type %s, but got %s instead", g_type_name(enum_type), g_type_name(((PyGEnum *) obj)->gtype)); } /* Dumb code duplication, but probably not worth it to have yet another macro. */ } else if (PyLong_Check(obj)) { *val = PyLong_AsLong(obj); res = 0; if (PyObject_TypeCheck(obj, &PyGEnum_Type) && ((PyGEnum *) obj)->gtype != enum_type) { g_warning("expected enumeration type %s, but got %s instead", g_type_name(enum_type), g_type_name(((PyGEnum *) obj)->gtype)); } } else if (PYGLIB_PyUnicode_Check(obj)) { GEnumValue *info; char *str = PYGLIB_PyUnicode_AsString(obj); if (enum_type != G_TYPE_NONE) eclass = G_ENUM_CLASS(g_type_class_ref(enum_type)); else { PyErr_SetString(PyExc_TypeError, "could not convert string to enum because there is no GType associated to look up the value"); res = -1; } info = g_enum_get_value_by_name(eclass, str); g_type_class_unref(eclass); if (!info) info = g_enum_get_value_by_nick(eclass, str); if (info) { *val = info->value; res = 0; } else { PyErr_SetString(PyExc_TypeError, "could not convert string"); res = -1; } } else { PyErr_SetString(PyExc_TypeError,"enum values must be strings or ints"); res = -1; } return res; }
static GTokenType gimp_config_deserialize_enum (GValue *value, GParamSpec *prop_spec, GScanner *scanner) { GEnumClass *enum_class; GEnumValue *enum_value; enum_class = g_type_class_peek (G_VALUE_TYPE (value)); switch (g_scanner_peek_next_token (scanner)) { case G_TOKEN_IDENTIFIER: g_scanner_get_next_token (scanner); enum_value = g_enum_get_value_by_nick (enum_class, scanner->value.v_identifier); if (!enum_value) enum_value = g_enum_get_value_by_name (enum_class, scanner->value.v_identifier); if (!enum_value) { g_scanner_error (scanner, _("invalid value '%s' for token %s"), scanner->value.v_identifier, prop_spec->name); return G_TOKEN_NONE; } break; case G_TOKEN_INT: g_scanner_get_next_token (scanner); enum_value = g_enum_get_value (enum_class, (gint) scanner->value.v_int64); if (!enum_value) { g_scanner_error (scanner, _("invalid value '%ld' for token %s"), (glong) scanner->value.v_int64, prop_spec->name); return G_TOKEN_NONE; } break; default: return G_TOKEN_IDENTIFIER; } g_value_set_enum (value, enum_value->value); return G_TOKEN_RIGHT_PAREN; }
static void thunar_column_model_load_column_order (ThunarColumnModel *column_model) { ThunarColumn column; GEnumClass *klass; GEnumValue *value; gchar **column_order; gchar *tmp; gint i, j; /* determine the column order from the preferences */ g_object_get (G_OBJECT (column_model->preferences), "last-details-view-column-order", &tmp, NULL); column_order = g_strsplit (tmp, ",", -1); g_free (tmp); /* reset the column order to its default */ for (j = 0; j < THUNAR_N_VISIBLE_COLUMNS; ++j) column_model->order[j] = j; /* now rearrange the columns according to the preferences */ klass = g_type_class_ref (THUNAR_TYPE_COLUMN); for (i = 0; column_order[i] != NULL; ++i) { /* determine the enum value for the name */ value = g_enum_get_value_by_name (klass, column_order[i]); if (G_UNLIKELY (value == NULL || value->value == i)) continue; /* find the current position of the value */ for (j = 0; j < THUNAR_N_VISIBLE_COLUMNS; ++j) if (column_model->order[j] == value->value) break; /* check if valid */ if (G_LIKELY (j < THUNAR_N_VISIBLE_COLUMNS)) { /* exchange the positions of i and j */ column = column_model->order[i]; column_model->order[i] = column_model->order[j]; column_model->order[j] = column; } } g_type_class_unref (klass); /* release the column order */ g_strfreev (column_order); }
gboolean bb_enum_value_lookup (GType enum_type, const char *name, guint *out) { GEnumClass *c = g_type_class_ref (enum_type); GEnumValue *v = g_enum_get_value_by_name (c, name); if (v == NULL) { guint i; for (i = 0; i < c->n_values; i++) if (bb_param_spec_names_equal (c->values[i].value_nick, name)) { v = c->values + i; break; } } if (v == NULL) return FALSE; *out = v->value; g_type_class_unref (c); return TRUE; }
int main(int argc, char **argv) { GEnumClass *xenum = g_type_class_ref(MESON_TYPE_THE_XENUM); GFlagsClass *flags_enum = g_type_class_ref(MESON_TYPE_THE_FLAGS_ENUM); if (g_enum_get_value_by_name(xenum, "MESON_THE_XVALUE")->value != MESON_THE_XVALUE) { fprintf(stderr, "Get MESON_THE_XVALUE by name failed.\n"); return 1; } if (g_enum_get_value_by_nick(xenum, "the-xvalue")->value != MESON_THE_XVALUE) { fprintf(stderr, "Get MESON_THE_XVALUE by nick failed.\n"); return 2; } if (g_flags_get_value_by_name(flags_enum, "MESON_THE_FIRST_VALUE")->value != MESON_THE_FIRST_VALUE) { fprintf(stderr, "Get MESON_THE_FIRST_VALUE by name failed.\n"); return 3; } if (g_flags_get_value_by_nick(flags_enum, "the-first-value")->value != MESON_THE_FIRST_VALUE) { fprintf(stderr, "Get MESON_THE_FIRST_VALUE by nick failed.\n"); return 4; } g_type_class_unref(xenum); g_type_class_unref(flags_enum); fprintf(stderr, "All ok.\n"); return 0; }
static GTokenType plug_in_icon_deserialize (GScanner *scanner, GimpPlugInProcedure *proc) { GEnumClass *enum_class; GEnumValue *enum_value; GimpIconType icon_type; gint icon_data_length; gchar *icon_name; guint8 *icon_data; if (! gimp_scanner_parse_token (scanner, G_TOKEN_LEFT_PAREN)) return G_TOKEN_LEFT_PAREN; if (! gimp_scanner_parse_token (scanner, G_TOKEN_SYMBOL) || GPOINTER_TO_INT (scanner->value.v_symbol) != ICON) return G_TOKEN_SYMBOL; enum_class = g_type_class_peek (GIMP_TYPE_ICON_TYPE); switch (g_scanner_peek_next_token (scanner)) { case G_TOKEN_IDENTIFIER: g_scanner_get_next_token (scanner); enum_value = g_enum_get_value_by_nick (G_ENUM_CLASS (enum_class), scanner->value.v_identifier); if (!enum_value) enum_value = g_enum_get_value_by_name (G_ENUM_CLASS (enum_class), scanner->value.v_identifier); if (!enum_value) { g_scanner_error (scanner, _("invalid value '%s' for icon type"), scanner->value.v_identifier); return G_TOKEN_NONE; } break; case G_TOKEN_INT: g_scanner_get_next_token (scanner); enum_value = g_enum_get_value (enum_class, (gint) scanner->value.v_int64); if (!enum_value) { g_scanner_error (scanner, _("invalid value '%ld' for icon type"), (glong) scanner->value.v_int64); return G_TOKEN_NONE; } break; default: return G_TOKEN_IDENTIFIER; } icon_type = enum_value->value; if (! gimp_scanner_parse_int (scanner, &icon_data_length)) return G_TOKEN_INT; switch (icon_type) { case GIMP_ICON_TYPE_ICON_NAME: case GIMP_ICON_TYPE_IMAGE_FILE: icon_data_length = -1; if (! gimp_scanner_parse_string_no_validate (scanner, &icon_name)) return G_TOKEN_STRING; icon_data = (guint8 *) icon_name; break; case GIMP_ICON_TYPE_INLINE_PIXBUF: if (icon_data_length < 0) return G_TOKEN_STRING; if (! gimp_scanner_parse_data (scanner, icon_data_length, &icon_data)) return G_TOKEN_STRING; break; } gimp_plug_in_procedure_take_icon (proc, icon_type, icon_data, icon_data_length); if (! gimp_scanner_parse_token (scanner, G_TOKEN_RIGHT_PAREN)) return G_TOKEN_RIGHT_PAREN; return G_TOKEN_LEFT_PAREN; }