Main: add example "printf_fmt"

This commit is contained in:
Alex Kotov 2022-05-27 02:17:29 +03:00
parent 841d6991e7
commit f34b66479a
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 72 additions and 0 deletions

1
.gitignore vendored
View File

@ -96,6 +96,7 @@
/examples/panic_simple
/examples/pfa
/examples/printf
/examples/printf_fmt
/examples/printf_va
/examples/snprintf
/examples/snprintf_va

View File

@ -140,6 +140,9 @@ endif
if WITH_PRINTF_FMT
libkernaux_a_SOURCES += src/printf_fmt.c
if ENABLE_TESTS
TESTS += examples/printf_fmt
endif
endif
if WITH_UNITS
@ -179,6 +182,10 @@ examples_printf_SOURCES = \
$(libkernaux_a_SOURCES) \
examples/printf.c
examples_printf_fmt_SOURCES = \
$(libkernaux_a_SOURCES) \
examples/printf_fmt.c
examples_printf_va_SOURCES = \
$(libkernaux_a_SOURCES) \
examples/printf_va.c

View File

@ -63,6 +63,7 @@ zero). Work-in-progress APIs can change at any time.
* [To human](/examples/units_human.c)
* [printf format parser](/include/kernaux/printf_fmt.h)
* Code from [https://github.com/mpaland/printf](https://github.com/mpaland/printf). Thank you!
* [Example](/examples/printf_fmt.c)
* Usual functions
* [libc replacement](/include/kernaux/libc.h) (*stable since* **0.1.0**)
* [itoa/ftoa replacement](/include/kernaux/ntoa.h) (*stable since* **0.1.0**)

63
examples/printf_fmt.c Normal file
View File

@ -0,0 +1,63 @@
#include <kernaux/printf_fmt.h>
#include <assert.h>
int main()
{
{
const char *format = "012.34f";
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create();
KernAux_PrintfFmt_Spec_eval_flags(&spec, &format);
if (KernAux_PrintfFmt_Spec_eval_width1(&spec, &format)) {
KernAux_PrintfFmt_Spec_eval_width2(&spec, &format, 0);
}
if (KernAux_PrintfFmt_Spec_eval_precision1(&spec, &format)) {
KernAux_PrintfFmt_Spec_eval_precision2(&spec, &format, 0);
}
KernAux_PrintfFmt_Spec_eval_length(&spec, &format);
KernAux_PrintfFmt_Spec_eval_type(&spec, &format);
assert(
spec.flags ==
(
KERNAUX_PRINTF_FMT_FLAGS_ZEROPAD |
KERNAUX_PRINTF_FMT_FLAGS_PRECISION
)
);
assert(spec.width == 12);
assert(spec.precision == 34);
assert(spec.type == KERNAUX_PRINTF_FMT_TYPE_FLOAT);
assert(spec.base == 0);
}
{
const char *format = " *.*ld";
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create();
KernAux_PrintfFmt_Spec_eval_flags(&spec, &format);
if (KernAux_PrintfFmt_Spec_eval_width1(&spec, &format)) {
KernAux_PrintfFmt_Spec_eval_width2(&spec, &format, 12);
}
if (KernAux_PrintfFmt_Spec_eval_precision1(&spec, &format)) {
KernAux_PrintfFmt_Spec_eval_precision2(&spec, &format, 34);
}
KernAux_PrintfFmt_Spec_eval_length(&spec, &format);
KernAux_PrintfFmt_Spec_eval_type(&spec, &format);
assert(
spec.flags ==
(
KERNAUX_PRINTF_FMT_FLAGS_SPACE |
KERNAUX_PRINTF_FMT_FLAGS_LONG |
KERNAUX_PRINTF_FMT_FLAGS_PRECISION
)
);
assert(spec.width == 12);
assert(spec.precision == 34);
assert(spec.type == KERNAUX_PRINTF_FMT_TYPE_INT);
assert(spec.base == 10);
}
return 0;
}