Add dual-license information.
[bertos.git] / mware / sprintf.c
1 /*!
2  * \file
3  * <!--
4  * Copyright (C) 2002,2004 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See devlib/README for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Bernardo Innocenti <bernie@develer.com>
11  *
12  * \brief Simple sprintf() implementation based on _formatted_write()
13  */
14
15 /*
16  * $Log$
17  * Revision 1.2  2004/06/03 11:27:09  bernie
18  * Add dual-license information.
19  *
20  * Revision 1.1  2004/05/23 15:43:16  bernie
21  * Import mware modules.
22  *
23  * Revision 1.4  2004/04/03 20:42:27  aleph
24  * Remove duplicated defines
25  *
26  * Revision 1.3  2004/03/24 15:48:53  bernie
27  * Remove Copyright messages from Doxygen output
28  *
29  * Revision 1.2  2004/03/19 16:51:30  bernie
30  * Add PROGMEM kludge.
31  *
32  * Revision 1.1  2004/02/23 09:45:09  aleph
33  * Add missing library functions.
34  *
35  * Revision 1.1  2003/11/13 16:56:37  aleph
36  * Add first implementation of dsp firmware
37  *
38  */
39
40 #include "compiler.h"
41 #include "formatwr.h"
42 #include <stdio.h>
43
44
45 static void __str_put_char(char c, void *ptr)
46 {
47         /*
48          * This Does not work on Code Warrior. Hmm...
49          *      *(*((char **)ptr))++ = c;
50          */
51
52         **((char **)ptr) = c;
53         (*((char **)ptr))++;
54 }
55
56 static void __null_put_char(UNUSED(char c), UNUSED(void *ptr))
57 {
58         /* nop */
59 }
60
61
62 int PGM_FUNC(vsprintf)(char * str, const char * PGM_ATTR fmt, va_list ap)
63 {
64         int result;
65
66         result = PGM_FUNC(_formatted_write)(
67                         fmt, (str ? __str_put_char : __null_put_char), &str, ap);
68
69         /* terminate string */
70         *str = '\0';
71
72         return result;
73 }
74
75 /* FIXME: sprintf_P is incorrectly declared in <stdio.h> */
76 int PGM_FUNC(sprintf)(char *str, const char * fmt, ...)
77 {
78         int result;
79         va_list ap;
80
81         va_start(ap, fmt);
82         result = PGM_FUNC(vsprintf)(str, fmt, ap);
83         va_end(ap);
84
85         /* terminate string */
86         *str = '\0';
87
88         return result;
89 }