MeterLogger
Macros | Typedefs | Functions
tinyprintf.h File Reference
#include <stdarg.h>
#include <sys/types.h>
Include dependency graph for tinyprintf.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define TINYPRINTF_DEFINE_TFP_PRINTF   1
 
#define TINYPRINTF_DEFINE_TFP_SPRINTF   1
 
#define TINYPRINTF_OVERRIDE_LIBC   0
 
#define _TFP_SPECIFY_PRINTF_FMT(fmt_idx, arg1_idx)
 

Typedefs

typedef void(* putcf) (void *, char)
 

Functions

ICACHE_FLASH_ATTR void tfp_format (void *putp, putcf putf, const char *fmt, va_list va)
 
ICACHE_FLASH_ATTR int tfp_vsnprintf (char *str, size_t size, const char *fmt, va_list ap)
 
ICACHE_FLASH_ATTR int tfp_snprintf (char *str, size_t size, const char *fmt,...) _TFP_SPECIFY_PRINTF_FMT(3
 
ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR int tfp_vsprintf (char *str, const char *fmt, va_list ap)
 
ICACHE_FLASH_ATTR int tfp_sprintf (char *str, const char *fmt,...) _TFP_SPECIFY_PRINTF_FMT(2
 
ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR void init_printf (void *putp, putcf putf)
 
ICACHE_FLASH_ATTR void tfp_printf (char *fmt,...) _TFP_SPECIFY_PRINTF_FMT(1
 

Macro Definition Documentation

◆ _TFP_SPECIFY_PRINTF_FMT

#define _TFP_SPECIFY_PRINTF_FMT (   fmt_idx,
  arg1_idx 
)

Definition at line 138 of file tinyprintf.h.

◆ TINYPRINTF_DEFINE_TFP_PRINTF

#define TINYPRINTF_DEFINE_TFP_PRINTF   1

Definition at line 110 of file tinyprintf.h.

◆ TINYPRINTF_DEFINE_TFP_SPRINTF

#define TINYPRINTF_DEFINE_TFP_SPRINTF   1

Definition at line 116 of file tinyprintf.h.

◆ TINYPRINTF_OVERRIDE_LIBC

#define TINYPRINTF_OVERRIDE_LIBC   0

Definition at line 123 of file tinyprintf.h.

Typedef Documentation

◆ putcf

typedef void(* putcf) (void *, char)

Definition at line 145 of file tinyprintf.h.

Function Documentation

◆ init_printf()

ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR void init_printf ( void *  putp,
putcf  putf 
)

Definition at line 429 of file tinyprintf.c.

References stdout_putf, and stdout_putp.

430 {
431  stdout_putf = putf;
432  stdout_putp = putp;
433 }
static putcf stdout_putf
Definition: tinyprintf.c:426
static void * stdout_putp
Definition: tinyprintf.c:427

◆ tfp_format()

ICACHE_FLASH_ATTR void tfp_format ( void *  putp,
putcf  putf,
const char *  fmt,
va_list  va 
)

Definition at line 255 of file tinyprintf.c.

References a2u(), param::align_left, param::alt, param::base, param::bf, i2a(), li2a(), lli2a(), param::lz, putchw(), param::sign, param::uc, ui2a(), uli2a(), ulli2a(), and param::width.

Referenced by tfp_printf(), tfp_vsnprintf(), and tfp_vsprintf().

256 {
257  struct param p;
258 #ifdef PRINTF_LONG_SUPPORT
259  char bf[23]; /* long = 64b on some architectures */
260 #else
261  char bf[12]; /* int = 32b on some architectures */
262 #endif
263  char ch;
264  p.bf = bf;
265 
266  while ((ch = *(fmt++))) {
267  if (ch != '%') {
268  putf(putp, ch);
269  } else {
270 #ifdef PRINTF_LONG_SUPPORT
271  char lng = 0; /* 1 for long, 2 for long long */
272 #endif
273  /* Init parameter struct */
274  p.lz = 0;
275  p.alt = 0;
276  p.width = 0;
277  p.align_left = 0;
278  p.sign = 0;
279 
280  /* Flags */
281  while ((ch = *(fmt++))) {
282  switch (ch) {
283  case '-':
284  p.align_left = 1;
285  continue;
286  case '0':
287  p.lz = 1;
288  continue;
289  case '#':
290  p.alt = 1;
291  continue;
292  default:
293  break;
294  }
295  break;
296  }
297 
298  /* Width */
299  if (ch >= '0' && ch <= '9') {
300  ch = a2u(ch, &fmt, 10, &(p.width));
301  }
302 
303  /* We accept 'x.y' format but don't support it completely:
304  * we ignore the 'y' digit => this ignores 0-fill
305  * size and makes it == width (ie. 'x') */
306  if (ch == '.') {
307  p.lz = 1; /* zero-padding */
308  /* ignore actual 0-fill size: */
309  do {
310  ch = *(fmt++);
311  } while ((ch >= '0') && (ch <= '9'));
312  }
313 
314 #ifdef PRINTF_SIZE_T_SUPPORT
315 # ifdef PRINTF_LONG_SUPPORT
316  if (ch == 'z') {
317  ch = *(fmt++);
318  if (sizeof(size_t) == sizeof(unsigned long int))
319  lng = 1;
320 # ifdef PRINTF_LONG_LONG_SUPPORT
321  else if (sizeof(size_t) == sizeof(unsigned long long int))
322  lng = 2;
323 # endif
324  } else
325 # endif
326 #endif
327 
328 #ifdef PRINTF_LONG_SUPPORT
329  if (ch == 'l') {
330  ch = *(fmt++);
331  lng = 1;
332 #ifdef PRINTF_LONG_LONG_SUPPORT
333  if (ch == 'l') {
334  ch = *(fmt++);
335  lng = 2;
336  }
337 #endif
338  }
339 #endif
340  switch (ch) {
341  case 0:
342  goto abort;
343  case 'u':
344  p.base = 10;
345 #ifdef PRINTF_LONG_SUPPORT
346 #ifdef PRINTF_LONG_LONG_SUPPORT
347  if (2 == lng)
348  ulli2a(va_arg(va, unsigned long long int), &p);
349  else
350 #endif
351  if (1 == lng)
352  uli2a(va_arg(va, unsigned long int), &p);
353  else
354 #endif
355  ui2a(va_arg(va, unsigned int), &p);
356  putchw(putp, putf, &p);
357  break;
358  case 'd':
359  case 'i':
360  p.base = 10;
361 #ifdef PRINTF_LONG_SUPPORT
362 #ifdef PRINTF_LONG_LONG_SUPPORT
363  if (2 == lng)
364  lli2a(va_arg(va, long long int), &p);
365  else
366 #endif
367  if (1 == lng)
368  li2a(va_arg(va, long int), &p);
369  else
370 #endif
371  i2a(va_arg(va, int), &p);
372  putchw(putp, putf, &p);
373  break;
374 #ifdef SIZEOF_POINTER
375  case 'p':
376  p.alt = 1;
377 # if defined(SIZEOF_INT) && SIZEOF_POINTER <= SIZEOF_INT
378  lng = 0;
379 # elif defined(SIZEOF_LONG) && SIZEOF_POINTER <= SIZEOF_LONG
380  lng = 1;
381 # elif defined(SIZEOF_LONG_LONG) && SIZEOF_POINTER <= SIZEOF_LONG_LONG
382  lng = 2;
383 # endif
384 #endif
385  case 'x':
386  case 'X':
387  p.base = 16;
388  p.uc = (ch == 'X')?1:0;
389 #ifdef PRINTF_LONG_SUPPORT
390 #ifdef PRINTF_LONG_LONG_SUPPORT
391  if (2 == lng)
392  ulli2a(va_arg(va, unsigned long long int), &p);
393  else
394 #endif
395  if (1 == lng)
396  uli2a(va_arg(va, unsigned long int), &p);
397  else
398 #endif
399  ui2a(va_arg(va, unsigned int), &p);
400  putchw(putp, putf, &p);
401  break;
402  case 'o':
403  p.base = 8;
404  ui2a(va_arg(va, unsigned int), &p);
405  putchw(putp, putf, &p);
406  break;
407  case 'c':
408  putf(putp, (char)(va_arg(va, int)));
409  break;
410  case 's':
411  p.bf = va_arg(va, char *);
412  putchw(putp, putf, &p);
413  p.bf = bf;
414  break;
415  case '%':
416  putf(putp, ch);
417  default:
418  break;
419  }
420  }
421  }
422  abort:;
423 }
static ICACHE_FLASH_ATTR void lli2a(long long int num, struct param *p)
Definition: tinyprintf.c:107
static ICACHE_FLASH_ATTR void _TFP_GCC_NO_INLINE_ ulli2a(unsigned long long int num, struct param *p)
Definition: tinyprintf.c:87
static ICACHE_FLASH_ATTR void uli2a(unsigned long int num, struct param *p)
Definition: tinyprintf.c:118
static ICACHE_FLASH_ATTR char a2u(char ch, const char **src, int base, unsigned int *nump)
Definition: tinyprintf.c:187
static ICACHE_FLASH_ATTR void putchw(void *putp, putcf putf, struct param *p)
Definition: tinyprintf.c:203
static ICACHE_FLASH_ATTR void ui2a(unsigned int num, struct param *p)
Definition: tinyprintf.c:147
char * bf
Definition: tinyprintf.c:82
static ICACHE_FLASH_ATTR void li2a(long num, struct param *p)
Definition: tinyprintf.c:137
static ICACHE_FLASH_ATTR void i2a(int num, struct param *p)
Definition: tinyprintf.c:166
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tfp_printf()

ICACHE_FLASH_ATTR void tfp_printf ( char *  fmt,
  ... 
)

◆ tfp_snprintf()

ICACHE_FLASH_ATTR int tfp_snprintf ( char *  str,
size_t  size,
const char *  fmt,
  ... 
)

◆ tfp_sprintf()

ICACHE_FLASH_ATTR int tfp_sprintf ( char *  str,
const char *  fmt,
  ... 
)

◆ tfp_vsnprintf()

ICACHE_FLASH_ATTR int tfp_vsnprintf ( char *  str,
size_t  size,
const char *  fmt,
va_list  ap 
)

Definition at line 460 of file tinyprintf.c.

References _vsnprintf_putcf(), _vsnprintf_putcf_data::dest, _vsnprintf_putcf_data::dest_capacity, _vsnprintf_putcf_data::num_chars, and tfp_format().

Referenced by tfp_snprintf().

461 {
462  struct _vsnprintf_putcf_data data;
463 
464  if (size < 1)
465  return 0;
466 
467  data.dest = str;
468  data.dest_capacity = size-1;
469  data.num_chars = 0;
470  tfp_format(&data, _vsnprintf_putcf, format, ap);
471 
472  if (data.num_chars < data.dest_capacity)
473  data.dest[data.num_chars] = '\0';
474  else
475  data.dest[data.dest_capacity] = '\0';
476 
477  return data.num_chars;
478 }
ICACHE_FLASH_ATTR void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
Definition: tinyprintf.c:255
static ICACHE_FLASH_ATTR void _vsnprintf_putcf(void *p, char c)
Definition: tinyprintf.c:452
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tfp_vsprintf()

ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR int tfp_vsprintf ( char *  str,
const char *  fmt,
va_list  ap 
)

Definition at line 503 of file tinyprintf.c.

References _vsprintf_putcf(), _vsprintf_putcf_data::dest, _vsprintf_putcf_data::num_chars, and tfp_format().

Referenced by tfp_sprintf().

504 {
505  struct _vsprintf_putcf_data data;
506  data.dest = str;
507  data.num_chars = 0;
508  tfp_format(&data, _vsprintf_putcf, format, ap);
509  data.dest[data.num_chars] = '\0';
510  return data.num_chars;
511 }
ICACHE_FLASH_ATTR void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
Definition: tinyprintf.c:255
static ICACHE_FLASH_ATTR void _vsprintf_putcf(void *p, char c)
Definition: tinyprintf.c:497
Here is the call graph for this function:
Here is the caller graph for this function: