I am trying to write a function which converts a string into a GSList. The string has the following format : "a_stuff_d_123..." where the letters define the type of the GSList element to be created (a for string, d for integer. I only need these two types) and everything after "_" the value. For example, here the first element will be the "stuff" string, the second element will be the integer 123 and so on.
Here is what I've got so far:
Expand|Select|Wrap|Line Numbers
- GSList * string_to_GSList(gchar* string){
- char *temp;
- GSList *l = NULL;
- temp = strtok(string, "_");
- if(!strcmp(temp, "a")){
- temp = strtok(NULL, "_");
- l = g_slist_append(l, temp);
- }
- else if(!strcmp(temp, "d")){
- temp = strtok(NULL, "_");
- l = g_slist_append(l, GINT_TO_POINTER(atoi(temp)));
- }
- temp = strtok(NULL, "_");
- while( temp != NULL){
- if(!strcmp(temp, "a")){
- temp = strtok(NULL, "_");
- l = g_slist_append(l, temp);
- }
- else if(!strcmp(temp, "d")){
- temp = strtok(NULL, "_");
- l = g_slist_append(l, GINT_TO_POINTER(atoi(temp)));
- }
- temp = strtok(NULL, "_");
- }
- return l;
- }
Expand|Select|Wrap|Line Numbers
- strlist = strdup("a_one_d_1233_a_two_d_244_a_three_d_3_a_four_d_455_ a_five_d_566");
- list = string_to_GSList(strlist);
Any suggestions?
Thanks in advance.. :)