Skip to content

Commit 637df93

Browse files
committed
feat(printf): added vprintf() support
Closes #43
1 parent 87e1c83 commit 637df93

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

printf.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,13 @@ int snprintf_(char* buffer, size_t count, const char* format, ...)
884884
}
885885

886886

887+
int vprintf_(const char* format, va_list va)
888+
{
889+
char buffer[1];
890+
return _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
891+
}
892+
893+
887894
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va)
888895
{
889896
return _vsnprintf(_out_buffer, buffer, count, format, va);

printf.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///////////////////////////////////////////////////////////////////////////////
22
// \author (c) Marco Paland (info@paland.com)
3-
// 2014-2018, PALANDesign Hannover, Germany
3+
// 2014-2019, PALANDesign Hannover, Germany
44
//
55
// \license The MIT License (MIT)
66
//
@@ -77,6 +77,7 @@ int sprintf_(char* buffer, const char* format, ...);
7777
* \param buffer A pointer to the buffer where to store the formatted string
7878
* \param count The maximum number of characters to store in the buffer, including a terminating null character
7979
* \param format A string that specifies the format of the output
80+
* \param va A value identifying a variable arguments list
8081
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
8182
* If the formatted string is truncated the buffer size (count) is returned
8283
*/
@@ -86,6 +87,16 @@ int snprintf_(char* buffer, size_t count, const char* format, ...);
8687
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
8788

8889

90+
/**
91+
* Tiny vprintf implementation
92+
* \param format A string that specifies the format of the output
93+
* \param va A value identifying a variable arguments list
94+
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
95+
*/
96+
#define vprintf vprintf_
97+
int vprintf_(const char* format, va_list va);
98+
99+
89100
/**
90101
* printf with output function
91102
* You may use this as dynamic alternative to printf() with its fixed _putchar() output

test/test_suite.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ TEST_CASE("snprintf", "[]" ) {
8585
REQUIRE(!strcmp(buffer, "-1"));
8686
}
8787

88+
static void vprintf_builder_1(char* buffer, ...)
89+
{
90+
va_list args;
91+
va_start(args, buffer);
92+
test::vprintf("%d", args);
93+
va_end(args);
94+
}
8895

8996
static void vsnprintf_builder_1(char* buffer, ...)
9097
{
@@ -103,6 +110,17 @@ static void vsnprintf_builder_3(char* buffer, ...)
103110
}
104111

105112

113+
TEST_CASE("vprintf", "[]" ) {
114+
char buffer[100];
115+
printf_idx = 0U;
116+
memset(printf_buffer, 0xCC, 100U);
117+
vprintf_builder_1(buffer, 2345);
118+
REQUIRE(printf_buffer[4] == (char)0xCC);
119+
printf_buffer[4] = 0;
120+
REQUIRE(!strcmp(printf_buffer, "2345"));
121+
}
122+
123+
106124
TEST_CASE("vsnprintf", "[]" ) {
107125
char buffer[100];
108126

0 commit comments

Comments
 (0)