MeterLogger
Data Structures | Macros | Functions | Variables
tinyprintf.c File Reference
#include <esp8266.h>
#include "tinyprintf.h"
#include <sys/types.h>
Include dependency graph for tinyprintf.c:

Go to the source code of this file.

Data Structures

struct  param
 
struct  _vsnprintf_putcf_data
 
struct  _vsprintf_putcf_data
 

Macros

#define PRINTF_LONG_SUPPORT
 
#define PRINTF_LONG_LONG_SUPPORT
 
#define PRINTF_SIZE_T_SUPPORT
 
#define PRINTF_LONG_SUPPORT
 
#define _TFP_GCC_NO_INLINE_
 

Functions

static ICACHE_FLASH_ATTR void _TFP_GCC_NO_INLINE_ ulli2a (unsigned long long int num, struct param *p)
 
static ICACHE_FLASH_ATTR void lli2a (long long int num, struct param *p)
 
static ICACHE_FLASH_ATTR void uli2a (unsigned long int num, struct param *p)
 
static ICACHE_FLASH_ATTR void li2a (long num, struct param *p)
 
static ICACHE_FLASH_ATTR void ui2a (unsigned int num, struct param *p)
 
static ICACHE_FLASH_ATTR void i2a (int num, struct param *p)
 
static ICACHE_FLASH_ATTR int a2d (char ch)
 
static ICACHE_FLASH_ATTR char a2u (char ch, const char **src, int base, unsigned int *nump)
 
static ICACHE_FLASH_ATTR void putchw (void *putp, putcf putf, struct param *p)
 
ICACHE_FLASH_ATTR void tfp_format (void *putp, putcf putf, const char *fmt, va_list va)
 
ICACHE_FLASH_ATTR void init_printf (void *putp, putcf putf)
 
ICACHE_FLASH_ATTR void tfp_printf (char *fmt,...)
 
static ICACHE_FLASH_ATTR void _vsnprintf_putcf (void *p, char c)
 
ICACHE_FLASH_ATTR int tfp_vsnprintf (char *str, size_t size, const char *format, va_list ap)
 
ICACHE_FLASH_ATTR int tfp_snprintf (char *str, size_t size, const char *format,...)
 
static ICACHE_FLASH_ATTR void _vsprintf_putcf (void *p, char c)
 
ICACHE_FLASH_ATTR int tfp_vsprintf (char *str, const char *format, va_list ap)
 
ICACHE_FLASH_ATTR int tfp_sprintf (char *str, const char *format,...)
 

Variables

static putcf stdout_putf
 
static void * stdout_putp
 

Macro Definition Documentation

◆ _TFP_GCC_NO_INLINE_

#define _TFP_GCC_NO_INLINE_

Definition at line 68 of file tinyprintf.c.

◆ PRINTF_LONG_LONG_SUPPORT

#define PRINTF_LONG_LONG_SUPPORT

Definition at line 35 of file tinyprintf.c.

◆ PRINTF_LONG_SUPPORT [1/2]

#define PRINTF_LONG_SUPPORT

Definition at line 48 of file tinyprintf.c.

◆ PRINTF_LONG_SUPPORT [2/2]

#define PRINTF_LONG_SUPPORT

Definition at line 48 of file tinyprintf.c.

◆ PRINTF_SIZE_T_SUPPORT

#define PRINTF_SIZE_T_SUPPORT

Definition at line 38 of file tinyprintf.c.

Function Documentation

◆ _vsnprintf_putcf()

static ICACHE_FLASH_ATTR void _vsnprintf_putcf ( void *  p,
char  c 
)
static

Definition at line 452 of file tinyprintf.c.

References _vsnprintf_putcf_data::dest, _vsnprintf_putcf_data::dest_capacity, and _vsnprintf_putcf_data::num_chars.

Referenced by tfp_vsnprintf().

453 {
454  struct _vsnprintf_putcf_data *data = (struct _vsnprintf_putcf_data*)p;
455  if (data->num_chars < data->dest_capacity)
456  data->dest[data->num_chars] = c;
457  data->num_chars ++;
458 }
Here is the caller graph for this function:

◆ _vsprintf_putcf()

static ICACHE_FLASH_ATTR void _vsprintf_putcf ( void *  p,
char  c 
)
static

Definition at line 497 of file tinyprintf.c.

References _vsprintf_putcf_data::dest, and _vsprintf_putcf_data::num_chars.

Referenced by tfp_vsprintf().

498 {
499  struct _vsprintf_putcf_data *data = (struct _vsprintf_putcf_data*)p;
500  data->dest[data->num_chars++] = c;
501 }
Here is the caller graph for this function:

◆ a2d()

static ICACHE_FLASH_ATTR int a2d ( char  ch)
static

Definition at line 175 of file tinyprintf.c.

Referenced by a2u().

176 {
177  if (ch >= '0' && ch <= '9')
178  return ch - '0';
179  else if (ch >= 'a' && ch <= 'f')
180  return ch - 'a' + 10;
181  else if (ch >= 'A' && ch <= 'F')
182  return ch - 'A' + 10;
183  else
184  return -1;
185 }
Here is the caller graph for this function:

◆ a2u()

static ICACHE_FLASH_ATTR char a2u ( char  ch,
const char **  src,
int  base,
unsigned int *  nump 
)
static

Definition at line 187 of file tinyprintf.c.

References a2d().

Referenced by tfp_format().

188 {
189  const char *p = *src;
190  unsigned int num = 0;
191  int digit;
192  while ((digit = a2d(ch)) >= 0) {
193  if (digit > base)
194  break;
195  num = num * base + digit;
196  ch = *p++;
197  }
198  *src = p;
199  *nump = num;
200  return ch;
201 }
static ICACHE_FLASH_ATTR int a2d(char ch)
Definition: tinyprintf.c:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ i2a()

static ICACHE_FLASH_ATTR void i2a ( int  num,
struct param p 
)
static

Definition at line 166 of file tinyprintf.c.

References param::sign, and ui2a().

Referenced by tfp_format().

167 {
168  if (num < 0) {
169  num = -num;
170  p->sign = '-';
171  }
172  ui2a(num, p);
173 }
char sign
Definition: tinyprintf.c:80
static ICACHE_FLASH_ATTR void ui2a(unsigned int num, struct param *p)
Definition: tinyprintf.c:147
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_printf()

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

◆ li2a()

static ICACHE_FLASH_ATTR void li2a ( long  num,
struct param p 
)
static

Definition at line 137 of file tinyprintf.c.

References param::sign, and uli2a().

Referenced by tfp_format().

138 {
139  if (num < 0) {
140  num = -num;
141  p->sign = '-';
142  }
143  uli2a(num, p);
144 }
static ICACHE_FLASH_ATTR void uli2a(unsigned long int num, struct param *p)
Definition: tinyprintf.c:118
char sign
Definition: tinyprintf.c:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lli2a()

static ICACHE_FLASH_ATTR void lli2a ( long long int  num,
struct param p 
)
static

Definition at line 107 of file tinyprintf.c.

References param::sign, and ulli2a().

Referenced by tfp_format().

108 {
109  if (num < 0) {
110  num = -num;
111  p->sign = '-';
112  }
113  ulli2a(num, p);
114 }
static ICACHE_FLASH_ATTR void _TFP_GCC_NO_INLINE_ ulli2a(unsigned long long int num, struct param *p)
Definition: tinyprintf.c:87
char sign
Definition: tinyprintf.c:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ putchw()

static ICACHE_FLASH_ATTR void putchw ( void *  putp,
putcf  putf,
struct param p 
)
static

Definition at line 203 of file tinyprintf.c.

References param::align_left, param::alt, param::base, param::bf, param::lz, param::sign, param::uc, and param::width.

Referenced by tfp_format().

204 {
205  char ch;
206  int n = p->width;
207  char *bf = p->bf;
208 
209  /* Number of filling characters */
210  while (*bf++ && n > 0)
211  n--;
212  if (p->sign)
213  n--;
214  if (p->alt && p->base == 16)
215  n -= 2;
216  else if (p->alt && p->base == 8)
217  n--;
218 
219  /* Fill with space to align to the right, before alternate or sign */
220  if (!p->lz && !p->align_left) {
221  while (n-- > 0)
222  putf(putp, ' ');
223  }
224 
225  /* print sign */
226  if (p->sign)
227  putf(putp, p->sign);
228 
229  /* Alternate */
230  if (p->alt && p->base == 16) {
231  putf(putp, '0');
232  putf(putp, (p->uc ? 'X' : 'x'));
233  } else if (p->alt && p->base == 8) {
234  putf(putp, '0');
235  }
236 
237  /* Fill with zeros, after alternate or sign */
238  if (p->lz) {
239  while (n-- > 0)
240  putf(putp, '0');
241  }
242 
243  /* Put actual buffer */
244  bf = p->bf;
245  while ((ch = *bf++))
246  putf(putp, ch);
247 
248  /* Fill with space to align to the left, after string */
249  if (!p->lz && p->align_left) {
250  while (n-- > 0)
251  putf(putp, ' ');
252  }
253 }
char uc
Definition: tinyprintf.c:77
char sign
Definition: tinyprintf.c:80
char alt
Definition: tinyprintf.c:76
unsigned int base
Definition: tinyprintf.c:81
char align_left
Definition: tinyprintf.c:78
unsigned int width
Definition: tinyprintf.c:79
char * bf
Definition: tinyprintf.c:82
char lz
Definition: tinyprintf.c:75
Here is the caller graph for this function:

◆ 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,
  ... 
)

Definition at line 435 of file tinyprintf.c.

References stdout_putf, stdout_putp, and tfp_format().

436 {
437  va_list va;
438  va_start(va, fmt);
440  va_end(va);
441 }
ICACHE_FLASH_ATTR void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
Definition: tinyprintf.c:255
static putcf stdout_putf
Definition: tinyprintf.c:426
static void * stdout_putp
Definition: tinyprintf.c:427
Here is the call graph for this function:

◆ tfp_snprintf()

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

Definition at line 480 of file tinyprintf.c.

References tfp_vsnprintf().

Referenced by cfg_load(), cgiWiFiScan(), cleanup_decimal_str(), divide_str_by_10(), divide_str_by_100(), en61107_received_task(), en61107_response_set_unit(), en61107_response_set_value(), httpdHeader(), httpdRedirect(), httpdStartResponse(), kmp_received_task(), kmp_request_send(), kmp_value_to_string(), kw_to_w_str(), parse_mc66cde_inst_values_frame(), parse_mc66cde_standard_data_1_frame(), tplSetup(), w_to_kw_str(), wifi_connect(), wifi_default(), wifi_fallback(), and wifi_softap_config().

481 {
482  va_list ap;
483  int retval;
484 
485  va_start(ap, format);
486  retval = tfp_vsnprintf(str, size, format, ap);
487  va_end(ap);
488  return retval;
489 }
ICACHE_FLASH_ATTR int tfp_vsnprintf(char *str, size_t size, const char *format, va_list ap)
Definition: tinyprintf.c:460
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tfp_sprintf()

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

Definition at line 513 of file tinyprintf.c.

References tfp_vsprintf().

514 {
515  va_list ap;
516  int retval;
517 
518  va_start(ap, format);
519  retval = tfp_vsprintf(str, format, ap);
520  va_end(ap);
521  return retval;
522 }
ICACHE_FLASH_ATTR int tfp_vsprintf(char *str, const char *format, va_list ap)
Definition: tinyprintf.c:503
Here is the call graph for this function:

◆ tfp_vsnprintf()

ICACHE_FLASH_ATTR int tfp_vsnprintf ( char *  str,
size_t  size,
const char *  format,
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 tfp_vsprintf ( char *  str,
const char *  format,
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:

◆ ui2a()

static ICACHE_FLASH_ATTR void ui2a ( unsigned int  num,
struct param p 
)
static

Definition at line 147 of file tinyprintf.c.

References param::base, param::bf, and param::uc.

Referenced by i2a(), and tfp_format().

148 {
149  int n = 0;
150  unsigned int d = 1;
151  char *bf = p->bf;
152  while (num / d >= p->base)
153  d *= p->base;
154  while (d != 0) {
155  int dgt = num / d;
156  num %= d;
157  d /= p->base;
158  if (n || dgt > 0 || d == 0) {
159  *bf++ = dgt + (dgt < 10 ? '0' : (p->uc ? 'A' : 'a') - 10);
160  ++n;
161  }
162  }
163  *bf = 0;
164 }
char uc
Definition: tinyprintf.c:77
unsigned int base
Definition: tinyprintf.c:81
char * bf
Definition: tinyprintf.c:82
Here is the caller graph for this function:

◆ uli2a()

static ICACHE_FLASH_ATTR void uli2a ( unsigned long int  num,
struct param p 
)
static

Definition at line 118 of file tinyprintf.c.

References param::base, param::bf, and param::uc.

Referenced by li2a(), and tfp_format().

119 {
120  int n = 0;
121  unsigned long int d = 1;
122  char *bf = p->bf;
123  while (num / d >= p->base)
124  d *= p->base;
125  while (d != 0) {
126  int dgt = num / d;
127  num %= d;
128  d /= p->base;
129  if (n || dgt > 0 || d == 0) {
130  *bf++ = dgt + (dgt < 10 ? '0' : (p->uc ? 'A' : 'a') - 10);
131  ++n;
132  }
133  }
134  *bf = 0;
135 }
char uc
Definition: tinyprintf.c:77
unsigned int base
Definition: tinyprintf.c:81
char * bf
Definition: tinyprintf.c:82
Here is the caller graph for this function:

◆ ulli2a()

static ICACHE_FLASH_ATTR void _TFP_GCC_NO_INLINE_ ulli2a ( unsigned long long int  num,
struct param p 
)
static

Definition at line 87 of file tinyprintf.c.

References param::base, param::bf, and param::uc.

Referenced by lli2a(), and tfp_format().

89 {
90  int n = 0;
91  unsigned long long int d = 1;
92  char *bf = p->bf;
93  while (num / d >= p->base)
94  d *= p->base;
95  while (d != 0) {
96  int dgt = num / d;
97  num %= d;
98  d /= p->base;
99  if (n || dgt > 0 || d == 0) {
100  *bf++ = dgt + (dgt < 10 ? '0' : (p->uc ? 'A' : 'a') - 10);
101  ++n;
102  }
103  }
104  *bf = 0;
105 }
char uc
Definition: tinyprintf.c:77
unsigned int base
Definition: tinyprintf.c:81
char * bf
Definition: tinyprintf.c:82
Here is the caller graph for this function:

Variable Documentation

◆ stdout_putf

putcf stdout_putf
static

Definition at line 426 of file tinyprintf.c.

Referenced by init_printf(), and tfp_printf().

◆ stdout_putp

void* stdout_putp
static

Definition at line 427 of file tinyprintf.c.

Referenced by init_printf(), and tfp_printf().