I'm using gcc. For the conf->item.id I want the id's of objects be "names" and integers at the same time. To create the 'id' I parse a file of "name=value" pairs and the first 4 letters are used.
the actual code:
Code:
/* n is of the form "name=value" or "name{"
* copyName takes the "name" and puts it in i->id/name
*/
int copyName(ConfItem* i, char* n){
int k;
static char buf[32];
/* Gets the "name" part of "name=value" and stores in buf */
getIDPart(buf, n);
/* check if the "name" part looks like a hexnumber e.g. "0x3434" */
if(isHexStr(buf)){
/* use id specified */
sscanf(buf+2, "%x", &i->id);
} else {
/* if not use first 4 letters of "name" */
i->id = 0;
/* create id from first 4 letters */
for(k = 0; k < 4; k++){
if(buf[k] == 0)
break;
/* i->name & i->id share a union: The next line is not portable its either (3-k) or (k)
* depending on endianess */
i->name[3-k] = buf[k];
}
}
return 0;
}