/** Structure for specifying output methods for different printf clones. */
struct printf_spec {
/* Output function, returns count of printed characters or EOF. */
- int (*write) (void *, size_t, void *);
+ int (*write) (const char *, size_t, void *);
/* Support data - output stream specification, its state, locks, ... */
void *data;
};
PrintfQualifierPointer,
} qualifier_t;
-static char digits_small[] = "0123456789abcdef";
-static char digits_big[] = "0123456789ABCDEF";
+static const char digits_small[] = "0123456789abcdef";
+static const char digits_big[] = "0123456789ABCDEF";
/**
* Print one or more characters without adding newline.
static int printf_putnchars(const char *buf, size_t count,
struct printf_spec *ps)
{
- return ps->write((void *)buf, count, ps->data);
+ return ps->write(buf, count, ps->data);
}
/**
* @param ps Write function specification and support data.
* @return Number of characters printed.
*/
-static int printf_putstr(const char *str, struct printf_spec *ps)
+static inline int printf_putstr(const char *str, struct printf_spec *ps)
{
- size_t count;
-
- if (str == NULL) {
- const char *nullstr = "(NULL)";
- return printf_putnchars(nullstr, strlen(nullstr), ps);
- }
-
- count = strlen(str);
-
- return ps->write((void *)str, count, ps->data);
+ return printf_putnchars(str, strlen(str), ps);
}
/**
*/
static int printf_putchar(int c, struct printf_spec *ps)
{
- unsigned char ch = c;
+ char ch = c;
- return ps->write((void *)&ch, 1, ps->data);
+ return ps->write(&ch, 1, ps->data);
}
/**
static int print_number(uint64_t num, int width, int precision, int base,
uint64_t flags, struct printf_spec *ps)
{
- char *digits = digits_small;
+ const char *digits = digits_small;
char d[PRINT_NUMBER_BUFFER_SIZE];
char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
int size = 0; /* Size of number with all prefixes and signs. */
*
* @param str Source string to print.
* @param count Size of source string.
- * @param data Structure with destination string, counter of used space
+ * @param _data Structure with destination string, counter of used space
* and total string size.
* @return Number of characters to print (not characters really printed!).
*/
-static int vsnprintf_write(const char *str, size_t count,
- struct vsnprintf_data *data)
+static int vsnprintf_write(const char *str, size_t count, void *_data)
{
+ struct vsnprintf_data *data = _data;
size_t i;
i = data->size - data->len;
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
{
struct vsnprintf_data data = { size, 0, str };
- struct printf_spec ps =
- { (int (*)(void *, size_t, void *))vsnprintf_write, &data };
+ struct printf_spec ps = { vsnprintf_write, &data };
/* Print 0 at end of string - fix case that nothing will be printed. */
if (size > 0)
int vprintf(const char *fmt, va_list ap)
{
- struct printf_spec ps =
- { (int (*)(void *, size_t, void *))vprintf_write, NULL };
+ struct printf_spec ps = { vprintf_write, NULL };
return printf_core(fmt, &ps, ap);
}