Because you are using strtok() incorrectly....
bleh... I hate writing out examples for people like this...
Example:
Code:
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
/* First let me copy and paste my data structure from an earlier post */
enum E_VARIABLE_TYPE =
{
EVT_UNK,
EVT_INT,
EVT_STR,
EVT_FLT
};
struct variable
{
enum E_VARIABLE_TYPE type;
const char *name;
union
{
const char *str_val;
int int_val;
float flt_val;
}; /* This isn't standard as far as I am aware. But it may work for you.
* If not, just resolve the anonymous union error your compiler throws up.
*/
/* Let me make this a linked list for simplicity. */
struct variable *next;
};
struct variable *parse(char *buffer)
{
struct variable *head = malloc(sizeof(*head)), *node = head;
if(!head)
return 0;
for(buffer = strtok(buffer, ";"); buffer; strtok(NULL, "';"))
{
if(!node)
{
fputs("Cannot complete the parsing! Out of memory!\n", stderr);
return head;
}
node->name = buffer;
node->next = malloc(sizeof(*node));
node = node->next;
}
if(node)
node->next = 0;
if(head)
/* Now parse each segment of data */
for(node = head; node; node = node->next)
{
char *data = strchr(node->name, '=');
if(data)
{
*data++ = 0;
node->str_val = data;
node->type = EVT_INT;
/* Now the type needs to be determined */
for(;*data;++data)
{
if(isalpha(*data))
{
node->type = EVT_STR;
break;
}
if(ispunct(*data))
{
node->type = EVT_FLT;
}
}
switch(node->type)
{
case EVT_INT:
node->int_val = strtol(node->str_val, NULL, "10");
break;
case EVT_FLT:
node->flt_val = atof(node->str_val);
break;
}
}
}
return head;
}
//[EDIT]
/* For the hell of it... */
void revert(char *buffer, size_t len, struct variable *node)
{
size_t offset;
for(;node; node = node->next)
{
switch(node->type)
{
case EVT_INT:
offset = snprintf(buffer, len, "%s=%d;", node->name, node->int_val);
break;
case EVT_FLT:
offset = snprintf(buffer, len, "%s=%f;", node->name, node->flt_val);
break;
case EVT_STR:
offset = snprintf(buffer, len, "%s=%s;", node->name, node->str_val);
break;
}
buffer += offset;
len -= offset;
}
}
//[/EDIT]
There are many imperfections in this code... It doesn't handle hex values (which it easily could). It doesn't necessarily handle negative ints correctly, which again, its an easy fix. But its a start.