GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Converting a space separated list into an array

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
William J Giddings
Familiar Face


Joined: 17 Jan 2010
Posts: 42

PostPosted: Sat Mar 13, 2010 9:07 am    Post subject: Converting a space separated list into an array Reply with quote

One little task that I was working on yesterday was converting a string in the form of a list space separated items, into a NULL terminated array. Ok, its simple in Tcl, but not so straight forward in C. Imagine this as:

Code: (Plaintext)
1
2
char *str;
str =  “but1 but2 but3”;


This needs to become the equivalent of:

Code: (Plaintext)
1
char *objnames[] = { "but1", “but2”, “but3”, NULL };


What is the most effective way of doing this?

I’ve thought of ‘tokenizing’ the source string and dynamically building an array, but then, there’s no assurance that each entry in the new array will have the same size. That is, instead of “but1 but2 but3” might, during run time, be “b1 but2 button3” or something similar.

The idea of to allow me to successfully complete the following line of code to complete the loading of portions of a builder UI xml file into a pre-existing container object. This is the relevant command.

Code: (Plaintext)
1
gtk_builder_add_objects_from_string ( builder, builder_ui, strlen ( builder_ui ) * sizeof ( gchar ), objnames, NULL );


Will


Last edited by William J Giddings on Sat Mar 13, 2010 9:48 am; edited 1 time in total
Back to top
owl102
GTK+ Geek


Joined: 23 May 2008
Posts: 68

PostPosted: Sat Mar 13, 2010 9:47 am    Post subject: Re: Converting a space separated list into an array Reply with quote

William J Giddings wrote:
What is the most effective way of doing this?


You could use g_strsplit_set( str, " ", -1 ) for this, see:

http://library.gnome.org/devel/glib/stable/glib-String-Utility-Functions.html#g-strsplit-set
Back to top
William J Giddings
Familiar Face


Joined: 17 Jan 2010
Posts: 42

PostPosted: Sat Mar 13, 2010 5:52 pm    Post subject: Problem Solved Reply with quote

Thanks for that! It was exactly what I was looking for.

WJG
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP