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 //------------------------------------------------------------------------------
44 //------------------------------------------------------------------------------
46 // Maximum string size allowed (in bytes).
47 #define MAX_STRING_SIZE 255
49 //------------------------------------------------------------------------------
51 //------------------------------------------------------------------------------
53 // Required for proper compilation.
54 //struct _reent r = {0, (FILE*) 0, (FILE*) 1, (FILE*) 0};
55 //struct _reent *_impure_ptr = &r;
57 //------------------------------------------------------------------------------
59 //------------------------------------------------------------------------------
61 //------------------------------------------------------------------------------
62 // Writes a character inside the given string. Returns 1.
63 // \param pStr Storage string.
64 // \param c Character to write.
65 //------------------------------------------------------------------------------
66 signed int append_char(char *pStr
, char c
)
72 //------------------------------------------------------------------------------
73 // Writes a string inside the given string.
74 // Returns the size of the written
76 // \param pStr Storage string.
77 // \param pSource Source string.
78 //------------------------------------------------------------------------------
79 signed int PutString(char *pStr
, char fill
, signed int width
, const char *pSource
)
83 while (*pSource
!= 0) {
100 //------------------------------------------------------------------------------
101 // Writes an unsigned int inside the given string, using the provided fill &
103 // Returns the size in characters of the written integer.
104 // \param pStr Storage string.
105 // \param fill Fill character.
106 // \param width Minimum integer width.
107 // \param value Integer value.
108 //------------------------------------------------------------------------------
109 signed int PutUnsignedInt(
117 // Take current digit into account when calculating width
120 // Recursively write upper digits
121 if ((value
/ 10) > 0) {
123 num
= PutUnsignedInt(pStr
, fill
, width
, value
/ 10);
126 // Write filler characters
131 append_char(pStr
, fill
);
139 num
+= append_char(pStr
, (value
% 10) + '0');
144 //------------------------------------------------------------------------------
145 // Writes a signed int inside the given string, using the provided fill & width
147 // Returns the size of the written integer.
148 // \param pStr Storage string.
149 // \param fill Fill character.
150 // \param width Minimum integer width.
151 // \param value Signed integer value.
152 //------------------------------------------------------------------------------
153 signed int PutSignedInt(
160 unsigned int absolute
;
162 // Compute absolute value
172 // Take current digit into account when calculating width
175 // Recursively write upper digits
176 if ((absolute
/ 10) > 0) {
180 num
= PutSignedInt(pStr
, fill
, width
, -(absolute
/ 10));
184 num
= PutSignedInt(pStr
, fill
, width
, absolute
/ 10);
190 // Reserve space for sign
196 // Write filler characters
199 append_char(pStr
, fill
);
208 num
+= append_char(pStr
, '-');
214 num
+= append_char(pStr
, (absolute
% 10) + '0');
219 //------------------------------------------------------------------------------
220 // Writes an hexadecimal value into a string, using the given fill, width &
221 // capital parameters.
222 // Returns the number of char written.
223 // \param pStr Storage string.
224 // \param fill Fill character.
225 // \param width Minimum integer width.
226 // \param maj Indicates if the letters must be printed in lower- or upper-case.
227 // \param value Hexadecimal value.
228 //------------------------------------------------------------------------------
241 // Recursively output upper digits
242 if ((value
>> 4) > 0) {
244 num
+= PutHexa(pStr
, fill
, width
, maj
, value
>> 4);
247 // Write filler chars
252 append_char(pStr
, fill
);
259 // Write current digit
260 if ((value
& 0xF) < 10) {
262 append_char(pStr
, (value
& 0xF) + '0');
266 append_char(pStr
, (value
& 0xF) - 10 + 'A');
270 append_char(pStr
, (value
& 0xF) - 10 + 'a');
277 //------------------------------------------------------------------------------
279 //------------------------------------------------------------------------------
281 //------------------------------------------------------------------------------
282 /// Stores the result of a formatted string into another string. Format
283 /// arguments are given in a va_list instance.
284 /// Return the number of characters written.
285 /// \param pStr Destination string.
286 /// \param length Length of Destination string.
287 /// \param pFormat Format string.
288 /// \param ap Argument list.
289 //------------------------------------------------------------------------------
290 signed int vsnprintf(char *pStr
, size_t length
, const char *pFormat
, va_list ap
)
304 while (*pFormat
!= 0 && size
< length
) {
307 if (*pFormat
!= '%') {
309 *pStr
++ = *pFormat
++;
313 else if (*(pFormat
+1) == '%') {
327 if (*pFormat
== '0') {
334 if (*pFormat
== '-') {
339 while ((*pFormat
>= '0') && (*pFormat
<= '9')) {
341 width
= (width
*10) + *pFormat
-'0';
345 // Check if there is enough space
346 if (size
+ width
> length
) {
348 width
= length
- size
;
354 case 'i': num
= PutSignedInt(pStr
, fill
, width
, va_arg(ap
, signed int)); break;
355 case 'u': num
= PutUnsignedInt(pStr
, fill
, width
, va_arg(ap
, unsigned int)); break;
356 case 'x': num
= PutHexa(pStr
, fill
, width
, 0, va_arg(ap
, unsigned int)); break;
357 case 'X': num
= PutHexa(pStr
, fill
, width
, 1, va_arg(ap
, unsigned int)); break;
358 case 's': num
= PutString(pStr
, fill
, width
, va_arg(ap
, char *)); break;
359 case 'c': num
= append_char(pStr
, va_arg(ap
, unsigned int)); break;
370 // NULL-terminated (final \0 is not counted)
384 //------------------------------------------------------------------------------
385 /// Stores the result of a formatted string into another string. Format
386 /// arguments are given in a va_list instance.
387 /// Return the number of characters written.
388 /// \param pString Destination string.
389 /// \param length Length of Destination string.
390 /// \param pFormat Format string.
391 /// \param ... Other arguments
392 //------------------------------------------------------------------------------
393 signed int snprintf(char *pString
, size_t length
, const char *pFormat
, ...)
398 va_start(ap
, pFormat
);
399 rc
= vsnprintf(pString
, length
, pFormat
, ap
);
405 //------------------------------------------------------------------------------
406 /// Stores the result of a formatted string into another string. Format
407 /// arguments are given in a va_list instance.
408 /// Return the number of characters written.
409 /// \param pString Destination string.
410 /// \param pFormat Format string.
411 /// \param ap Argument list.
412 //------------------------------------------------------------------------------
413 signed int vsprintf(char *pString
, const char *pFormat
, va_list ap
)
415 return vsnprintf(pString
, MAX_STRING_SIZE
, pFormat
, ap
);
418 //------------------------------------------------------------------------------
419 /// Outputs a formatted string on the DBGU stream. Format arguments are given
420 /// in a va_list instance.
421 /// \param pFormat Format string
422 /// \param ap Argument list.
423 //------------------------------------------------------------------------------
424 signed int vprintf(const char *pFormat
, va_list ap
)
426 char pStr
[MAX_STRING_SIZE
];
427 char pError
[] = "stdio.c: increase MAX_STRING_SIZE\r\n";
429 // Write formatted string in buffer
430 if (vsprintf(pStr
, pFormat
, ap
) >= MAX_STRING_SIZE
) {
433 while (1); // Increase MAX_STRING_SIZE
440 //------------------------------------------------------------------------------
441 /// Outputs a formatted string on the DBGU stream, using a variable number of
443 /// \param pFormat Format string.
444 //------------------------------------------------------------------------------
445 signed int printf(const char *pFormat
, ...)
450 // Forward call to vprintf
451 va_start(ap
, pFormat
);
452 result
= vprintf(pFormat
, ap
);
459 //------------------------------------------------------------------------------
460 /// Writes a formatted string inside another string.
461 /// \param pStr Storage string.
462 /// \param pFormat Format string.
463 //------------------------------------------------------------------------------
464 signed int sprintf(char *pStr
, const char *pFormat
, ...)
469 // Forward call to vsprintf
470 va_start(ap
, pFormat
);
471 result
= vsprintf(pStr
, pFormat
, ap
);
This page took 0.084695 seconds and 5 git commands to generate.