Skip to content

Commit

Permalink
Make working with GError in more right way (like with exceptions).
Browse files Browse the repository at this point in the history
Signed-off-by: Slava Zanko <[email protected]>
  • Loading branch information
slavaz authored and aborodin committed Jul 22, 2014
1 parent a8d46f2 commit 512ad7d
Show file tree
Hide file tree
Showing 35 changed files with 625 additions and 493 deletions.
15 changes: 10 additions & 5 deletions lib/event/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <config.h>

#include "lib/global.h"
#include "lib/util.h"
#include "lib/event.h"

#include "internal.h"
Expand All @@ -49,10 +50,11 @@ GTree *mc_event_grouplist = NULL;
gboolean
mc_event_init (GError ** mcerror)
{
mc_return_val_if_error (mcerror, FALSE);

if (mc_event_grouplist != NULL)
{
g_propagate_error (mcerror,
g_error_new (MC_ERROR, 1, _("Event system already initialized")));
mc_propagate_error (mcerror, 1, "%s", _("Event system already initialized"));
return FALSE;
}

Expand All @@ -62,8 +64,7 @@ mc_event_init (GError ** mcerror)

if (mc_event_grouplist == NULL)
{
g_propagate_error (mcerror,
g_error_new (MC_ERROR, 2, _("Failed to initialize event system")));
mc_propagate_error (mcerror, 2, "%s", _("Failed to initialize event system"));
return FALSE;
}

Expand All @@ -75,9 +76,11 @@ mc_event_init (GError ** mcerror)
gboolean
mc_event_deinit (GError ** mcerror)
{
mc_return_val_if_error (mcerror, FALSE);

if (mc_event_grouplist == NULL)
{
g_propagate_error (mcerror, g_error_new (MC_ERROR, 1, _("Event system not initialized")));
mc_propagate_error (mcerror, 3, "%s", _("Event system not initialized"));
return FALSE;
}

Expand All @@ -93,6 +96,8 @@ mc_event_mass_add (event_init_t * events, GError ** mcerror)
{
size_t array_index;

mc_return_val_if_error (mcerror, FALSE);

for (array_index = 0; events[array_index].event_group_name != NULL; array_index++)
{
if (!mc_event_add (events[array_index].event_group_name,
Expand Down
21 changes: 11 additions & 10 deletions lib/event/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <config.h>

#include "lib/global.h"
#include "lib/util.h"
#include "lib/event.h"

#include "internal.h"
Expand Down Expand Up @@ -68,12 +69,12 @@ mc_event_add (const gchar * event_group_name, const gchar * event_name,
GPtrArray *callbacks;
mc_event_callback_t *cb;

mc_return_val_if_error (mcerror, FALSE);

if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
|| event_callback == NULL)
{
g_propagate_error (mcerror,
g_error_new (MC_ERROR, 1,
_("Check input data! Some of parameters are NULL!")));
mc_propagate_error (mcerror, 1, "%s", _("Check input data! Some of parameters are NULL!"));
return FALSE;
}

Expand Down Expand Up @@ -159,6 +160,8 @@ mc_event_get_event_group_by_name (const gchar * event_group_name, gboolean creat
{
GTree *event_group;

mc_return_val_if_error (mcerror, FALSE);

event_group = (GTree *) g_tree_lookup (mc_event_grouplist, (gconstpointer) event_group_name);
if (event_group == NULL && create_new)
{
Expand All @@ -169,10 +172,8 @@ mc_event_get_event_group_by_name (const gchar * event_group_name, gboolean creat
(GDestroyNotify) mc_event_group_destroy_value);
if (event_group == NULL)
{
g_propagate_error (mcerror,
g_error_new (MC_ERROR, 1,
_("Unable to create group '%s' for events!"),
event_group_name));
mc_propagate_error (mcerror, 1, _("Unable to create group '%s' for events!"),
event_group_name);
return NULL;
}
g_tree_insert (mc_event_grouplist, g_strdup (event_group_name), (gpointer) event_group);
Expand All @@ -188,15 +189,15 @@ mc_event_get_event_by_name (GTree * event_group, const gchar * event_name, gbool
{
GPtrArray *callbacks;

mc_return_val_if_error (mcerror, FALSE);

callbacks = (GPtrArray *) g_tree_lookup (event_group, (gconstpointer) event_name);
if (callbacks == NULL && create_new)
{
callbacks = g_ptr_array_new ();
if (callbacks == NULL)
{
g_propagate_error (mcerror,
g_error_new (MC_ERROR, 1,
_("Unable to create event '%s'!"), event_name));
mc_propagate_error (mcerror, 1, _("Unable to create event '%s'!"), event_name);
return NULL;
}
g_tree_insert (event_group, g_strdup (event_name), (gpointer) callbacks);
Expand Down
29 changes: 29 additions & 0 deletions lib/glibcompat.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,32 @@ g_list_free_full (GList * list, GDestroyNotify free_func)
#endif /* ! GLIB_CHECK_VERSION (2, 28, 0) */

/* --------------------------------------------------------------------------------------------- */
#if ! GLIB_CHECK_VERSION (2, 22, 0)
/**
* Creates a new GError with the given domain and code, and a message formatted with format.
* @param domain error domain
* @param code error code
* @param format printf()-style format for error message
* @param args va_list of parameters for the message format
* @returns a new GError
*/

GError *
g_error_new_valist (GQuark domain, gint code, const gchar * format, va_list args)
{
char *message;
GError *ret_value;

va_start (ap, format);
message = g_strdup_vprintf (format, ap);
va_end (ap);

ret_value = g_error_new_literal (domain, code, message);
g_free (message);

return ret_value;
}

#endif /* ! GLIB_CHECK_VERSION (2, 22, 0) */

/* --------------------------------------------------------------------------------------------- */
28 changes: 16 additions & 12 deletions lib/mcconfig/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mc_config_t *mc_panels_config;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

static gboolean
mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, GError ** error)
mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
{
gchar *data, *written_data;
gsize len, total_written;
Expand All @@ -57,10 +57,12 @@ mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path,
ssize_t cur_written;
vfs_path_t *ini_vpath;

mc_return_val_if_error (mcerror, FALSE);

data = g_key_file_to_data (mc_config->handle, &len, NULL);
if (!exist_file (ini_path))
{
ret = g_file_set_contents (ini_path, data, len, error);
ret = g_file_set_contents (ini_path, data, len, mcerror);
g_free (data);
return ret;
}
Expand All @@ -73,7 +75,7 @@ mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path,

if (fd == -1)
{
g_propagate_error (error, g_error_new (MC_ERROR, 0, "%s", unix_error_string (errno)));
mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
g_free (data);
return FALSE;
}
Expand All @@ -88,7 +90,7 @@ mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path,
if (cur_written == -1)
{
mc_util_restore_from_backup_if_possible (ini_path, "~");
g_propagate_error (error, g_error_new (MC_ERROR, 0, "%s", unix_error_string (errno)));
mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
return FALSE;
}

Expand Down Expand Up @@ -261,25 +263,27 @@ mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path, gboolean r
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

gboolean
mc_config_save_file (mc_config_t * mc_config, GError ** error)
mc_config_save_file (mc_config_t * mc_config, GError ** mcerror)
{
mc_return_val_if_error (mcerror, FALSE);

if (mc_config == NULL || mc_config->ini_path == NULL)
{
return FALSE;
}
return mc_config_new_or_override_file (mc_config, mc_config->ini_path, error);

return mc_config_new_or_override_file (mc_config, mc_config->ini_path, mcerror);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

gboolean
mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError ** error)
mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
{
mc_return_val_if_error (mcerror, FALSE);

if (mc_config == NULL)
{
return FALSE;
}
return mc_config_new_or_override_file (mc_config, ini_path, error);

return mc_config_new_or_override_file (mc_config, ini_path, mcerror);

}

Expand Down
Loading

0 comments on commit 512ad7d

Please sign in to comment.