Question

Can anyone point me to a source code file or to a package that has a good, reusable implementation of sprintf() in C which I can customize as per my own need?

An explanation on why I need it: Strings are not null terminated in my code (binary compatible). Therefore sprintf("%s") is useless unless I fix the code to understand how to render string.

Thanks to quinmars for pointing out that there is way to print string through %s without it being null terminated. Though it solves the requriement right now, I shall eventually need the sprintf (or snprintf) implementation for higher level functions which use variants. Out of other mentioned till now, it seems to me that SQLite implementation is the best. Thanks Doug Currie for pointing it out.

Was it helpful?

Solution

I haven't tried it, because I don't have a compiler here, but reading the man page, it looks like that you can pass a precision for '%s':

... If a precision is given, no null character need be present; if the precision is not specified, or is greater than the size of the array, the array must contain a terminating NUL character.

So have you tried to do something like that?

snprintf(buffer, sizeof(buffer), "%.*s", bstring_len, bstring);

As said I haven't test it, and if it works, it works of course only if you have no '\0'-byte inside of the string.

EDIT: I've tested it now and it works!

OTHER TIPS

You should really be looking for snprintf (sprintf respecting output buffer size); google suggests http://www.ijs.si/software/snprintf/.

There is a nice public domain implementation as part of SQLite here.

I agree with Dickon Reed that you want snprintf, which is included in the SQLite version.

I have used this guys source code. It is small, understandable and easy to modify(as opposed to glib & libc).

According to this link- http://www.programmingforums.org/thread12049.html :

If you have the full gcc distribution, the source for the C library (glib or libc) is one of the subdirectories that comes for the ride.

So you can look it up there. I don't know how helpful that will be...

The only reason I can think of for wanting to modify sprintf is to extend it, and the only reason for extending it is when you're on your way to writing some sort of parser.

If you are looking to create a parser for something like a coding language, XML, or really anything with a syntax, I suggest you look into Lexers and Parser Generators (2 of the most commonly used ones are Flex and Bison) which can pretty much write the extremely complex code for parsers for you (though the tools themselves are somewhat complex).

Otherwise, you can find the code for it in the source files that are included with Visual Studio (at least 2005 and 2008, others might have it, but those 2 definitely do).

snprintf from glibc is customizable via hook/handler mechanism

Just an idea... Example:

#include <stdio.h>
#include <string.h>
#include <stdarg.h>

int sprintf(char * str, const char * format, ... )    
{// Here you can redfine your input before continuing to compy with standard inputs
    va_list args;
    va_start(args, format);
    vsprintf(str,format, args);// This still uses standaes formating
    va_end(args);
    return 0;// Before return you can redefine it back if you want...
}
int main (void)
{
    char h[20];
    sprintf(h,"hei %d ",10);
    printf("t %s\n",h);
    getchar();
    return 0;
}

Look at Hanson's C Interfaces: Implementations and Techniques. It is an interesting book in that it is written using Knuth's Literate Programming techniques, and it specifically includes an extensible formatted I/O interface based on snprintf().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top