I want to extract some values in i-commas(") from lines like this:
<P k="9,0,1" vt="191" v="100.99936" z="" />
Example:
getCharVal ( cp, char "k=\"", 9)
where cp is a pointer the line above should return "9,0,1"
The function:
#define ENDTAG "/>"
#define ICOMMAS '"'
char * getCharVal ( char *cp, char *att, size_t s)
{
char * val;
char * endTagP;
if (cp == NULL)return NULL;
cp = strstr(cp, att)+strlen(att);
if (cp == NULL) return NULL;
char * endP = strchr(cp, ICOMMAS);
if (endP == NULL) return NULL;
endTagP = strstr(cp, ENDTAG);
if (endTagP == NULL) return NULL;
if (endP > endTagP) return NULL;
size_t valsize = endP - cp ;
if (valsize > s) return NULL;
val = malloc(valsize + 1);
memcpy (val, cp, valsize);
val[valsize]='\0';
cp = endTagP;
return val;
}
This code is ugly. Could anyone give me hints on how to write it better?