aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Paland <marco@paland.com>2018-11-02 14:00:03 +0100
committerMarco Paland <marco@paland.com>2018-11-02 14:00:03 +0100
commit54dfd185432d300746a8586cd3e620d5eb178a5d (patch)
tree10720771a853e351438ea99c05ca6b1fe3195f95
parentcc8f3bc050042270c661c0807d6555c242e0b244 (diff)
downloadprintf-54dfd185432d300746a8586cd3e620d5eb178a5d.tar.gz
printf-54dfd185432d300746a8586cd3e620d5eb178a5d.tar.bz2
printf-54dfd185432d300746a8586cd3e620d5eb178a5d.zip
feat(printf): added PRINTF_OVERRIDE_LIBC support
Fixes #16
-rw-r--r--README.md2
-rw-r--r--printf.c21
-rw-r--r--printf.h25
3 files changed, 46 insertions, 2 deletions
diff --git a/README.md b/README.md
index 95cd069..bc2c62d 100644
--- a/README.md
+++ b/README.md
@@ -160,6 +160,8 @@ 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_SUPPORT_FLOAT | defined | Define this to enable floating point (%f) support |
diff --git a/printf.c b/printf.c
index ddea9e3..b5a38fc 100644
--- a/printf.c
+++ b/printf.c
@@ -699,8 +699,11 @@ 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);
@@ -711,7 +714,11 @@ 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);
@@ -721,7 +728,11 @@ 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);
@@ -731,13 +742,21 @@ 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 b82e70f..69245cb 100644
--- a/printf.h
+++ b/printf.h
@@ -25,7 +25,7 @@
// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on
// embedded systems with a very limited resources.
// Use this instead of bloated standard/newlib printf.
-// These routines are thread safe and reentrant!
+// These routines are thread safe and reentrant.
//
///////////////////////////////////////////////////////////////////////////////
@@ -56,6 +56,12 @@ 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()
@@ -97,6 +103,23 @@ 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
}