static gboolean notify_notification_set_hint_variant (NotifyNotification *notification, const char *type, const char *key, const char *value, GError **error) { static gboolean conv_error = FALSE; if (!strcasecmp (type, "string")) { notify_notification_set_hint_string (notification, key, value); } else if (!strcasecmp (type, "int")) { if (!g_ascii_isdigit (*value)) conv_error = TRUE; else { gint h_int = (gint) g_ascii_strtoull (value, NULL, 10); notify_notification_set_hint_int32 (notification, key, h_int); } } else if (!strcasecmp (type, "double")) { if (!g_ascii_isdigit (*value)) conv_error = TRUE; else { gdouble h_double = g_strtod (value, NULL); notify_notification_set_hint_double (notification, key, h_double); } } else if (!strcasecmp (type, "byte")) { gint h_byte = (gint) g_ascii_strtoull (value, NULL, 10); if (h_byte < 0 || h_byte > 0xFF) conv_error = TRUE; else { notify_notification_set_hint_byte (notification, key, (guchar) h_byte); } } else { *error = g_error_new (G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, N_("Invalid hint type \"%s\". Valid types " "are int, double, string and byte."), type); return FALSE; } if (conv_error) { *error = g_error_new (G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, N_("Value \"%s\" of hint \"%s\" could not be " "parsed as type \"%s\"."), value, key, type); return FALSE; } return TRUE; }
int notify_hints_type(ErlNifEnv *env, NotifyNotification *notify, int arity, ERL_NIF_TERM key, ERL_NIF_TERM value) { char a_key[256] = {0}; union { ErlNifBinary b; int i; double d; } val; const ERL_NIF_TERM *byte = NULL; char a_byte[256] = {0}; int len = 0; if (!enif_get_atom(env, key, a_key, sizeof(a_key), ERL_NIF_LATIN1)) return -1; if (enif_get_int(env, value, &val.i)) notify_notification_set_hint_int32(notify, a_key, val.i); else if (enif_get_double(env, value, &val.d)) notify_notification_set_hint_double(notify, a_key, val.d); else if (enif_get_tuple(env, value, &len, &byte)) { if (len != 2 || !enif_get_atom(env, byte[0], a_byte, sizeof(a_byte), ERL_NIF_LATIN1) || (strcmp(a_byte, "byte") != 0) || !enif_get_int(env, byte[1], &val.i)) return -1; notify_notification_set_hint_byte(notify, a_key, (u_int8_t)val.i); } else if (arity == 0 || enif_inspect_iolist_as_binary(env, value, &val.b)) { gchar *tmpstr = NULL; if (arity > 0) tmpstr = stralloc(&val.b); notify_notification_set_hint_string(notify, a_key, (arity > 0 ? "" : tmpstr)); strfree(tmpstr); } else return -1; return 0; }
int notify_hints_type(ErlNifEnv *env, NotifyNotification *notify, int arity, ERL_NIF_TERM key, ERL_NIF_TERM value) { char s_key[1024] = {0}; char s_value[1024] = {0}; int i_value = 0; double d_value = 0; const ERL_NIF_TERM *byte = NULL; char s_byte[256] = {0}; int len = 0; if (!enif_get_string(env, key, s_key, sizeof(s_key), ERL_NIF_LATIN1)) return -1; if (enif_get_int(env, value, &i_value)) notify_notification_set_hint_int32(notify, s_key, (value == atom_undefined ? 0 : i_value)); else if (enif_get_double(env, value, &d_value)) notify_notification_set_hint_double(notify, s_key, (value == atom_undefined ? 0 : d_value)); else if (enif_get_tuple(env, value, &len, &byte)) { if ( (len != 2) || !enif_get_atom(env, byte[0], s_byte, sizeof(s_byte), ERL_NIF_LATIN1) || (strcmp(s_byte, "byte") != 0) || !enif_get_int(env, byte[1], &i_value)) return -1; notify_notification_set_hint_byte(notify, s_key, (value == atom_undefined ? 0 : (u_int8_t)i_value)); } else if ((arity == 0) || enif_get_string(env, value, s_value, sizeof(s_value), ERL_NIF_LATIN1)) notify_notification_set_hint_string(notify, s_key, (value == atom_undefined ? "" : s_value)); else return -1; return 0; }