aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Paland <info@paland.com>2018-06-05 11:00:23 +0200
committerGitHub <noreply@github.com>2018-06-05 11:00:23 +0200
commitaa9d7a9a5413a867ff484436edf25a65dbe9c2d0 (patch)
tree1a1cd508faaf775eadef05899f3342a973ba985c
parent0116b7491693306e613491b7ccc44743da6d60cb (diff)
parentd40951182f051899acf66f11d55c526003b266c8 (diff)
downloadprintf-aa9d7a9a5413a867ff484436edf25a65dbe9c2d0.tar.gz
printf-aa9d7a9a5413a867ff484436edf25a65dbe9c2d0.tar.bz2
printf-aa9d7a9a5413a867ff484436edf25a65dbe9c2d0.zip
Merge pull request #18 from sgoll/fctprintf-user-data
feat(printf): add user pointer to fctprintf()
-rw-r--r--README.md2
-rw-r--r--printf.c19
-rw-r--r--printf.h3
3 files changed, 14 insertions, 10 deletions
diff --git a/README.md b/README.md
index 5ee138e..8420a5b 100644
--- a/README.md
+++ b/README.md
@@ -52,7 +52,7 @@ int snprintf(char* buffer, size_t count, const char* format, ...);
int vsnprintf(char* buffer, size_t count, const char* format, va_list va);
// call output function (instead of buffer)
-int fctprintf(void (*out)(char character), const char* format, ...);
+int fctprintf(void (*out)(char character, void* user), void* user, const char* format, ...);
```
diff --git a/printf.c b/printf.c
index aa34cbd..dec4555 100644
--- a/printf.c
+++ b/printf.c
@@ -77,6 +77,13 @@
typedef void (*out_fct_type)(char character, char* buffer, size_t idx, size_t maxlen);
+// wrapper used as buffer
+typedef struct {
+ void (*fct)(char character, void* user);
+ void* user;
+} out_fct_wrap_type;
+
+
// internal buffer output
static inline void _out_buffer(char character, char* buffer, size_t idx, size_t maxlen)
{
@@ -105,10 +112,8 @@ static inline void _out_char(char character, char* buffer, size_t idx, size_t ma
static inline void _out_fct(char character, char* buffer, size_t idx, size_t maxlen)
{
(void)idx; (void)maxlen;
- typedef struct tag_out_fct_wrap_type {
- void (*fct)(char character);
- } out_fct_wrap_type;
- ((out_fct_wrap_type*)buffer)->fct(character); // buffer is the output fct pointer
+ // buffer is the output fct pointer
+ ((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->user);
}
@@ -691,13 +696,11 @@ int vsnprintf(char* buffer, size_t count, const char* format, va_list va)
}
-int fctprintf(void (*out)(char character), const char* format, ...)
+int fctprintf(void (*out)(char character, void* user), void* user, const char* format, ...)
{
va_list va;
va_start(va, format);
- const struct tag_out_fct_wrap {
- void (*fct)(char character);
- } out_fct_wrap = { out };
+ const out_fct_wrap_type out_fct_wrap = { out, user };
const int ret = _vsnprintf(_out_fct, (char*)&out_fct_wrap, (size_t)-1, format, va);
va_end(va);
return ret;
diff --git a/printf.h b/printf.h
index 194f65f..8b8e76e 100644
--- a/printf.h
+++ b/printf.h
@@ -84,10 +84,11 @@ int vsnprintf(char* buffer, size_t count, const char* format, va_list va);
* printf with output function
* You may use this as dynamic alternative to printf() with its fixed _putchar() output
* \param out An output function which takes one character
+ * \param user A pointer to user data passed to output function
* \param format A string that specifies the format of the output
* \return The number of characters that are sent to the output function, not counting the terminating null character
*/
-int fctprintf(void (*out)(char character), const char* format, ...);
+int fctprintf(void (*out)(char character, void* user), void* user, const char* format, ...);
#ifdef __cplusplus