4 * Copyright 2002, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
8 * \brief sprintf() implementation based on _formatted_write()
11 * \author Bernardo Innocenti <bernie@develer.com>
16 *#* Revision 1.8 2005/02/18 12:34:29 bernie
17 *#* Include <mware/pgm.h> explicitly for non-Harvard archs.
19 *#* Revision 1.7 2004/12/31 17:47:45 bernie
20 *#* Rename UNUSED() to UNUSED_ARG().
22 *#* Revision 1.6 2004/11/16 21:15:19 bernie
23 *#* Fix off-by-one bug in [v]snprintf().
25 *#* Revision 1.5 2004/10/03 18:54:36 bernie
26 *#* sprintf(): Fix a serious bug; snprintf(): New function.
28 *#* Revision 1.4 2004/08/25 14:12:09 rasky
29 *#* Aggiornato il comment block dei log RCS
31 *#* Revision 1.3 2004/06/27 15:20:26 aleph
32 *#* Change UNUSED() macro to accept two arguments: type and name;
33 *#* Add macro GNUC_PREREQ to detect GCC version during build;
34 *#* Some spacing cleanups and typo fix
36 *#* Revision 1.2 2004/06/03 11:27:09 bernie
37 *#* Add dual-license information.
40 #include <mware/formatwr.h>
41 #include <mware/pgm.h>
47 static void __str_put_char(char c, void *ptr)
50 * This Does not work on Code Warrior. Hmm...
51 * *(*((char **)ptr))++ = c;
58 static void __null_put_char(UNUSED_ARG(char, c), UNUSED_ARG(void *, ptr))
64 int PGM_FUNC(vsprintf)(char * str, const char * PGM_ATTR fmt, va_list ap)
68 result = PGM_FUNC(_formatted_write)(
69 fmt, (str ? __str_put_char : __null_put_char), &str, ap);
71 /* Terminate string */
78 int PGM_FUNC(sprintf)(char *str, const char * fmt, ...)
84 result = PGM_FUNC(vsprintf)(str, fmt, ap);
91 * State information for __sn_put_char()
100 * formatted_write() callback used [v]snprintf().
102 static void __sn_put_char(char c, void *ptr)
104 struct __sn_state *state = (struct __sn_state *)ptr;
114 int PGM_FUNC(vsnprintf)(char *str, size_t size, const char * PGM_ATTR fmt, va_list ap)
118 /* Make room for traling '\0'. */
121 struct __sn_state state;
125 result = PGM_FUNC(_formatted_write)(
126 fmt, (str ? __sn_put_char : __null_put_char), &state, ap);
128 /* Terminate string. */
136 int PGM_FUNC(snprintf)(char *str, size_t size, const char * fmt, ...)
142 result = PGM_FUNC(vsnprintf)(str, size, fmt, ap);