MESSAGE
DATE | 2002-04-24 |
FROM | Ruben I Safir
|
SUBJECT | Subject: [hangout] Re: GLIB Hash Data (fwd)
|
----- Forwarded message from Ruben I Safir -----
>From ruben-at-mrbrklyn.com Thu Apr 25 00:16:29 2002 Received: (from ruben-at-localhost) by www2.mrbrklyn.com (8.11.2/8.11.2/SuSE Linux 8.11.1-0.5) id g3P4GTd24911; Thu, 25 Apr 2002 00:16:29 -0400 From: Ruben I Safir Message-Id: <200204250416.g3P4GTd24911-at-www2.mrbrklyn.com> Subject: Re: GLIB Hash Data In-Reply-To: <20020424191909.A7800-at-mark.mielke.cc> from Mark Mielke at "Apr 24, 2002 07:19:10 pm" To: Mark Mielke Date: Thu, 25 Apr 2002 00:16:29 -0400 (EDT) CC: Ruben I Safir , Gtk-List X-Mailer: ELM [version 2.4ME+ PL60 (25)]
> Your example is useless.
The example is very simple. Take in data from STDIN and use it for hash keys and values. I have to create a new pointer every time to do this with the hash functions in glib. The only way I can think of doing this is with mallac. The STDIN has to go into a defined buffer which will point to the same emmory location every time. Even strcpy would be useless in this case without mallocing new memory by hand every time.
IF it sees it's a char * it should just take in the data.
Ruben
GLIB is doing exactly what you asked it to. > GLIB cannot possibly know, and would not want to know, the nature of > your data. You give it a pointer. How does it know the length of the > pointer? How does it know you are not passing it a structure, and > intend to have it copy the structure recursively? > > If you need it to do strcpy(), pass the key and value through g_strdup() > before storing. > > mark > > > On Wed, Apr 24, 2002 at 06:28:22PM -0400, Ruben I Safir wrote: > > I'm wondering how useful the hashing system can be ibn GLIB > > > > I tried this code, and it seems that instead of copying strings, it just copies the pointer value > > resulting in making it impossible to look through input and assigning it to key values, unless I'm > > doing something worng. This is just about useless > > > > #include > > #include > > #include > > > > GHashTable * hTable; > > > > guint HashFunction(gpointer key){ > > char *sKey; > > guint giHashValue = 0; > > int nIndex; > > > > sKey = key; > > if(key == NULL) return(0); > > > > for (nIndex = 0; nIndex < strlen(sKey); nIndex++){ > > giHashValue = (giHashValue << 4) + (giHashValue ^(guint) sKey[nIndex]); > > } > > return (giHashValue); > > } > > > > gint HashCompare( gpointer sName1, gpointer sName2){ > > return (!strcmp((char *)sName1, (char *) sName2)); > > } > > > > void print_hash(gpointer key, gpointer value, gpointer otherdata){ > > g_print ("Key: %s ==> Value: %s\n", (gchar *) key, (gchar *) value); > > } > > > > int main(int argc, char *argv[]){ > > gchar buff_key[255]; > > gchar buff_val[255]; > > hTable = g_hash_table_new(HashFunction, HashCompare); > > while(1){ > > if(scanf("%255s", buff_key) < 1 ) break; > > if(scanf("%255s", buff_val)< 1) break; > > g_print("%s %s\n", buff_key, buff_val); > > > > g_hash_table_insert(hTable, buff_key, buff_val); > > } > > > > > > g_hash_table_foreach(hTable, (GHFunc) print_hash, NULL); > > > > exit(1); > > } > > > > > > ruben-at-www2:~/gtk > ./hash > > One > > Two > > One Two > > Three > > Four > > Three Four > > Five > > Six > > Five Six > > > > Key: Five ==> Value: Six > > Key: Five ==> Value: Six > > Key: Five ==> Value: Six > > > > > > If it doesn't work like strcpy, it doesn't do much. > > > > -- > > __________________________ > > > > Brooklyn Linux Solutions > > __________________________ > > http://www.mrbrklyn.com - Consulting > > http://www.brooklynonline.com - For the love of Brooklyn > > http://www.nylxs.com - Leadership Development in Free Software > > http://www.nyfairuse.org - The foundation of Democracy > > http://www2.mrbrklyn.com/resources - Unpublished Archive or stories and articles from around the net > > http://www2.mrbrklyn.com/mp3/dr.mp3 - Imagine my surprise when I saw you... > > http://www2.mrbrklyn.com/downtown.html - See the New Downtown Brooklyn.... > > > > 1-718-382-5752 > > > > > > > > _______________________________________________ > > gtk-list mailing list > > gtk-list-at-gnome.org > > http://mail.gnome.org/mailman/listinfo/gtk-list > > -- > mark-at-mielke.cc/markm-at-ncf.ca/markm-at-nortelnetworks.com __________________________ > . . _ ._ . . .__ . . ._. .__ . . . .__ | Neighbourhood Coder > |\/| |_| |_| |/ |_ |\/| | |_ | |/ |_ | > | | | | | \ | \ |__ . | | .|. |__ |__ | \ |__ | Ottawa, Ontario, Canada > > One ring to rule them all, one ring to find them, one ring to bring them all > and in the darkness bind them... > > http://mark.mielke.cc/ >
----- End of forwarded message from Ruben I Safir ----- ____________________________ New Yorker Linux Users Scene Fair Use - because it's either fair use or useless....
|
|