22
22
#include <stdint.h>
23
23
#include <stddef.h>
24
24
25
- /***************************/
26
- /* MBED */
27
- /***************************/
28
- #if TARGET_LIKE_MBED
29
-
30
- #if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
31
- static char mbed_stdio_out_prev = 0 ;
32
- #endif
33
-
34
-
35
- /***************************/
36
- /* Linux */
37
- /***************************/
38
- #else
25
+ #if !TARGET_LIKE_MBED
39
26
/* Linux implementation is for debug only */
40
27
#define MBED_CONF_PLATFORM_MINIMAL_PRINTF_ENABLE_FLOATING_POINT 1
41
28
#define MBED_CONF_PLATFORM_MINIMAL_PRINTF_SET_FLOATING_POINT_MAX_DECIMALS 6
@@ -106,7 +93,6 @@ static void mbed_minimal_formatted_string_signed(char *buffer, size_t length, in
106
93
static void mbed_minimal_formatted_string_unsigned (char * buffer , size_t length , int * result , MBED_UNSIGNED_STORAGE value , FILE * stream );
107
94
static void mbed_minimal_formatted_string_hexadecimal (char * buffer , size_t length , int * result , MBED_UNSIGNED_STORAGE value , FILE * stream , bool upper );
108
95
static void mbed_minimal_formatted_string_void_pointer (char * buffer , size_t length , int * result , const void * value , FILE * stream );
109
- static void mbed_minimal_formatted_string_character (char * buffer , size_t length , int * result , char character , FILE * stream );
110
96
static void mbed_minimal_formatted_string_string (char * buffer , size_t length , int * result , const char * string , size_t precision , FILE * stream );
111
97
112
98
@@ -122,21 +108,23 @@ static void mbed_minimal_putchar(char *buffer, size_t length, int *result, char
122
108
{
123
109
/* only continue if 'result' doesn't overflow */
124
110
if ((* result >= 0 ) && (* result <= INT_MAX - 1 )) {
125
- if (buffer ) {
126
- /* write data only if there's enough space */
127
- if ((size_t )* result < length ) {
128
- buffer [* result ] = data ;
129
- }
130
-
131
- /* increment 'result' even if data was not written. This ensures that
132
- 'mbed_minimal_formatted_string' returns the correct value. */
133
- * result += 1 ;
134
- } else {
111
+ if (stream ) {
135
112
if (fputc (data , stream ) == EOF ) {
136
113
* result = EOF ;
137
114
} else {
138
115
* result += 1 ;
139
116
}
117
+ } else {
118
+ if (buffer ) {
119
+ /* write data only if there's enough space */
120
+ if ((size_t )* result < length ) {
121
+ buffer [* result ] = data ;
122
+ }
123
+ }
124
+
125
+ /* increment 'result' even if data was not written. This ensures that
126
+ 'mbed_minimal_formatted_string' returns the correct value. */
127
+ * result += 1 ;
140
128
}
141
129
}
142
130
}
@@ -280,7 +268,7 @@ static void mbed_minimal_formatted_string_double(char *buffer, size_t length, in
280
268
mbed_minimal_formatted_string_signed (buffer , length , result , integer , stream );
281
269
282
270
/* write decimal point */
283
- mbed_minimal_formatted_string_character (buffer , length , result , '.' , stream );
271
+ mbed_minimal_putchar (buffer , length , result , '.' , stream );
284
272
285
273
/* get decimal part */
286
274
double precision = 1.0 ;
@@ -325,33 +313,6 @@ static void mbed_minimal_formatted_string_double(char *buffer, size_t length, in
325
313
}
326
314
#endif
327
315
328
- /**
329
- * @brief Print character.
330
- *
331
- * @param buffer The buffer to store output (NULL for stdout).
332
- * @param[in] length The length of the buffer.
333
- * @param result The current output location.
334
- * @param[in] value The character to be printed.
335
- */
336
- static void mbed_minimal_formatted_string_character (char * buffer , size_t length , int * result , char character , FILE * stream )
337
- {
338
- /* write character */
339
- if (buffer ) {
340
- mbed_minimal_putchar (buffer , length , result , character , stream );
341
- } else {
342
- /* convert \n to \r\n if enabled in platform configuration */
343
- #if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
344
- if (character == '\n' && mbed_stdio_out_prev != '\r' ) {
345
- mbed_minimal_putchar (buffer , length , result , '\r' , stream );
346
- }
347
-
348
- /* cache character */
349
- mbed_stdio_out_prev = character ;
350
- #endif
351
- mbed_minimal_putchar (buffer , length , result , character , stream );
352
- }
353
- }
354
-
355
316
/**
356
317
* @brief Print string with precision.
357
318
*
@@ -511,7 +472,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
511
472
#else
512
473
/* If 64 bit is not enabled, print %ll[di] rather than truncated value */
513
474
if (length_modifier == LENGTH_LL ) {
514
- mbed_minimal_formatted_string_character (buffer , length , & result , '%' , stream );
475
+ mbed_minimal_putchar (buffer , length , & result , '%' , stream );
515
476
if (next == '%' ) {
516
477
// Continue printing loop after `%`
517
478
index = next_index ;
@@ -570,7 +531,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
570
531
#else
571
532
/* If 64 bit is not enabled, print %ll[uxX] rather than truncated value */
572
533
if (length_modifier == LENGTH_LL ) {
573
- mbed_minimal_formatted_string_character (buffer , length , & result , '%' , stream );
534
+ mbed_minimal_putchar (buffer , length , & result , '%' , stream );
574
535
if (next == '%' ) {
575
536
// Continue printing loop after `%`
576
537
index = next_index ;
@@ -636,7 +597,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
636
597
char value = va_arg (arguments , MBED_SIGNED_NATIVE_TYPE );
637
598
index = next_index ;
638
599
639
- mbed_minimal_formatted_string_character (buffer , length , & result , value , stream );
600
+ mbed_minimal_putchar (buffer , length , & result , value , stream );
640
601
}
641
602
/* string */
642
603
else if (next == 's' ) {
@@ -653,7 +614,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
653
614
mbed_minimal_formatted_string_void_pointer (buffer , length , & result , value , stream );
654
615
} else {
655
616
// Unrecognised, or `%%`. Print the `%` that led us in.
656
- mbed_minimal_formatted_string_character (buffer , length , & result , '%' , stream );
617
+ mbed_minimal_putchar (buffer , length , & result , '%' , stream );
657
618
if (next == '%' ) {
658
619
// Continue printing loop after `%%`
659
620
index = next_index ;
@@ -665,7 +626,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
665
626
/* not a format specifier */
666
627
{
667
628
/* write normal character */
668
- mbed_minimal_formatted_string_character (buffer , length , & result , format [index ], stream );
629
+ mbed_minimal_putchar (buffer , length , & result , format [index ], stream );
669
630
}
670
631
}
671
632
0 commit comments