<commit_before>#include "e_connman_private.h"
#include <string.h>
#include <errno.h>

static Eina_Hash *elements = NULL;

typedef struct _E_Connman_Array E_Connman_Array;
typedef struct _E_Connman_Element_Pending E_Connman_Element_Pending;
typedef struct _E_Connman_Element_Call_Data E_Connman_Element_Call_Data;
typedef struct _E_Connman_Element_Property E_Connman_Element_Property;
typedef struct _E_Connman_Element_Listener E_Connman_Element_Listener;
typedef struct _E_Connman_Element_Dict_Entry E_Connman_Element_Dict_Entry;

struct _E_Connman_Array
{
   int type;
   Eina_Array *array;
};

struct _E_Connman_Element_Pending
{
   EINA_INLIST;
   DBusPendingCall *pending;
   void *data;
   E_DBus_Method_Return_Cb user_cb;
   void *user_data;
};

struct _E_Connman_Element_Call_Data
{
   E_Connman_Element *element;
   E_DBus_Method_Return_Cb cb;
   E_Connman_Element_Pending *pending;
   Eina_Inlist **p_list;
};

struct _E_Connman_Element_Property
{
   EINA_INLIST;
   const char *name;
   int type;
   union {
      bool boolean;
      const char *str;
      unsigned short u16;
      unsigned int u32;
      unsigned char byte;
      const char *path;
      void *variant;
      E_Connman_Array *array;
   } value;
};

struct _E_Connman_Element_Dict_Entry
{
   const char *name;
   int type;
   union {
      bool boolean;
      const char *str;
      unsigned short u16;
      unsigned int u32;
      unsigned char byte;
      const char *path;
   } value;
};

struct _E_Connman_Element_Listener
{
   EINA_INLIST;
   void (*cb)(void *data, const E_Connman_Element *element);
   void *data;
   void (*free_data)(void *data);
};


static void
_e_connman_element_event_no_free(void *data __UNUSED__, void *ev)
{
   E_Connman_Element *element = ev;
   e_connman_element_unref(element);
}

static void
e_connman_element_event_add(int event_type, E_Connman_Element *element)
{
   e_connman_element_ref(element);
   ecore_event_add
     (event_type, element, _e_connman_element_event_no_free, element);
}

static void
e_connman_element_call_dispatch_and_free(void *d, DBusMessage *msg, DBusError *err)
{
   E_Connman_Element_Call_Data *data = d;
   E_Connman_Element_Pending *pending;

   pending = data->pending;
   pending->pending = NULL;

   if (data->cb)
     data->cb(data->element, msg, err);

   if (pending->user_cb)
     pending->user_cb(pending->user_data, msg, err);

   pending->data = NULL;
   *data->p_list = eina_inlist_remove(*data->p_list, EINA_INLIST_GET(pending));
   free(pending);
   free(data);
}

static void
e_connman_element_pending_cancel_and_free(Eina_Inlist **pending)
{
   while (*pending)
     {
	E_Connman_Element_Pending *p = (E_Connman_Element_Pending *)*pending;
	DBusError err;

	dbus_pending_call_cancel(p->pending);

	dbus_error_init(&err);
	dbus_set_error(&err, "Canceled", "Pending method call was canceled.");
	e_connman_element_call_dispatch_and_free(p->data, NULL, &err);
	dbus_error_free(&err);
     }
}

void
e_connman_element_listener_add(E_Connman_Element *element, void (*cb)(void *data, const E_Connman_Element *element), const void *data, void (*free_data)(void *data))
{
   E_Connman_Element_Listener *l;

   if (!element)
     {
	ERR("safety check failed: element == NULL");
	goto error;
     }
   if (!cb)
     {
	ERR("safety check failed: cb == NULL");
	goto error;
     }

   l = malloc(sizeof(*l));
   if (!l)
     {
	ERR("could not allocate E_Connman_Element_Listener");
	goto error;
     }

   l->cb = cb;
   l->data = (void *)data;
   l->free_data = free_data;

   element->_listeners = eina_inlist_append
     (element->_listeners, EINA_INLIST_GET(l));

   return;

 error:
   if (free_data)
     free_data((void *)data);
}

void
e_connman_element_listener_del(E_Connman_Element *element, void (*cb)(void *data, const E_Connman_Element *element), const void *data)
{
   E_Connman_Element_Listener *l;

   EINA_SAFETY_ON_NULL_RETURN(element);<commit_msg>Use proper error handling functions<commit_after>#include "e_connman_private.h"
#include <string.h>
#include <errno.h>

static Eina_Hash *elements = NULL;

typedef struct _E_Connman_Array E_Connman_Array;
typedef struct _E_Connman_Element_Pending E_Connman_Element_Pending;
typedef struct _E_Connman_Element_Call_Data E_Connman_Element_Call_Data;
typedef struct _E_Connman_Element_Property E_Connman_Element_Property;
typedef struct _E_Connman_Element_Listener E_Connman_Element_Listener;
typedef struct _E_Connman_Element_Dict_Entry E_Connman_Element_Dict_Entry;

struct _E_Connman_Array
{
   int type;
   Eina_Array *array;
};

struct _E_Connman_Element_Pending
{
   EINA_INLIST;
   DBusPendingCall *pending;
   void *data;
   E_DBus_Method_Return_Cb user_cb;
   void *user_data;
};

struct _E_Connman_Element_Call_Data
{
   E_Connman_Element *element;
   E_DBus_Method_Return_Cb cb;
   E_Connman_Element_Pending *pending;
   Eina_Inlist **p_list;
};

struct _E_Connman_Element_Property
{
   EINA_INLIST;
   const char *name;
   int type;
   union {
      bool boolean;
      const char *str;
      unsigned short u16;
      unsigned int u32;
      unsigned char byte;
      const char *path;
      void *variant;
      E_Connman_Array *array;
   } value;
};

struct _E_Connman_Element_Dict_Entry
{
   const char *name;
   int type;
   union {
      bool boolean;
      const char *str;
      unsigned short u16;
      unsigned int u32;
      unsigned char byte;
      const char *path;
   } value;
};

struct _E_Connman_Element_Listener
{
   EINA_INLIST;
   void (*cb)(void *data, const E_Connman_Element *element);
   void *data;
   void (*free_data)(void *data);
};


static void
_e_connman_element_event_no_free(void *data __UNUSED__, void *ev)
{
   E_Connman_Element *element = ev;
   e_connman_element_unref(element);
}

static void
e_connman_element_event_add(int event_type, E_Connman_Element *element)
{
   e_connman_element_ref(element);
   ecore_event_add
     (event_type, element, _e_connman_element_event_no_free, element);
}

static void
e_connman_element_call_dispatch_and_free(void *d, DBusMessage *msg, DBusError *err)
{
   E_Connman_Element_Call_Data *data = d;
   E_Connman_Element_Pending *pending;

   pending = data->pending;
   pending->pending = NULL;

   if (data->cb)
     data->cb(data->element, msg, err);

   if (pending->user_cb)
     pending->user_cb(pending->user_data, msg, err);

   pending->data = NULL;
   *data->p_list = eina_inlist_remove(*data->p_list, EINA_INLIST_GET(pending));
   free(pending);
   free(data);
}

static void
e_connman_element_pending_cancel_and_free(Eina_Inlist **pending)
{
   while (*pending)
     {
	E_Connman_Element_Pending *p = (E_Connman_Element_Pending *)*pending;
	DBusError err;

	dbus_pending_call_cancel(p->pending);

	dbus_error_init(&err);
	dbus_set_error(&err, "Canceled", "Pending method call was canceled.");
	e_connman_element_call_dispatch_and_free(p->data, NULL, &err);
	dbus_error_free(&err);
     }
}

void
e_connman_element_listener_add(E_Connman_Element *element, void (*cb)(void *data, const E_Connman_Element *element), const void *data, void (*free_data)(void *data))
{
   E_Connman_Element_Listener *l;
   EINA_SAFETY_ON_FALSE_GOTO(element, error);
   EINA_SAFETY_ON_FALSE_GOTO(cb, error);

   l = malloc(sizeof(*l));
   if (!l)
     {
	ERR("could not allocate E_Connman_Element_Listener");
	goto error;
     }

   l->cb = cb;
   l->data = (void *)data;
   l->free_data = free_data;

   element->_listeners = eina_inlist_append
     (element->_listeners, EINA_INLIST_GET(l));

   return;

 error:
   if (free_data)
     free_data((void *)data);
}

void
e_connman_element_listener_del(E_Connman_Element *element, void (*cb)(void *data, const E_Connman_Element *element), const void *data)
{
   E_Connman_Element_Listener *l;

   EINA_SAFETY_ON_NULL_RETURN(element);
{'fraction_non_alphanumeric': 0.0978120978120978, 'fraction_numerical': 0.002059202059202059, 'mean_word_length': 4.63933236574746, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5919076', 'n_tokens_mistral': 2902, 'n_tokens_neox': 2838, 'n_words': 571}