|
From: <cli...@li...> - 2007-05-16 16:27:26
|
Revision: 54
http://cligg.svn.sourceforge.net/cligg/?rev=54&view=rev
Author: sithhell
Date: 2007-05-16 09:27:24 -0700 (Wed, 16 May 2007)
Log Message:
-----------
well ... now it should really work. memory leaks still remain
Modified Paths:
--------------
src/bin/cligg.c
src/bin/cligg_mainloop.c
src/bin/cligg_modulehandler.c
src/lib/cligg_btree.c
src/lib/cligg_list.c
src/lib/cligg_list.h
Modified: src/bin/cligg.c
===================================================================
--- src/bin/cligg.c 2007-05-16 14:08:44 UTC (rev 53)
+++ src/bin/cligg.c 2007-05-16 16:27:24 UTC (rev 54)
@@ -33,6 +33,7 @@
* for discussion about the code use the mailing list or/and irc
*/
+static int quit_cligg(void *data);
int main(int argc, char **argv)
{
@@ -49,15 +50,19 @@
exit(EXIT_FAILURE);
/* TODO register basic events */
+ /* event for quitting softly */
+ cligg_register_event("quit_cligg", quit_cligg);
/* init module system */
ret = cligg_init_modulehandler();
if(!cligg_trigger_event("test", NULL))
- fprintf(stderr, "Error laoding module\n");
+ fprintf(stderr, "Error loading module\n");
cligg_trigger_event("load_module", "test");
+ cligg_trigger_event("quit_cligg", NULL);
+
/* TODO load the config */
/* TODO process the config */
@@ -67,12 +72,18 @@
/* starting the event loop */
cligg_main_begin();
/* MAGIC!! */
+ return quit_cligg(NULL);
+}
+
+static int quit_cligg(void *data)
+{
+ int ret;
ret = cligg_main_end();
-printf("blubb\n");
/* Clean up */
cligg_delete_handler();
- cligg_del_modulehandler();
+ cligg_del_modulehandler();
+ fprintf(stderr, "Good Bye!\n");
+
exit(ret);
}
-
Modified: src/bin/cligg_mainloop.c
===================================================================
--- src/bin/cligg_mainloop.c 2007-05-16 14:08:44 UTC (rev 53)
+++ src/bin/cligg_mainloop.c 2007-05-16 16:27:24 UTC (rev 54)
@@ -30,9 +30,11 @@
int
cligg_main_end(void)
{
- void *ret_val;
+ void *ret_val = NULL;
pthread_join(main_loop, &ret_val);
+ if(ret_val == NULL)
+ return EXIT_SUCCESS;
return (int)ret_val;
}
@@ -50,16 +52,13 @@
if(new == NULL)
return (void *)EXIT_FAILURE;
- /*printf("%s: %x\n", new->name, cb);*/
-
cb = cligg_lookup_event(new->name);
- /*printf("%s: %x\n", new->name, cb);*/
-
if(cb == NULL) continue;
if(!cb(new->data))
fprintf(stderr, "Event callback failed!\n");
+ if(strcmp(new->name, "quit_cligg") == 0) break;
}
return (void *)EXIT_SUCCESS;
Modified: src/bin/cligg_modulehandler.c
===================================================================
--- src/bin/cligg_modulehandler.c 2007-05-16 14:08:44 UTC (rev 53)
+++ src/bin/cligg_modulehandler.c 2007-05-16 16:27:24 UTC (rev 54)
@@ -50,7 +50,9 @@
int cligg_register_module(char *path)
{
+ int ret = TRUE;
cligg_module *new = cligg_module_new(path);
+
if(new == NULL) {
perror("Error loading module");
return FALSE;
@@ -70,7 +72,7 @@
if(!cligg_list_element_append(module_list, new)) {
perror("Error loading module");
- return FALSE;
+ ret = FALSE;
}
if(pthread_mutex_unlock(&module_lock) != 0) {
fprintf(stderr, "Couldn't unlock module lock!\n");
@@ -80,7 +82,7 @@
dlerror(); /* clear any existing errors */
- return TRUE;
+ return ret;
}
int cligg_unregister_module(cligg_module *module)
@@ -97,21 +99,20 @@
if(cligg_list_element_find(module_list, cmp, module) == -1) {
fprintf(stderr, "Couldn't delete module: Not found!\n");
ret = FALSE;
- goto jump;
}
+ else {
+ tmp = (cligg_module *)module_list->current->data;
- tmp = (cligg_module *)module_list->current->data;
+ dlclose(tmp->handle);
- dlclose(tmp->handle);
+ cligg_list_element_del_current(module_list);
+ }
- cligg_list_element_del_current(module_list);
-
if(pthread_mutex_unlock(&module_lock) != 0) {
fprintf(stderr, "Couldn't unlock module lock!\n");
cligg_module_del(tmp);
return FALSE;
}
-jump:
return ret;
}
Modified: src/lib/cligg_btree.c
===================================================================
--- src/lib/cligg_btree.c 2007-05-16 14:08:44 UTC (rev 53)
+++ src/lib/cligg_btree.c 2007-05-16 16:27:24 UTC (rev 54)
@@ -30,35 +30,33 @@
return tree;
}
+static void del(cligg_btree_element *, free_func_cb);
+
CLIGG void
cligg_btree_clear(cligg_btree *tree)
{
- cligg_btree_element *tmp;
cligg_list *tofree = cligg_list_new(NULL);
- /**
- * TODO put error handling code here ... i am lazy right now ...
- */
-
- if(!cligg_list_element_append(tofree, tree->root)) {
- /*error*/
+ if(tofree == NULL) {
+ perror("Couldn't clear tree");
+ return;
}
- while(!cligg_list_isempty(tofree)) {
- tmp = (cligg_btree_element *)cligg_list_element_get_first(tofree);
- cligg_list_element_del_first(tofree);
- if(!cligg_list_element_append(tofree, tmp->left)) {
- /*error*/
- }
- if(!cligg_list_element_append(tofree, tmp->right)) {
- /*error*/
- }
- if(tree->free_func)
- tree->free_func(tmp->data);
- free(tmp);
- }
+ del(tree->root, tree->free_func);
+
tree->size = 0;
}
+static void del(cligg_btree_element *dele, free_func_cb free_func)
+{
+ if(dele == NULL) return;
+ del(dele->left, free_func);
+ del(dele->right, free_func);
+
+ /*if(free_func)
+ free_func(dele->data);*/
+ free(dele);
+}
+
CLIGG void
cligg_btree_del(cligg_btree *tree)
{
@@ -172,10 +170,10 @@
tmp = tree->root;
while(tmp) {
switch (tree->cmp_func(tmp->data, data)) {
- case 1:
+ case -1:
tmp = tmp->right;
break;
- case -1:
+ case 1:
tmp = tmp->left;
break;
case 0:
Modified: src/lib/cligg_list.c
===================================================================
--- src/lib/cligg_list.c 2007-05-16 14:08:44 UTC (rev 53)
+++ src/lib/cligg_list.c 2007-05-16 16:27:24 UTC (rev 54)
@@ -119,17 +119,16 @@
new_element->prev = NULL;
/*append to list*/
- if(list->last) {
- list->last->next = new_element;
- new_element->prev = list->last;
- }
+ new_element->prev = list->last;
+ if(list->first == NULL)
+ list->first = new_element;
+
+ if(list->last != NULL)
+ list->last->next = new_element;
list->last = new_element;
+ new_element->next = NULL;
- if(list->first == NULL) {
- list->first = list->last;
- }
-
list->size++;
return TRUE;
}
@@ -154,12 +153,16 @@
/* prepend to list */
new_element->next = list->first;
- list->first = new_element;
-
- if(list->last == NULL) {
- list->last = list->first;
+ if(list->last == NULL)
+ list->last = new_element;
+
+ if(list->first != NULL) {
+ list->first->prev = new_element;
}
+ list->first = new_element;
+ new_element->prev = NULL;
+
list->size++;
return TRUE;
@@ -171,6 +174,7 @@
list1->last->next = list2->first;
list2->first->prev = list1->last;
list1->last = list2->last;
+ list1->size += list2->size;
return TRUE;
}
@@ -181,83 +185,48 @@
list2->last->next = list1->first;
list1->first = list2->first;
list2->last = list1->first;
+ list1->size += list2->size;
return TRUE;
}
/* delete an element from the list */
CLIGG int cligg_list_element_del_first(cligg_list *list)
{
- cligg_list_element *old;
-
- if(cligg_list_isempty(list)) {
- return FALSE;
- }
+ cligg_list_element *tmp;
+ int ret;
- if(list->first == NULL) {
- return FALSE;
- }
-
- if(list->current == list->first)
- list->current = NULL;
+ if(list == NULL) return FALSE;
+ tmp = list->current;
+ if((tmp = list->first))
+ tmp = NULL;
- old = list->first;
- if(list->free_func) {
- list->free_func(old->data);
- }
- list->first = old->next;
- if(list->first != NULL)
- list->first->prev = NULL;
- free(old);
-
- list->size--;
-
- return TRUE;
+ list->current = list->first;
+ ret = cligg_list_element_del_current(list);
+ list->current = tmp;
+ return ret;
}
CLIGG int cligg_list_element_del_last(cligg_list *list)
{
- cligg_list_element *old;
- cligg_list_element *previous;
+ cligg_list_element *tmp;
+ int ret;
- if(cligg_list_isempty(list)) {
- return FALSE;
- }
+ if(list == NULL) return FALSE;
+ tmp = list->current;
+ if((tmp = list->last))
+ tmp = NULL;
- if(list->last == NULL) {
- return FALSE;
- }
-
- if(list->current == list->last)
- list->current = NULL;
-
- old = list->last;
-
- if(list->free_func) {
- list->free_func(old->data);
- }
-
- previous = list->first;
- while(previous && previous->next != old) {
- previous = previous->next;
- }
-
- list->last = previous;
- if(previous) {
- previous->next = NULL;
- }
-
- free(old);
-
- list->size--;
-
- return TRUE;
+ list->current = list->last;
+ ret = cligg_list_element_del_current(list);
+ list->current = tmp;
+ return ret;
}
CLIGG int cligg_list_element_del_index(cligg_list *list, int index)
{
int i = 0;
cligg_list_element *old;
- cligg_list_element *previous;
+ cligg_list_element *tmp;
if(cligg_list_isempty(list)) {
return FALSE;
@@ -279,47 +248,41 @@
if(i == index) {
break;
}
- previous = old;
old = old->next;
i++;
}
- if(list->current == old)
- list->current = NULL;
+ tmp = list->current;
- previous->next = old->next;
- previous->next->prev = old->next->prev;
- if(list->free_func) {
- list->free_func(old->data);
- }
- free(old);
+ if(tmp == old)
+ tmp = NULL;
- return TRUE;
+ list->current = old;
+
+ i = cligg_list_element_del_current(list);
+ list->current = tmp;
+ return i;
}
CLIGG int cligg_list_element_del_current(cligg_list *list)
{
- cligg_list_element *tmp;
+ cligg_list_element *tmp = list->current;
if(list->current == NULL)
return FALSE;
- if(list->current == list->first)
- return cligg_list_element_del_first(list);
- if(list->current == list->last)
- return cligg_list_element_del_last(list);
+ if(list->current->prev != NULL)
+ list->current->prev->next = list->current->next;
+ else
+ list->first = list->current->next;
+ if(list->current->next != NULL)
+ list->current->next->prev = list->current->prev;
- tmp = list->current;
- list->current->next = list->current->next->next;
- list->current = list->current->next;
-
+ list->current = NULL;
if(list->free_func)
list->free_func(tmp->data);
-
- tmp->prev->next = tmp->next;
- tmp->next->prev = tmp->prev;
-
free(tmp);
+ list->size--;
return TRUE;
}
@@ -340,16 +303,23 @@
return list->last->data;
}
-CLIGG void* cligg_list_element_get_index(cligg_list *list, int index)
+CLIGG void* cligg_list_element_get_current(cligg_list *list)
{
+ if(cligg_list_isempty(list))
+ return NULL;
+ return list->current->data;
+}
+
+CLIGG int cligg_list_element_goto(cligg_list *list, int index)
+{
int i = 0;
cligg_list_element *tmp;
if(cligg_list_isempty(list)) {
- return NULL;
+ return FALSE;
}
if(index >= list->size || index < 0) {
- return NULL;
+ return FALSE;
}
tmp = list->first;
@@ -361,7 +331,8 @@
tmp = tmp->next;
i++;
}
- return tmp->data;
+ list->current = tmp;
+ return TRUE;
}
CLIGG int cligg_list_element_find(cligg_list *list, compare_func_cb compare_func, void *data)
@@ -370,26 +341,29 @@
cligg_list_element *tmp = NULL;
if(cligg_list_isempty(list)) {
- return -1;
+ return 0;
}
if(compare_func(list->last->data, data) == 0) {
list->current = list->last;
return list->size-1;
}
+ if(compare_func(list->first->data, data) == 0) {
+ list->current = list->first;
+ return 1;
+ }
tmp = list->first;
- while(tmp->next != list->last) {
- if(compare_func(tmp->data, data) == 0) {
- list->current = tmp;
- return i;
- }
+ while(tmp->next != NULL && (compare_func(tmp->data, data) == 0)) {
tmp = tmp->next;
i++;
}
- list->current = NULL;
- return -1;
+ list->current = tmp;
+
+ if(i > list->size)
+ return -1;
+ return i;
}
CLIGG int cligg_list_element_add_sorted(cligg_list *list, compare_func_cb compare_func, void *data)
Modified: src/lib/cligg_list.h
===================================================================
--- src/lib/cligg_list.h 2007-05-16 14:08:44 UTC (rev 53)
+++ src/lib/cligg_list.h 2007-05-16 16:27:24 UTC (rev 54)
@@ -87,14 +87,15 @@
/* get list element data*/
CLIGG void* cligg_list_element_get_first(cligg_list *);
CLIGG void* cligg_list_element_get_last(cligg_list *);
-CLIGG void* cligg_list_element_get_index(cligg_list *, int);
+CLIGG void* cligg_list_element_get_current(cligg_list *);
+CLIGG int cligg_list_element_goto(cligg_list *, int);
/* returns the index of the first element which matches data */
CLIGG int cligg_list_element_find(cligg_list *, compare_func_cb, void *);
/* sorted list stuff*/
/* please remember, sorting a list which is in an unsorted state is n*n, so i won't implement it here. *
* inserting an element in sorted mode is n, this is far from optimum, use another datatype, thanks. *
- * this is just for completion */
+ * this is just for completion, and not tested yet. */
CLIGG int cligg_list_element_add_sorted(cligg_list *, compare_func_cb , void *);
/** @} */
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|