Re: GList and Combo



On Sat, 23 Sep 2000, Heitham Omar wrote:
Hi,
I've written a function that splits text by spaces and adds them to a
GList:

GList *split_to_glist(char *s)
{
    GList *list = NULL;
    char *str = (char *) malloc(sizeof(s));
    int i, x;

    for (i = 0, x = 0; i <= strlen(s); i++, x++) {
        str[x] = s[i];
        if (str[x] == ' ' || i == strlen(s)) {
            str[x] = '\0';
here you add the '\0' to your list.
            list = g_list_append(list, str);
            memset(str, 0, sizeof(str));
            x = -1;
        }
    }
    free(str);

    return list;
}

If i use the following code the combo only ever gets filled with the
value
"GtkListItem".

char s[] = "Hello World";
GList *list = NULL;

list = split_to_glist(s);
gtk_combo_set_popdown_strings(GTK_COMBO(cbo), list);
            
Thanks in advance for any help.

here is a quick hack, that work for simple cases:

#include <gtk/gtk.h>


GList *
split_to_glist( gchar *s, GList *list )
{
    gchar *str = s;
    gchar *start = str;

    while( *str ){
        if( *str == ' ' ){
            list = g_list_append( list, g_strndup( start, str - start ));
            str++;
            start = str;
        }
        str++;
    }
    if( start != str )
        list = g_list_append( list, g_strndup( start, str - start ));
    return list;
}

void
output( gchar *word ){

    g_message( "words >>%s<<", word );
}

int
main(){

    char s[] = "Hello World, this is a little bigger test";
    GList *list = NULL;

    list = split_to_glist( s, list );
    list = g_list_first( list );
    g_list_foreach( list, (gpointer) output, list->data );
    return 0;

but remind, that it wont work fine, if you have more than one whitespace
between the words.

better you use a loop, which test with isalnum() or friends for a printable
char.
if it is a whitespace and the last was too, kick it. 
if it is a whitespace and the last wasnt, copy the word.
if it is a non-whitespace only go to the next char.
if it is nothing of all, give up ;)

hope that helps

detlef
-- 
|  Multipliciter Lingua  |
|  muli.sourceforge.net  |





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]