From: Alex V. Myltsev Date: Tue, 11 Sep 2007 20:52:53 +0000 (+0400) Subject: Factor out the string_in_list function. X-Git-Url: http://git.altlinux.org/people/avm/packages/exo.git?p=exo.git;a=commitdiff_plain;h=5a99ac4b08257a5f5e44b75942ec6c20f53b9c39 Factor out the string_in_list function. --- diff --git a/exo/exo-mount/exo-mount-hal.c b/exo/exo-mount/exo-mount-hal.c index 0b5776a..cee60c6 100644 --- a/exo/exo-mount/exo-mount-hal.c +++ b/exo/exo-mount/exo-mount-hal.c @@ -145,6 +145,21 @@ exo_mount_hal_propagate_error (GError **error, } +gboolean +string_in_list(gchar * const *haystack, const gchar *needle) +{ + gint n; + + if (!haystack) + return FALSE; + + for (n=0; haystack[n]; ++n) { + if (!strcmp (haystack[n], needle)) + return TRUE; + } + return FALSE; +} + /** * exo_mount_hal_device_from_udi: @@ -167,7 +182,6 @@ exo_mount_hal_device_from_udi (const gchar *udi, gchar **volume_udis; gchar *volume_udi = NULL; gint n_volume_udis; - gint n; g_return_val_if_fail (udi != NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); @@ -215,10 +229,7 @@ err0: exo_mount_hal_propagate_error (error, &derror); } /* verify that we have a mountable device here */ - for (n = 0; interfaces[n] != NULL; ++n) - if (strcmp (interfaces[n], "org.freedesktop.Hal.Device.Volume") == 0) - break; - if (G_UNLIKELY (interfaces[n] == NULL)) + if (!string_in_list (interfaces, "org.freedesktop.Hal.Device.Volume")) { /* definitely not a device that we're able to mount, eject or unmount */ err1: g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Given device \"%s\" is not a volume or drive"), udi); @@ -313,7 +324,7 @@ exo_mount_hal_device_from_file (const gchar *file, gchar **interfaces; gchar **udis; gint n_udis; - gint n, m; + gint n; g_return_val_if_fail (g_path_is_absolute (file), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); @@ -347,12 +358,7 @@ exo_mount_hal_device_from_file (const gchar *file, continue; /* check if we have a mountable device here */ - for (m = 0; interfaces[m] != NULL; ++m) - if (strcmp (interfaces[m], "org.freedesktop.Hal.Device.Volume") == 0) - break; - - /* check if it's a usable device */ - if (interfaces[m] != NULL) + if (string_in_list (interfaces, "org.freedesktop.Hal.Device.Volume")) { libhal_free_string_array (interfaces); break; @@ -971,3 +977,4 @@ oom: g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM, g_strerror (ENOMEM)) return TRUE; } +/* vim:set et sw=4: */