Skip to content

Commit

Permalink
re-organize the GList utility functions
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.ic-s.nl/svn/dbmail/trunk/dbmail@2840 7b491191-dbf0-0310-aff6-d879d4d69008
  • Loading branch information
pjstevns committed Nov 17, 2007
1 parent 95567f7 commit ade6b23
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 132 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ COMMON = dbmail-user.c \
dbmail-mailbox.c \
config.c \
debug.c \
list.c \
dm_list.c \
db.c \
acl.c \
misc.c \
Expand Down
2 changes: 1 addition & 1 deletion check_dbmail_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ START_TEST(test_dbmail_list_dedup)

g_list_destroy(concat);

list = g_list_dedup(list);
list = g_list_dedup(list, (GCompareFunc)strcmp, TRUE);
concat = g_list_slices(list, 100);

fail_unless(strcmp("qux,baz,bar,foo", concat->data) == 0,
Expand Down
2 changes: 1 addition & 1 deletion dbmail-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ int do_show(const char * const name)

/* show all aliases with forwarding addresses */
aliases = auth_get_known_aliases();
aliases = g_list_dedup(aliases);
aliases = g_list_dedup(aliases, (GCompareFunc)strcmp, TRUE);
if (g_list_length(aliases) > 0) {
aliases = g_list_first(aliases);
while (aliases) {
Expand Down
2 changes: 1 addition & 1 deletion dbmail.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
#include <ldap.h>
#endif

#include "list.h"
#include "dm_list.h"
#include "memblock.h"
#include "dbmailtypes.h"
#include "debug.h"
Expand Down
120 changes: 113 additions & 7 deletions list.c → dm_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@
* functions to create lists and add/delete items */

#include "dbmail.h"


void g_list_destroy(GList *l)
{
l = g_list_first(l);
g_list_foreach(l,(GFunc)g_free,NULL);

l = g_list_first(l);
g_list_free(l);
}




/*
* return a list of strings (a,b,c,..N)
*/
Expand Down Expand Up @@ -83,7 +97,7 @@ GList *g_list_slices_u64(GList *list, unsigned limit)
return new;
}

GList *g_list_dedup_func(GList *list, GCompareFunc compare_func, int freeitems)
GList *g_list_dedup(GList *list, GCompareFunc compare_func, int freeitems)
{
char *lastdata = NULL;

Expand All @@ -104,15 +118,107 @@ GList *g_list_dedup_func(GList *list, GCompareFunc compare_func, int freeitems)
return g_list_first(list);
}

/* Given a _sorted_ list of _char *_ entries, removes duplicates and frees them. */
GList *g_list_dedup(GList *list)
GString * g_list_join(GList * list, const gchar * sep)
{
GString *string = g_string_new("");
if (sep == NULL)
sep="";
if (list == NULL)
return string;
list = g_list_first(list);
g_string_append_printf(string,"%s",(gchar *)list->data);
while((list = g_list_next(list))) {
g_string_append_printf(string,"%s%s", sep,(gchar *)list->data);
if (! g_list_next(list))
break;
}
return string;
}
GString * g_list_join_u64(GList * list, const gchar * sep)
{
u64_t *token;
GString *string = g_string_new("");
if (sep == NULL)
sep="";
if (list == NULL)
return string;
list = g_list_first(list);
token = (u64_t*)list->data;
g_string_append_printf(string,"%llu",*token);
while((list = g_list_next(list))) {
token = (u64_t*)list->data;
g_string_append_printf(string,"%s%llu", sep,*token);
if (! g_list_next(list))
break;
}
return string;
}

/*
* append a formatted string to a GList
*/
GList * g_list_append_printf(GList * list, const char * format, ...)
{
return g_list_dedup_func(list, (GCompareFunc)strcmp, TRUE);
va_list argp;
va_start(argp, format);
list = g_list_append(list, g_strdup_vprintf(format, argp));
va_end(argp);
return list;
}

/* Given a _sorted_ list of pointers to u64's, removes duplicates and frees the pointers to them. */
GList *g_list_dedup_u64_p(GList *list)

/*
* a and b are lists of char keys
* matching is done using func
* for each key in b, keys are copied into or removed from a and freed
*/

void g_list_merge(GList **a, GList *b, int condition, GCompareFunc func)
{
return g_list_dedup_func(list, (GCompareFunc)ucmp, TRUE);
gchar *t;

b = g_list_first(b);

if (condition == IMAPFA_ADD) {
while (b) {
t = (gchar *)b->data;
if (! g_list_find_custom(*(GList **)a, t, (GCompareFunc)func))
*(GList **)a = g_list_append(*(GList **)a, g_strdup(t));
if (! g_list_next(b))
break;
b = g_list_next(b);
}
}
if (condition == IMAPFA_REMOVE) {
GList *el = NULL;

while (b) {
*(GList **)a = g_list_first(*(GList **)a);
t = (gchar *)b->data;
if ((el = g_list_find_custom(*(GList **)a, t, (GCompareFunc)func)) != NULL) {
*(GList **)a = g_list_remove_link(*(GList **)a, el);
g_list_destroy(el);
}

if (! g_list_next(b))
break;
b = g_list_next(b);
}
}
if (condition == IMAPFA_REPLACE) {
g_list_destroy(*(GList **)a);
*(GList **)a = NULL;

while (b) {
t = (gchar *)b->data;
*(GList **)a = g_list_append(*(GList **)a, g_strdup(t));
if (! g_list_next(b))
break;
b = g_list_next(b);
}
}


}


10 changes: 9 additions & 1 deletion list.h → dm_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@

GList *g_list_slices(GList *list, unsigned limit);
GList *g_list_slices_u64(GList *list, unsigned limit);
GList *g_list_dedup(GList *list);
GList *g_list_dedup(GList *list, GCompareFunc compare_func, int freeitems);

GString * g_list_join(GList * list, const gchar * sep);
GString * g_list_join_u64(GList * list, const gchar * sep);
GList * g_list_append_printf(GList * list, const char * format, ...);

void g_list_destroy(GList *list);
void g_list_merge(GList **a, GList *b, int condition, GCompareFunc func);


#endif
2 changes: 1 addition & 1 deletion maintenance.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ int do_dangling_aliases(void)
qprintf("\nRemoving aliases with nonexistent delivery userid's...\n");

aliases = auth_get_known_aliases();
aliases = g_list_dedup(aliases);
aliases = g_list_dedup(aliases, (GCompareFunc)strcmp, TRUE);
aliases = g_list_first(aliases);
while (aliases) {
char deliver_to[21];
Expand Down
113 changes: 0 additions & 113 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,42 +437,6 @@ int zap_between(const char * const instring, signed char left, signed char right
*
*
*/
GString * g_list_join(GList * list, const gchar * sep)
{
GString *string = g_string_new("");
if (sep == NULL)
sep="";
if (list == NULL)
return string;
list = g_list_first(list);
g_string_append_printf(string,"%s",(gchar *)list->data);
while((list = g_list_next(list))) {
g_string_append_printf(string,"%s%s", sep,(gchar *)list->data);
if (! g_list_next(list))
break;
}
return string;
}
GString * g_list_join_u64(GList * list, const gchar * sep)
{
u64_t *token;
GString *string = g_string_new("");
if (sep == NULL)
sep="";
if (list == NULL)
return string;
list = g_list_first(list);
token = (u64_t*)list->data;
g_string_append_printf(string,"%llu",*token);
while((list = g_list_next(list))) {
token = (u64_t*)list->data;
g_string_append_printf(string,"%s%llu", sep,*token);
if (! g_list_next(list))
break;
}
return string;
}


GList * g_string_split(GString * string, const gchar * sep)
{
Expand All @@ -491,18 +455,6 @@ GList * g_string_split(GString * string, const gchar * sep)
g_strfreev(array);
return list;
}
/*
* append a formatted string to a GList
*/
GList * g_list_append_printf(GList * list, const char * format, ...)
{
va_list argp;
va_start(argp, format);
list = g_list_append(list, g_strdup_vprintf(format, argp));
va_end(argp);
return list;
}

char * g_strcasestr(const char *haystack, const char *needle)
{
// Like strstr, but case insensitive.
Expand Down Expand Up @@ -1104,16 +1056,6 @@ int num_from_imapdate(const char *date)

return atoi(datenum);
}

void g_list_destroy(GList *l)
{
l = g_list_first(l);
g_list_foreach(l,(GFunc)g_free,NULL);

l = g_list_first(l);
g_list_free(l);
}

static gboolean traverse_tree_keys(gpointer key, gpointer value UNUSED, GList **l)
{
*(GList **)l = g_list_prepend(*(GList **)l, key);
Expand Down Expand Up @@ -1290,61 +1232,6 @@ gint ucmp(const u64_t *a, const u64_t *b)
return 0;
return -1;
}

/*
* a and b are lists of char keys
* matching is done using func
* for each key in b, keys are copied into or removed from a and freed
*/

void g_list_merge(GList **a, GList *b, int condition, GCompareFunc func)
{
gchar *t;

b = g_list_first(b);

if (condition == IMAPFA_ADD) {
while (b) {
t = (gchar *)b->data;
if (! g_list_find_custom(*(GList **)a, t, (GCompareFunc)func))
*(GList **)a = g_list_append(*(GList **)a, g_strdup(t));
if (! g_list_next(b))
break;
b = g_list_next(b);
}
}
if (condition == IMAPFA_REMOVE) {
GList *el = NULL;

while (b) {
*(GList **)a = g_list_first(*(GList **)a);
t = (gchar *)b->data;
if ((el = g_list_find_custom(*(GList **)a, t, (GCompareFunc)func)) != NULL) {
*(GList **)a = g_list_remove_link(*(GList **)a, el);
g_list_destroy(el);
}

if (! g_list_next(b))
break;
b = g_list_next(b);
}
}
if (condition == IMAPFA_REPLACE) {
g_list_destroy(*(GList **)a);
*(GList **)a = NULL;

while (b) {
t = (gchar *)b->data;
*(GList **)a = g_list_append(*(GList **)a, g_strdup(t));
if (! g_list_next(b))
break;
b = g_list_next(b);
}
}


}

/* Read from instream until ".\r\n", discarding what is read. */
int discard_client_input(FILE * instream)
{
Expand Down
7 changes: 1 addition & 6 deletions misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,15 @@ int find_bounded(const char * const value, char left, char right,
int zap_between(const char * const instring, signed char left, signed char right,
char **outstring, size_t *outlen, size_t *zaplen);

GString * g_list_join(GList * list, const gchar * sep);
GString * g_list_join_u64(GList * list, const gchar * sep);
GList * g_string_split(GString * string, const gchar * sep);
GList * g_list_append_printf(GList * list, const char * format, ...);

char * g_strcasestr(const char *haystack, const char *needle);

gint ucmp(const u64_t *a, const u64_t *b);
void g_list_destroy(GList *list);
GList * g_tree_keys(GTree *tree);
GList * g_tree_values(GTree *tree);
void tree_dump(GTree *t);
int g_tree_merge(GTree *a, GTree *b, int condition);
void g_list_merge(GList **a, GList *b, int condition, GCompareFunc func);

char * dm_stresc(const char * from);
char * dm_strnesc(const char * from, size_t len);
Expand All @@ -131,7 +127,6 @@ int dm_sock_score(const char *base, const char *test);
int dm_sock_compare(const char *clientsock, const char *sock_allow, const char *sock_deny);
int dm_valid_format(const char *str);

GList * g_tree_keys(GTree *tree);

char *date_sql2imap(const char *sqldate);
char *date_imap2sql(const char *imapdate);
Expand Down

0 comments on commit ade6b23

Please sign in to comment.