Update to new naming scheme in mware/gfx.c.
[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  * \brief sprintf() implementation based on _formatted_write()
9  *
10  * \version $Id$
11  * \author Bernardo Innocenti <bernie@develer.com>
12  */
13
14 /*#*
15  *#* $Log$
16  *#* Revision 1.5  2004/10/03 18:54:36  bernie
17  *#* sprintf(): Fix a serious bug; snprintf(): New function.
18  *#*
19  *#* Revision 1.4  2004/08/25 14:12:09  rasky
20  *#* Aggiornato il comment block dei log RCS
21  *#*
22  *#* Revision 1.3  2004/06/27 15:20:26  aleph
23  *#* Change UNUSED() macro to accept two arguments: type and name;
24  *#* Add macro GNUC_PREREQ to detect GCC version during build;
25  *#* Some spacing cleanups and typo fix
26  *#*
27  *#* Revision 1.2  2004/06/03 11:27:09  bernie
28  *#* Add dual-license information.
29  *#*
30  *#* Revision 1.1  2004/05/23 15:43:16  bernie
31  *#* Import mware modules.
32  *#*
33  *#* Revision 1.4  2004/04/03 20:42:27  aleph
34  *#* Remove duplicated defines
35  *#*
36  *#* Revision 1.3  2004/03/24 15:48:53  bernie
37  *#* Remove Copyright messages from Doxygen output
38  *#*
39  *#* Revision 1.2  2004/03/19 16:51:30  bernie
40  *#* Add PROGMEM kludge.
41  *#*
42  *#* Revision 1.1  2004/02/23 09:45:09  aleph
43  *#* Add missing library functions.
44  *#*
45  *#* Revision 1.1  2003/11/13 16:56:37  aleph
46  *#* Add first implementation of dsp firmware
47  *#*
48  *#*/
49
50 #include "compiler.h"
51 #include "formatwr.h"
52 #include <stdio.h>
53
54
55 static void __str_put_char(char c, void *ptr)
56 {
57         /*
58          * This Does not work on Code Warrior. Hmm...
59          *      *(*((char **)ptr))++ = c;
60          */
61
62         **((char **)ptr) = c;
63         (*((char **)ptr))++;
64 }
65
66 static void __null_put_char(UNUSED(char, c), UNUSED(void *, ptr))
67 {
68         /* nop */
69 }
70
71
72 int PGM_FUNC(vsprintf)(char * str, const char * PGM_ATTR fmt, va_list ap)
73 {
74         int result;
75
76         result = PGM_FUNC(_formatted_write)(
77                         fmt, (str ? __str_put_char : __null_put_char), &str, ap);
78
79         /* Terminate string */
80         *str = '\0';
81
82         return result;
83 }
84
85
86 int PGM_FUNC(sprintf)(char *str, const char * fmt, ...)
87 {
88         int result;
89         va_list ap;
90
91         va_start(ap, fmt);
92         result = PGM_FUNC(vsprintf)(str, fmt, ap);
93         va_end(ap);
94
95         return result;
96 }
97
98 /*!
99  * State information for __sn_put_char()
100  */
101 struct __sn_state
102 {
103         char *str;
104         size_t len;
105 };
106
107 /*!
108  * formatted_write() callback used [v]snprintf().
109  */
110 static void __sn_put_char(char c, void *ptr)
111 {
112         struct __sn_state *state = (struct __sn_state *)ptr;
113
114         if (state->len > 0)
115         {
116                 --state->len;
117                 *state->str++ = c;
118         }
119 }
120
121
122 int PGM_FUNC(vsnprintf)(char * str, size_t size, const char * PGM_ATTR fmt, va_list ap)
123 {
124         int result;
125         struct __sn_state state = { str, size };
126
127         result = PGM_FUNC(_formatted_write)(
128                         fmt, (str ? __sn_put_char : __null_put_char), &state, ap);
129
130         /* Terminate string */
131         *state.str = '\0';
132
133         return result;
134 }
135
136
137 int PGM_FUNC(snprintf)(char *str, size_t size, const char * fmt, ...)
138 {
139         int result;
140         va_list ap;
141
142         va_start(ap, fmt);
143         result = PGM_FUNC(vsnprintf)(str, size, fmt, ap);
144         va_end(ap);
145
146         return result;
147 }