Reputation: 8408
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.
Upvotes: 3
Views: 18613
Reputation: 132340
A small implementation originally authored by Marco Paland, and I have been maintaining it, fixing many bugs and adding missing functionality, in this repository: eyalroz/printf. It's ~1170 lines-of-code for full C99 sprintf/vsprintf/etc. compared to sqlite's 3993 (although SQLite could use a lot less; it includes this sqliteint.h
header with a lot of unrelated stuff; also, %a
is not yet supported, as are sub-normal doubles)
Upvotes: 0
Reputation: 11583
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!
Upvotes: 5
Reputation: 1079
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;
}
Upvotes: 1
Reputation: 7069
I have used this guys source code. It is small, understandable and easy to modify(as opposed to glib & libc).
Upvotes: 2
Reputation: 755094
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()
.
Upvotes: 0
Reputation: 2532
snprintf from glibc is customizable via hook/handler mechanism
Upvotes: 1
Reputation: 41220
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.
Upvotes: 3
Reputation: 7835
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).
Upvotes: 1
Reputation: 5241
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...
Upvotes: 1
Reputation: 3625
You should really be looking for snprintf (sprintf respecting output buffer size); google suggests http://www.ijs.si/software/snprintf/.
Upvotes: 3