aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md4
-rw-r--r--printf.c28
-rw-r--r--printf.h44
3 files changed, 19 insertions, 57 deletions
diff --git a/README.md b/README.md
index 84a645a..945d66b 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@
This is a tiny but **fully loaded** printf, sprintf and (v)snprintf implementation.
Primarily designed for usage in embedded systems, where printf is not available due to memory issues or in avoidance of linking against libc.
Using the standard libc printf may pull **a lot** of unwanted library stuff and can bloat code size about 20k or is not 100% thread safe. In this cases the following implementation can be used.
-Absolutely **NO dependencies** are required, *printf.c* brings all necessary routines, even its own fast `ftoa` (float), `ntoa` (decimal) conversion.
+Absolutely **NO dependencies** are required, *printf.c* brings all necessary routines, even its own fast `ftoa` (floating point), `ntoa` (decimal) conversion.
If memory footprint is really a critical issue, floating point and 'long long' support and can be turned off via the `PRINTF_DISABLE_SUPPORT_FLOAT` and `PRINTF_DISABLE_SUPPORT_LONG_LONG` compiler switches.
When using printf (instead of sprintf/snprintf) you have to provide your own `_putchar()` low level function as console/serial output.
@@ -161,7 +161,6 @@ int length = sprintf(NULL, "Hello, world"); // length is set to 12
| Name | Default value | Description |
|------|---------------|-------------|
| PRINTF_INCLUDE_CONFIG_H | undefined | Define this as compiler switch (e.g. `gcc -DPRINTF_INCLUDE_CONFIG_H`) to include a "printf_config.h" definition file |
-| PRINTF_OVERRIDE_LIBC | undefined | Define this to override the LIBC function declarations by using `printf_()` instead of `printf()` directly |
| PRINTF_NTOA_BUFFER_SIZE | 32 | ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack |
| PRINTF_FTOA_BUFFER_SIZE | 32 | ftoa (float) conversion buffer size. This must be big enough to hold one converted float number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack |
| PRINTF_DISABLE_SUPPORT_FLOAT | undefined | Define this to enable floating point (%f) support |
@@ -173,7 +172,6 @@ int length = sprintf(NULL, "Hello, world"); // length is set to 12
- The internal floating point conversion has a maximum precision of 9 digits. Any higher precision is truncated after the 9th digit and zeros are returned.
So `printf("%.12f", 42.89522312345678)` gives `42.895223123000`.
- Exponential floating point format (e.g. `"%.10e"` to get `1.167e+65`) for large numbers is not supported yet. Sorry.
-- `double` type is not supported in the moment, downcast to `float` might by required.
## Test Suite
diff --git a/printf.c b/printf.c
index 8cbb211..a04b42b 100644
--- a/printf.c
+++ b/printf.c
@@ -36,6 +36,14 @@
#include "printf.h"
+// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
+// printf_config.h header file
+// default: undefined
+#ifdef PRINTF_INCLUDE_CONFIG_H
+#include "printf_config.h"
+#endif
+
+
// 'ntoa' conversion buffer size, this must be big enough to hold one converted
// numeric number including padded zeros (dynamically created on stack)
// default: 32 byte
@@ -703,11 +711,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
///////////////////////////////////////////////////////////////////////////////
-#ifndef PRINTF_OVERRIDE_LIBC
-int printf(const char* format, ...)
-#else
int printf_(const char* format, ...)
-#endif
{
va_list va;
va_start(va, format);
@@ -718,11 +722,7 @@ int printf_(const char* format, ...)
}
-#ifndef PRINTF_OVERRIDE_LIBC
-int sprintf(char* buffer, const char* format, ...)
-#else
int sprintf_(char* buffer, const char* format, ...)
-#endif
{
va_list va;
va_start(va, format);
@@ -732,11 +732,7 @@ int sprintf_(char* buffer, const char* format, ...)
}
-#ifndef PRINTF_OVERRIDE_LIBC
-int snprintf(char* buffer, size_t count, const char* format, ...)
-#else
int snprintf_(char* buffer, size_t count, const char* format, ...)
-#endif
{
va_list va;
va_start(va, format);
@@ -746,21 +742,13 @@ int snprintf_(char* buffer, size_t count, const char* format, ...)
}
-#ifndef PRINTF_OVERRIDE_LIBC
-int vsnprintf(char* buffer, size_t count, const char* format, va_list va)
-#else
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va)
-#endif
{
return _vsnprintf(_out_buffer, buffer, count, format, va);
}
-#ifndef PRINTF_OVERRIDE_LIBC
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...)
-#else
-int fctprintf_(void (*out)(char character, void* arg), void* arg, const char* format, ...)
-#endif
{
va_list va;
va_start(va, format);
diff --git a/printf.h b/printf.h
index 69245cb..6a9339e 100644
--- a/printf.h
+++ b/printf.h
@@ -35,13 +35,6 @@
#include <stdarg.h>
#include <stddef.h>
-// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
-// printf_config.h header file
-// default: undefined
-#ifdef PRINTF_INCLUDE_CONFIG_H
-#include "printf_config.h"
-#endif
-
#ifdef __cplusplus
extern "C" {
@@ -56,19 +49,16 @@ extern "C" {
void _putchar(char character);
-// if PRINTF_OVERRIDE_LIBC is is defined, the regular printf() API is overridden by macro defines
-// and internal underscore-appended functions like printf_() are used to avoid conflicts with
-// LIBC defined printf() functions.
-// default: undefined
-#ifndef PRINTF_OVERRIDE_LIBC
-
/**
* Tiny printf implementation
* You have to implement _putchar if you use printf()
+ * To avoid conflicts with the regular printf() API it is overridden by macro defines
+ * and internal underscore-appended functions like printf_() are used
* \param format A string that specifies the format of the output
* \return The number of characters that are written into the array, not counting the terminating null character
*/
-int printf(const char* format, ...);
+#define printf printf_
+int printf_(const char* format, ...);
/**
@@ -78,7 +68,8 @@ int printf(const char* format, ...);
* \param format A string that specifies the format of the output
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
*/
-int sprintf(char* buffer, const char* format, ...);
+#define sprintf sprintf_
+int sprintf_(char* buffer, const char* format, ...);
/**
@@ -89,8 +80,10 @@ int sprintf(char* buffer, const char* format, ...);
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
* If the formatted string is truncated the buffer size (count) is returned
*/
-int snprintf(char* buffer, size_t count, const char* format, ...);
-int vsnprintf(char* buffer, size_t count, const char* format, va_list va);
+#define snprintf snprintf_
+#define vsnprintf vsnprintf_
+int snprintf_(char* buffer, size_t count, const char* format, ...);
+int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
/**
@@ -103,23 +96,6 @@ int vsnprintf(char* buffer, size_t count, const char* format, va_list va);
*/
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...);
-#else // PRINTF_OVERRIDE_LIBC
-
-// override the LIBC defined function names
-#define printf printf_
-#define sprintf sprintf_
-#define snprintf snprintf_
-#define vsnprintf vsnprintf_
-#define fctprintf fctprintf_
-
-int printf_(const char* format, ...);
-int sprintf_(char* buffer, const char* format, ...);
-int snprintf_(char* buffer, size_t count, const char* format, ...);
-int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
-int fctprintf_(void (*out)(char character, void* arg), void* arg, const char* format, ...);
-
-#endif // PRINTF_OVERRIDE_LIBC
-
#ifdef __cplusplus
}