Skip to content

Commit

Permalink
Merge pull request #8 from lvlhead/printf-test
Browse files Browse the repository at this point in the history
Add include guards to printf library
  • Loading branch information
prokrypt authored Jul 23, 2024
2 parents c12afa6 + df8f2e4 commit b042743
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
19 changes: 13 additions & 6 deletions external/printf/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
}
}

#ifndef PRINTF_VOXLESS
// evaluate length field
switch (*format) {
case 'l' :
Expand All @@ -653,6 +654,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
format++;
}
break;

case 'h' :
flags |= FLAGS_SHORT;
format++;
Expand All @@ -661,6 +663,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
format++;
}
break;

#if defined(PRINTF_SUPPORT_PTRDIFF_T)
case 't' :
flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
Expand All @@ -678,6 +681,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
default :
break;
}
#endif // PRINTF_VOXLESS

// evaluate specifier
switch (*format) {
Expand Down Expand Up @@ -754,6 +758,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
format++;
break;
}

#if defined(PRINTF_SUPPORT_FLOAT)
case 'f' :
case 'F' :
Expand Down Expand Up @@ -818,7 +823,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
format++;
break;
}

#ifndef PRINTF_VOXLESS
case 'p' : {
width = sizeof(void*) * 2U;
flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
Expand All @@ -836,7 +841,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
format++;
break;
}

#endif // PRINTF_VOXLESS
case '%' :
out('%', buffer, idx++, maxlen);
format++;
Expand All @@ -858,7 +863,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const


///////////////////////////////////////////////////////////////////////////////

#ifndef PRINTF_VOXLESS
int printf_(const char* format, ...)
{
va_list va;
Expand All @@ -868,7 +873,7 @@ int printf_(const char* format, ...)
va_end(va);
return ret;
}

#endif

int sprintf_(char* buffer, const char* format, ...)
{
Expand All @@ -889,20 +894,21 @@ int snprintf_(char* buffer, size_t count, const char* format, ...)
return ret;
}


#ifndef PRINTF_VOXLESS
int vprintf_(const char* format, va_list va)
{
char buffer[1];
return _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
}
#endif


int vsnprintf_(char* buffer, size_t count, const char* format, va_list va)
{
return _vsnprintf(_out_buffer, buffer, count, format, va);
}


#ifndef PRINTF_VOXLESS
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...)
{
va_list va;
Expand All @@ -912,3 +918,4 @@ int fctprintf(void (*out)(char character, void* arg), void* arg, const char* for
va_end(va);
return ret;
}
#endif
8 changes: 4 additions & 4 deletions external/printf/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extern "C" {
*/
void _putchar(char character);


#ifndef PRINTF_VOXLESS
/**
* Tiny printf implementation
* You have to implement _putchar if you use printf()
Expand All @@ -59,7 +59,7 @@ void _putchar(char character);
*/
#define printf printf_
int printf_(const char* format, ...);

#endif

/**
* Tiny sprintf implementation
Expand Down Expand Up @@ -88,6 +88,7 @@ int snprintf_(char* buffer, size_t count, const char* format, ...);
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);


#ifndef PRINTF_VOXLESS
/**
* Tiny vprintf implementation
* \param format A string that specifies the format of the output
Expand All @@ -97,7 +98,6 @@ int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
#define vprintf vprintf_
int vprintf_(const char* format, va_list va);


/**
* printf with output function
* You may use this as dynamic alternative to printf() with its fixed _putchar() output
Expand All @@ -107,7 +107,7 @@ int vprintf_(const char* format, va_list va);
* \return The number of characters that are sent to the output function, not counting the terminating null character
*/
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...);

#endif

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions printf_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
#define PRINTF_DISABLE_SUPPORT_EXPONENTIAL
#define PRINTF_DISABLE_SUPPORT_PTRDIFF_T
#define PRINTF_DISABLE_SUPPORT_FLOAT
#define PRINTF_VOXLESS

0 comments on commit b042743

Please sign in to comment.