Sscanf replacement for xilinx EDK

Apr 26, 2007 4 Replies

Dear All,



Does anyone know of a replacement for the standard C sscanf() function?



I need to use this function, but it takes a huge chunk memory, a little bit like printf(), and my system based on the spartan-3 starter kit board only has 16KB of memory.



Tanks



Jos=E9 Mariano



With some luck, you can find a smaller implementation. Otherwise you may be able to write your own scanf. It is always a good idea to create smaller implementations of standard C library functions instead of inventing your own library.

Reply to nico@nctdevpuntnl (punt=.) Bedrijven en winkels vindt U op www.adresboekje.nl

sscanf takes a huge chunk of memory because it does a lot of formatting. If your format statements can be limited to a small subset of the available data conversion types, for example %x or %d, you could reduce the code size considerably. What formats do you really need? Perhaps you could get away with atoi (ascii to integer) instead of using sscanf?

Here is a simple reduced version of sscanf I wrote years ago. Hope it helps!

// // Reduced version of scanf (%d, %x, %c, %n are supported) // %d dec integer (E.g.: 12) // %x hex integer (E.g.: 0xa0) // %b bin integer (E.g.: b1010100010) // %n hex, de or bin integer (e.g: 12, 0xa0, b1010100010) // %c any character // int rsscanf(const char* str, const char* format, ...) { va_list ap; int value, tmp; int count; int pos; char neg, fmt_code; const char* pf;

va_start(ap, format);

for (pf = format, count = 0; *format != 0 && *str != 0; format++, str+

+) { while (*format == ' ' && *format != 0) format++; if (*format == 0) break;

while (*str == ' ' && *str != 0) str++; if (*str == 0) break;

if (*format == '%') { format++; if (*format == 'n') { if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { fmt_code = 'x'; str += 2; } else if (str[0] == 'b') { fmt_code = 'b'; str++; } else fmt_code = 'd'; } else fmt_code = *format;

switch (fmt_code) { case 'x': case 'X': for (value = 0, pos = 0; *str != 0; str++, pos++) { if ('0'

Dear All,

Many tanks for your input. You all have been really helpful.

My special tanks to Zoltan. Your function is now working like a charm in my system.

Best Regards,

Jos=E9 Mariano

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required