2 * Software License Agreement (BSD License)
4 * Based on original stdio.c released by Atmel
5 * Copyright (c) 2008, Atmel Corporation
8 * Modified by Roel Verdult, Copyright (c) 2010
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the copyright holders nor the
18 * names of its contributors may be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
42 #include "projectconfig.h" // For CFG_PRINTF_MAXSTRINGSIZE
44 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
48 // Required for proper compilation.
49 //struct _reent r = {0, (FILE*) 0, (FILE*) 1, (FILE*) 0};
50 //struct _reent *_impure_ptr = &r;
52 //------------------------------------------------------------------------------
54 //------------------------------------------------------------------------------
56 //------------------------------------------------------------------------------
57 // Writes a character inside the given string. Returns 1.
58 // \param pStr Storage string.
59 // \param c Character to write.
60 //------------------------------------------------------------------------------
61 signed int append_char(char *pStr
, char c
)
67 //------------------------------------------------------------------------------
68 // Writes a string inside the given string.
69 // Returns the size of the written
71 // \param pStr Storage string.
72 // \param pSource Source string.
73 //------------------------------------------------------------------------------
74 signed int PutString(char *pStr
, char fill
, signed int width
, const char *pSource
)
78 while (*pSource
!= 0) {
95 //------------------------------------------------------------------------------
96 // Writes an unsigned int inside the given string, using the provided fill &
98 // Returns the size in characters of the written integer.
99 // \param pStr Storage string.
100 // \param fill Fill character.
101 // \param width Minimum integer width.
102 // \param value Integer value.
103 //------------------------------------------------------------------------------
104 signed int PutUnsignedInt(
112 // Take current digit into account when calculating width
115 // Recursively write upper digits
116 if ((value
/ 10) > 0) {
118 num
= PutUnsignedInt(pStr
, fill
, width
, value
/ 10);
121 // Write filler characters
126 append_char(pStr
, fill
);
134 num
+= append_char(pStr
, (value
% 10) + '0');
139 //------------------------------------------------------------------------------
140 // Writes a signed int inside the given string, using the provided fill & width
142 // Returns the size of the written integer.
143 // \param pStr Storage string.
144 // \param fill Fill character.
145 // \param width Minimum integer width.
146 // \param value Signed integer value.
147 //------------------------------------------------------------------------------
148 signed int PutSignedInt(
155 unsigned int absolute
;
157 // Compute absolute value
167 // Take current digit into account when calculating width
170 // Recursively write upper digits
171 if ((absolute
/ 10) > 0) {
175 num
= PutSignedInt(pStr
, fill
, width
, -(absolute
/ 10));
179 num
= PutSignedInt(pStr
, fill
, width
, absolute
/ 10);
185 // Reserve space for sign
191 // Write filler characters
194 append_char(pStr
, fill
);
203 num
+= append_char(pStr
, '-');
209 num
+= append_char(pStr
, (absolute
% 10) + '0');
214 //------------------------------------------------------------------------------
215 // Writes an hexadecimal value into a string, using the given fill, width &
216 // capital parameters.
217 // Returns the number of char written.
218 // \param pStr Storage string.
219 // \param fill Fill character.
220 // \param width Minimum integer width.
221 // \param maj Indicates if the letters must be printed in lower- or upper-case.
222 // \param value Hexadecimal value.
223 //------------------------------------------------------------------------------
236 // Recursively output upper digits
237 if ((value
>> 4) > 0) {
239 num
+= PutHexa(pStr
, fill
, width
, maj
, value
>> 4);
242 // Write filler chars
247 append_char(pStr
, fill
);
254 // Write current digit
255 if ((value
& 0xF) < 10) {
257 append_char(pStr
, (value
& 0xF) + '0');
261 append_char(pStr
, (value
& 0xF) - 10 + 'A');
265 append_char(pStr
, (value
& 0xF) - 10 + 'a');
272 //------------------------------------------------------------------------------
274 //------------------------------------------------------------------------------
276 //------------------------------------------------------------------------------
277 /// Stores the result of a formatted string into another string. Format
278 /// arguments are given in a va_list instance.
279 /// Return the number of characters written.
280 /// \param pStr Destination string.
281 /// \param length Length of Destination string.
282 /// \param pFormat Format string.
283 /// \param ap Argument list.
284 //------------------------------------------------------------------------------
285 signed int vsnprintf(char *pStr
, size_t length
, const char *pFormat
, va_list ap
)
299 while (*pFormat
!= 0 && size
< length
) {
302 if (*pFormat
!= '%') {
304 *pStr
++ = *pFormat
++;
308 else if (*(pFormat
+1) == '%') {
322 if (*pFormat
== '0') {
329 if (*pFormat
== '-') {
334 while ((*pFormat
>= '0') && (*pFormat
<= '9')) {
336 width
= (width
*10) + *pFormat
-'0';
340 // Check if there is enough space
341 if (size
+ width
> length
) {
343 width
= length
- size
;
349 case 'i': num
= PutSignedInt(pStr
, fill
, width
, va_arg(ap
, signed int)); break;
350 case 'u': num
= PutUnsignedInt(pStr
, fill
, width
, va_arg(ap
, unsigned int)); break;
351 case 'x': num
= PutHexa(pStr
, fill
, width
, 0, va_arg(ap
, unsigned int)); break;
352 case 'X': num
= PutHexa(pStr
, fill
, width
, 1, va_arg(ap
, unsigned int)); break;
353 case 's': num
= PutString(pStr
, fill
, width
, va_arg(ap
, char *)); break;
354 case 'c': num
= append_char(pStr
, va_arg(ap
, unsigned int)); break;
365 // NULL-terminated (final \0 is not counted)
379 //------------------------------------------------------------------------------
380 /// Stores the result of a formatted string into another string. Format
381 /// arguments are given in a va_list instance.
382 /// Return the number of characters written.
383 /// \param pString Destination string.
384 /// \param length Length of Destination string.
385 /// \param pFormat Format string.
386 /// \param ... Other arguments
387 //------------------------------------------------------------------------------
388 signed int snprintf(char *pString
, size_t length
, const char *pFormat
, ...)
393 va_start(ap
, pFormat
);
394 rc
= vsnprintf(pString
, length
, pFormat
, ap
);
400 //------------------------------------------------------------------------------
401 /// Stores the result of a formatted string into another string. Format
402 /// arguments are given in a va_list instance.
403 /// Return the number of characters written.
404 /// \param pString Destination string.
405 /// \param pFormat Format string.
406 /// \param ap Argument list.
407 //------------------------------------------------------------------------------
408 signed int vsprintf(char *pString
, const char *pFormat
, va_list ap
)
410 return vsnprintf(pString
, CFG_PRINTF_MAXSTRINGSIZE
, pFormat
, ap
);
413 //------------------------------------------------------------------------------
414 /// Outputs a formatted string on the DBGU stream. Format arguments are given
415 /// in a va_list instance.
416 /// \param pFormat Format string
417 /// \param ap Argument list.
418 //------------------------------------------------------------------------------
419 signed int vprintf(const char *pFormat
, va_list ap
)
421 char pStr
[CFG_PRINTF_MAXSTRINGSIZE
];
422 char pError
[] = "stdio.c: increase CFG_PRINTF_MAXSTRINGSIZE\r\n";
424 // Write formatted string in buffer
425 if (vsprintf(pStr
, pFormat
, ap
) >= CFG_PRINTF_MAXSTRINGSIZE
) {
428 while (1); // Increase CFG_PRINTF_MAXSTRINGSIZE
435 //------------------------------------------------------------------------------
436 /// Outputs a formatted string on the DBGU stream, using a variable number of
438 /// \param pFormat Format string.
439 //------------------------------------------------------------------------------
440 signed int printf(const char *pFormat
, ...)
445 // Forward call to vprintf
446 va_start(ap
, pFormat
);
447 result
= vprintf(pFormat
, ap
);
454 //------------------------------------------------------------------------------
455 /// Writes a formatted string inside another string.
456 /// \param pStr Storage string.
457 /// \param pFormat Format string.
458 //------------------------------------------------------------------------------
459 signed int sprintf(char *pStr
, const char *pFormat
, ...)
464 // Forward call to vsprintf
465 va_start(ap
, pFormat
);
466 result
= vsprintf(pStr
, pFormat
, ap
);
This page took 0.082732 seconds and 5 git commands to generate.