53cb92058a60614cb4b29c9fc6ba25e799e29257
[bertos.git] / mware / sprintf.c
1 /**
2  * \file
3  * <!--
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.
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.11  2006/07/19 12:56:28  bernie
17  *#* Convert to new Doxygen style.
18  *#*
19  *#* Revision 1.10  2005/04/11 19:10:28  bernie
20  *#* Include top-level headers from cfg/ subdir.
21  *#*
22  *#* Revision 1.9  2005/02/18 12:48:01  bernie
23  *#* Fix bug with NULL buffers (caught with unit test).
24  *#*
25  *#* Revision 1.8  2005/02/18 12:34:29  bernie
26  *#* Include <mware/pgm.h> explicitly for non-Harvard archs.
27  *#*
28  *#* Revision 1.7  2004/12/31 17:47:45  bernie
29  *#* Rename UNUSED() to UNUSED_ARG().
30  *#*
31  *#* Revision 1.6  2004/11/16 21:15:19  bernie
32  *#* Fix off-by-one bug in [v]snprintf().
33  *#*
34  *#* Revision 1.5  2004/10/03 18:54:36  bernie
35  *#* sprintf(): Fix a serious bug; snprintf(): New function.
36  *#*
37  *#* Revision 1.4  2004/08/25 14:12:09  rasky
38  *#* Aggiornato il comment block dei log RCS
39  *#*
40  *#* Revision 1.3  2004/06/27 15:20:26  aleph
41  *#* Change UNUSED() macro to accept two arguments: type and name;
42  *#* Add macro GNUC_PREREQ to detect GCC version during build;
43  *#* Some spacing cleanups and typo fix
44  *#*
45  *#* Revision 1.2  2004/06/03 11:27:09  bernie
46  *#* Add dual-license information.
47  *#*/
48
49 #include <mware/formatwr.h>
50 #include <mware/pgm.h>
51 #include <cfg/compiler.h>
52
53 #include <stdio.h>
54
55
56 static void __str_put_char(char c, void *ptr)
57 {
58         /*
59          * This Does not work on Code Warrior. Hmm...
60          *      *(*((char **)ptr))++ = c;
61          */
62
63         **((char **)ptr) = c;
64         (*((char **)ptr))++;
65 }
66
67 static void __null_put_char(UNUSED_ARG(char, c), UNUSED_ARG(void *, ptr))
68 {
69         /* nop */
70 }
71
72
73 int PGM_FUNC(vsprintf)(char *str, const char * PGM_ATTR fmt, va_list ap)
74 {
75         int result;
76
77         if (str)
78         {
79                 result = PGM_FUNC(_formatted_write)(fmt, __str_put_char, &str, ap);
80
81                 /* Terminate string */
82                 *str = '\0';
83         }
84         else
85                 result = PGM_FUNC(_formatted_write)(fmt, __null_put_char, 0, ap);
86
87
88         return result;
89 }
90
91
92 int PGM_FUNC(sprintf)(char *str, const char * fmt, ...)
93 {
94         int result;
95         va_list ap;
96
97         va_start(ap, fmt);
98         result = PGM_FUNC(vsprintf)(str, fmt, ap);
99         va_end(ap);
100
101         return result;
102 }
103
104 /**
105  * State information for __sn_put_char()
106  */
107 struct __sn_state
108 {
109         char *str;
110         size_t len;
111 };
112
113 /**
114  * formatted_write() callback used [v]snprintf().
115  */
116 static void __sn_put_char(char c, void *ptr)
117 {
118         struct __sn_state *state = (struct __sn_state *)ptr;
119
120         if (state->len)
121         {
122                 --state->len;
123                 *state->str++ = c;
124         }
125 }
126
127
128 int PGM_FUNC(vsnprintf)(char *str, size_t size, const char * PGM_ATTR fmt, va_list ap)
129 {
130         int result = 0;
131
132         /* Make room for traling '\0'. */
133         if (size--)
134         {
135                 if (str)
136                 {
137                         struct __sn_state state;
138                         state.str = str;
139                         state.len = size;
140
141                         result = PGM_FUNC(_formatted_write)(fmt, __sn_put_char, &state, ap);
142
143                         /* Terminate string. */
144                         *state.str = '\0';
145                 }
146                 else
147                         result = PGM_FUNC(_formatted_write)(fmt, __null_put_char, 0, ap);
148         }
149
150         return result;
151 }
152
153
154 int PGM_FUNC(snprintf)(char *str, size_t size, const char * fmt, ...)
155 {
156         int result;
157         va_list ap;
158
159         va_start(ap, fmt);
160         result = PGM_FUNC(vsnprintf)(str, size, fmt, ap);
161         va_end(ap);
162
163         return result;
164 }