Skip to content
Snippets Groups Projects
Commit f5e15221 authored by edgrif's avatar edgrif
Browse files

add print func for hashlist package + fix bad bug when inserting.

parent 9efbd646
No related branches found
No related tags found
No related merge requests found
......@@ -26,9 +26,9 @@
* glib but not included with their distribution.
*
* HISTORY:
* Last edited: Apr 16 08:52 2009 (edgrif)
* Last edited: Apr 22 11:51 2009 (edgrif)
* Created: Thu Oct 13 15:56:54 2005 (edgrif)
* CVS info: $Id: zmapGLibUtils.h,v 1.20 2009-04-16 09:05:19 edgrif Exp $
* CVS info: $Id: zmapGLibUtils.h,v 1.21 2009-04-22 16:25:00 edgrif Exp $
*-------------------------------------------------------------------
*/
#ifndef ZMAP_GLIBUTILS_H
......@@ -86,8 +86,8 @@ GHashTable *zMap_g_hashlist_create(void) ;
gboolean zMap_g_string_replace(GString *string, char *target, char *source) ;
void zMap_g_hashlist_insert(GHashTable *hashlist, GQuark key, gpointer value) ;
void zMap_g_hashlist_insert(GHashTable *hashlsit, GQuark key, gpointer value) ;
GHashTable *zMap_g_hashlist_copy(GHashTable *orig_hashlist) ;
void zMap_g_hashlist_print(GHashTable *hashlist) ;
void zMap_g_hashlist_destroy(GHashTable *hashlist) ;
......
......@@ -26,9 +26,9 @@
*
* Exported functions: See ZMap/zmapGLibUtils.h
* HISTORY:
* Last edited: Apr 16 08:54 2009 (edgrif)
* Last edited: Apr 22 17:23 2009 (edgrif)
* Created: Thu Oct 13 15:22:35 2005 (edgrif)
* CVS info: $Id: zmapGLibUtils.c,v 1.27 2009-04-16 09:05:19 edgrif Exp $
* CVS info: $Id: zmapGLibUtils.c,v 1.28 2009-04-22 16:25:00 edgrif Exp $
*-------------------------------------------------------------------
*/
......@@ -83,6 +83,7 @@ static void get_first_datalist_key(GQuark id, gpointer data, gpointer user_data)
static gboolean getNthHashElement(gpointer key, gpointer value, gpointer user_data) ;
static void hashCopyListCB(gpointer key, gpointer value, gpointer user_data) ;
static void hashPrintListCB(gpointer key, gpointer value, gpointer user_data_unused) ;
static void destroyList(gpointer data) ;
......@@ -519,6 +520,12 @@ gpointer zMap_g_hash_table_nth(GHashTable *hash_table, int nth)
* construct and look at his list hence these functions.
*
* Copy function could easily be extended to allow deep list copying with a callback.
*
* NOTE WELL: There is a destroy function for the hash values and this WILL get
* called by g_hash_table for inserts/replaces. Below you will see we copy lists
* because as soon as we reinsert the list the old copy will be deleted by the
* delete callback.
*
* */
......@@ -540,9 +547,13 @@ void zMap_g_hashlist_insert(GHashTable *hashlist, GQuark key, gpointer value)
/* Slightly tricky coding, if we don't find a list the append creates a new list,
* otherwise we append to the existing list. Either way we then _always_ replace
* the list in the hash as list start in theory could change. */
* the list in the hash as list start in theory could change. BUT note that
* we must copy the list before we insert it because otherwise it is erased
* by g_hash_table calling the existing entries delete function. */
list = (GList *)g_hash_table_lookup(hashlist, GINT_TO_POINTER(key)) ;
list = g_list_copy(list) ;
list = g_list_append(list, value) ;
g_hash_table_insert(hashlist, GINT_TO_POINTER(key), list) ;
......@@ -564,6 +575,15 @@ GHashTable *zMap_g_hashlist_copy(GHashTable *orig_hashlist)
}
/*! Print out the featureset -> styles lists. */
void zMap_g_hashlist_print(GHashTable *hashlist)
{
g_hash_table_foreach(hashlist, hashPrintListCB, NULL) ;
return ;
}
void zMap_g_hashlist_destroy(GHashTable *hashlist)
{
g_hash_table_destroy(hashlist) ;
......@@ -944,6 +964,20 @@ static void hashCopyListCB(gpointer key, gpointer value, gpointer user_data)
/* A GHFunc() to print the list attached to a hash entry. */
static void hashPrintListCB(gpointer key, gpointer value, gpointer user_data_unused)
{
GList *styles_list = (GList *)value ;
printf("\"%s\": ", g_quark_to_string(GPOINTER_TO_INT(key))) ;
zMap_g_list_quark_print(styles_list, "Styles", FALSE) ;
return ;
}
/* A GDestroyNotify() to free Glists held as the data in a hash. */
static void destroyList(gpointer data)
{
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment