M
Malcolm McLean
Here is a handy hint to extract the length of input consumed by scanf().
(Returns -1 on a bad match / memory failure).
static int int_sscanf(const char *src, const char *fmt)
{
char *fmt2;
int i;
int j =0;
int answer = -1;
fmt = malloc(strlen(fmt) * 2 + 3);
if(!fmt)
return -1;
for(i=0;fmt;i++)
{
if(fmt == '%')
{
if(fmt[i+1] == '%')
{
fmt2[j++] = '%';
fmt2[j++] = '%';
i++;
}
else if(fmt[i+1] == '*')
{
fmt2[j++] = '%';
fmt2[j++] = '*';
i++;
}
else
{
fmt2[j++] = '%';
fmt2[j++] = '*';
}
}
fmt2[j++] = fmt;
}
fmt2[j++] = '%';
fmt2[j++] = 'n';
fmt2[j++] = 0;
sscanf(src, fmt2, &answer);
free(fmt2);
return answer;
}
(Returns -1 on a bad match / memory failure).
static int int_sscanf(const char *src, const char *fmt)
{
char *fmt2;
int i;
int j =0;
int answer = -1;
fmt = malloc(strlen(fmt) * 2 + 3);
if(!fmt)
return -1;
for(i=0;fmt;i++)
{
if(fmt == '%')
{
if(fmt[i+1] == '%')
{
fmt2[j++] = '%';
fmt2[j++] = '%';
i++;
}
else if(fmt[i+1] == '*')
{
fmt2[j++] = '%';
fmt2[j++] = '*';
i++;
}
else
{
fmt2[j++] = '%';
fmt2[j++] = '*';
}
}
fmt2[j++] = fmt;
}
fmt2[j++] = '%';
fmt2[j++] = 'n';
fmt2[j++] = 0;
sscanf(src, fmt2, &answer);
free(fmt2);
return answer;
}