sscanf wrapping function to advance string pointer in C
- by Dusty
I have a function that makes a series of calls to sscanf() and then, after each, updates the string pointer to point to the first character not consumed by sscanf() like so:
if(sscanf(str, "%d%n", &fooInt, &length) != 1)
{
// error handling
}
str+=length;
In order to clean it up and avoid duplicating this several times over, i'd like to encapsulate this into a nice utility function that looks something like the following:
int newSscanf ( char ** str, const char * format, ...)
{
int rv;
int length;
char buf[MAX_LENGTH];
va_list args;
strcpy(buf, format);
strcat(buf, "%n");
va_start(args, format);
rv = vsscanf(*str, buf, args, &length);
va_end(args);
*str += length;
return rv;
}
Then I could simply the calls as below to remove the additional parameter/bookkeeping:
if(newSscanf(&str, "%d", &fooInt) != 1)
{
// error handling
}
Unfortunately, I can't find a way to append the &length parameter onto the end of the arg list directly or otherwise inside newSscanf(). Is there some way to work around this, or am I just as well off handling the bookkeeping by hand at each call?