libmm-glib: use helper function to convert enum/flags from string

This commit is contained in:
Thomas Haller
2018-10-04 09:30:42 +02:00
committed by Aleksander Morgado
parent d83f50794d
commit 8bde29c9e2

View File

@@ -25,24 +25,64 @@
#include "mm-errors-types.h" #include "mm-errors-types.h"
#include "mm-common-helpers.h" #include "mm-common-helpers.h"
static int static gint
_enum_class_return_value (GEnumClass *enum_class, guint i) _enum_from_string (GType type,
const gchar *str,
gint error_value,
GError **error)
{ {
int value; GEnumClass *enum_class;
gint value;
guint i;
value = enum_class->values[i].value; enum_class = G_ENUM_CLASS (g_type_class_ref (type));
g_type_class_unref (enum_class);
return value; for (i = 0; enum_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, enum_class->values[i].value_nick)) {
value = enum_class->values[i].value;
g_type_class_unref (enum_class);
return value;
}
}
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid %s value",
str,
g_type_name (type));
g_type_class_unref (enum_class);
return error_value;
} }
static int static guint
_flags_class_return_value (GFlagsClass *flags_class, guint i) _flags_from_string (GType type,
const gchar *str,
guint error_value,
GError **error)
{ {
guint value; GFlagsClass *flags_class;
guint value;
guint i;
value = flags_class->values[i].value; flags_class = G_FLAGS_CLASS (g_type_class_ref (type));
g_type_class_unref (flags_class);
return value; for (i = 0; flags_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, flags_class->values[i].value_nick)) {
value = flags_class->values[i].value;
g_type_class_unref (flags_class);
return value;
}
}
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid %s value",
str,
g_type_name (type));
g_type_class_unref (flags_class);
return error_value;
} }
gchar * gchar *
@@ -808,24 +848,10 @@ MMModem3gppEpsUeModeOperation
mm_common_get_eps_ue_mode_operation_from_string (const gchar *str, mm_common_get_eps_ue_mode_operation_from_string (const gchar *str,
GError **error) GError **error)
{ {
GEnumClass *enum_class; return _enum_from_string (MM_TYPE_MODEM_3GPP_EPS_UE_MODE_OPERATION,
guint i; str,
MM_MODEM_3GPP_EPS_UE_MODE_OPERATION_UNKNOWN,
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_MODEM_3GPP_EPS_UE_MODE_OPERATION)); error);
for (i = 0; enum_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, enum_class->values[i].value_nick))
return _enum_class_return_value (enum_class, i);
}
g_type_class_unref (enum_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMModem3gppEpsUeModeOperation value",
str);
return MM_MODEM_3GPP_EPS_UE_MODE_OPERATION_UNKNOWN;
} }
GArray * GArray *
@@ -929,48 +955,20 @@ MMModemCdmaRmProtocol
mm_common_get_rm_protocol_from_string (const gchar *str, mm_common_get_rm_protocol_from_string (const gchar *str,
GError **error) GError **error)
{ {
GEnumClass *enum_class; return _enum_from_string (MM_TYPE_MODEM_CDMA_RM_PROTOCOL,
guint i; str,
MM_MODEM_CDMA_RM_PROTOCOL_UNKNOWN,
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_MODEM_CDMA_RM_PROTOCOL)); error);
for (i = 0; enum_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, enum_class->values[i].value_nick))
return _enum_class_return_value (enum_class, i);
}
g_type_class_unref (enum_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMModemCdmaRmProtocol value",
str);
return MM_MODEM_CDMA_RM_PROTOCOL_UNKNOWN;
} }
MMBearerIpFamily MMBearerIpFamily
mm_common_get_ip_type_from_string (const gchar *str, mm_common_get_ip_type_from_string (const gchar *str,
GError **error) GError **error)
{ {
GFlagsClass *flags_class; return _flags_from_string (MM_TYPE_BEARER_IP_FAMILY,
guint i; str,
MM_BEARER_IP_FAMILY_NONE,
flags_class = G_FLAGS_CLASS (g_type_class_ref (MM_TYPE_BEARER_IP_FAMILY)); error);
for (i = 0; flags_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, flags_class->values[i].value_nick))
return _flags_class_return_value (flags_class, i);
}
g_type_class_unref (flags_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMBearerIpFamily value",
str);
return MM_BEARER_IP_FAMILY_NONE;
} }
MMBearerAllowedAuth MMBearerAllowedAuth
@@ -1027,144 +1025,60 @@ MMSmsStorage
mm_common_get_sms_storage_from_string (const gchar *str, mm_common_get_sms_storage_from_string (const gchar *str,
GError **error) GError **error)
{ {
GEnumClass *enum_class; return _enum_from_string (MM_TYPE_SMS_STORAGE,
guint i; str,
MM_SMS_STORAGE_UNKNOWN,
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_SMS_STORAGE)); error);
for (i = 0; enum_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, enum_class->values[i].value_nick))
return _enum_class_return_value (enum_class, i);
}
g_type_class_unref (enum_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMSmsStorage value",
str);
return MM_SMS_STORAGE_UNKNOWN;
} }
MMSmsCdmaTeleserviceId MMSmsCdmaTeleserviceId
mm_common_get_sms_cdma_teleservice_id_from_string (const gchar *str, mm_common_get_sms_cdma_teleservice_id_from_string (const gchar *str,
GError **error) GError **error)
{ {
GEnumClass *enum_class; return _enum_from_string (MM_TYPE_SMS_CDMA_TELESERVICE_ID,
guint i; str,
MM_SMS_CDMA_TELESERVICE_ID_UNKNOWN,
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_SMS_CDMA_TELESERVICE_ID)); error);
for (i = 0; enum_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, enum_class->values[i].value_nick))
return _enum_class_return_value (enum_class, i);
}
g_type_class_unref (enum_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMSmsCdmaTeleserviceId value",
str);
return MM_SMS_CDMA_TELESERVICE_ID_UNKNOWN;
} }
MMSmsCdmaServiceCategory MMSmsCdmaServiceCategory
mm_common_get_sms_cdma_service_category_from_string (const gchar *str, mm_common_get_sms_cdma_service_category_from_string (const gchar *str,
GError **error) GError **error)
{ {
GEnumClass *enum_class; return _enum_from_string (MM_TYPE_SMS_CDMA_SERVICE_CATEGORY,
guint i; str,
MM_SMS_CDMA_SERVICE_CATEGORY_UNKNOWN,
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_SMS_CDMA_SERVICE_CATEGORY)); error);
for (i = 0; enum_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, enum_class->values[i].value_nick))
return _enum_class_return_value (enum_class, i);
}
g_type_class_unref (enum_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMSmsCdmaServiceCategory value",
str);
return MM_SMS_CDMA_SERVICE_CATEGORY_UNKNOWN;
} }
MMCallDirection MMCallDirection
mm_common_get_call_direction_from_string (const gchar *str, mm_common_get_call_direction_from_string (const gchar *str,
GError **error) GError **error)
{ {
GEnumClass *enum_class; return _enum_from_string (MM_TYPE_CALL_DIRECTION,
guint i; str,
MM_CALL_DIRECTION_UNKNOWN,
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_CALL_DIRECTION)); error);
for (i = 0; enum_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, enum_class->values[i].value_nick))
return _enum_class_return_value (enum_class, i);
}
g_type_class_unref (enum_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMCallDirection value",
str);
return MM_CALL_DIRECTION_UNKNOWN;
} }
MMCallState MMCallState
mm_common_get_call_state_from_string (const gchar *str, mm_common_get_call_state_from_string (const gchar *str,
GError **error) GError **error)
{ {
GEnumClass *enum_class; return _enum_from_string (MM_TYPE_CALL_STATE,
guint i; str,
MM_CALL_STATE_UNKNOWN,
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_CALL_STATE)); error);
for (i = 0; enum_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, enum_class->values[i].value_nick))
return _enum_class_return_value (enum_class, i);
}
g_type_class_unref (enum_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMCallState value",
str);
return MM_CALL_STATE_UNKNOWN;
} }
MMCallStateReason MMCallStateReason
mm_common_get_call_state_reason_from_string (const gchar *str, mm_common_get_call_state_reason_from_string (const gchar *str,
GError **error) GError **error)
{ {
GEnumClass *enum_class; return _enum_from_string (MM_TYPE_CALL_STATE_REASON,
guint i; str,
MM_CALL_STATE_REASON_UNKNOWN,
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_CALL_STATE_REASON)); error);
for (i = 0; enum_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, enum_class->values[i].value_nick))
return _enum_class_return_value (enum_class, i);
}
g_type_class_unref (enum_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMCallStateReason value",
str);
return MM_CALL_STATE_REASON_UNKNOWN;
} }
MMOmaFeature MMOmaFeature
@@ -1221,24 +1135,10 @@ MMOmaSessionType
mm_common_get_oma_session_type_from_string (const gchar *str, mm_common_get_oma_session_type_from_string (const gchar *str,
GError **error) GError **error)
{ {
GEnumClass *enum_class; return _enum_from_string (MM_TYPE_OMA_SESSION_TYPE,
guint i; str,
MM_OMA_SESSION_TYPE_UNKNOWN,
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_OMA_SESSION_TYPE)); error);
for (i = 0; enum_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, enum_class->values[i].value_nick))
return _enum_class_return_value (enum_class, i);
}
g_type_class_unref (enum_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMOmaSessionType value",
str);
return MM_OMA_SESSION_TYPE_UNKNOWN;
} }
GVariant * GVariant *