Ops, i have forgotten to attach the patch (lukily because i was about to send a broken and slow patch :-) I am also sending a patch to add g_tree_clear_nodes, that frees all nodes without deleting the tree. Thanks Maurizio Monge
Attachment:
patch_glib_foreach_range
Description: Binary data
/*
* compile with
* gcc gtreetest.c -o gtreetest `pkg-config glib-2.0 --cflags --libs`
*/
#include <glib.h>
#include <stdio.h>
gboolean print_key(gpointer key, gpointer value, gpointer data)
{
printf("%s", key);
return FALSE;
}
int main(int argc, char *argv[])
{
FILE *file;
gchar buf[1024];
GTree *tree;
if (argc < 2)
{
g_warning ("Usage: %s filename\n", argv[0]);
return 1;
}
file = fopen (argv[1], "r");
if (!file)
{
g_warning ("Cannot open %s\n", argv[1]);
return 1;
}
tree = g_tree_new( (GCompareFunc)g_ascii_strcasecmp );
while (fgets (buf, 1024, file))
g_tree_insert( tree, g_strdup(buf), NULL);
fclose (file);
while (1)
{
gchar buf2[1024];
printf("Enter one string to search from: \n");
fgets (buf, 1024, stdin);
printf("Enter another string to search to: \n");
fgets (buf2, 1024, stdin);
g_tree_foreach_range(tree, buf, buf2, print_key, NULL);
}
return 0;
}
Attachment:
patch_glib_clear_nodes
Description: Binary data