The built-in libc library of tbox has its own printf implementation. It also supports some extensions while supporting all standard formatting parameters to support printing custom formatting parameters, for example:
// Number of output points: 3.14tb _ printf ("% {fixed} \ n", tb_float_to_fixed (3.14); // output IPv4 address: 127.0.0.1tb _ 00004_t ADDR; tb_00004_set (& ADDR, "127.0.0.1"); tb_printf ("% {IPv4} \ n", & ADDR );
Both of the above are built-in tbox object parameter printing. You only need to register the object name and object description function that you need to print.
Where%{object_name}
Is the format of custom parameterized object printing. This is an extension of standard formats such as % s and % F, so that you canNSLog(@"%@", object)
In this way, you can easily print the content of a custom object.
For example, if you want to support custom printing of the following content:
typedef struct _rect_t{ tb_long_t x; tb_long_t y; tb_size_t w; tb_size_t h;}rect_t;tb_printf("%{rect}\n", &rect);
Then you only need to provide the corresponding rect object description function and register it:
// The description function of the rect object. The description is formatted into CSTR and the actual string size static tb_long_t tb_printf_rect_func (tb_cpointer_t object, tb_char_t * CSTR, tb_size_t maxn) is returned) {// check tb_assert_and_check_return_val (Object & CSTR & maxn,-1); // The rect rect_t * rect = (rect_t *) object; // format tb_long_t size = tb_snprintf (CSTR, maxn-1, "(X: % lD, Y: % lD, W: % lu, H: % lu )", rect-> X, rect-> Y, rect-> W, rect-> H); If (size> = 0) CSTR [size] = '\ 0'; // OK? Return size;} // register the printf parameterized printing support for the % {rect} object. Note: it is best to register tb_printf_object_register ("rect", tb_printf_rect_func) during program initialization ); // print the rect object information and display: "(X: 10, Y: 10, W: 640, H: 480)" rect_t rect = {10, 10,640,480 }; tb_printf ("% {rect} \ n", & rect );
Of course, not only does printf support custom parameterized object printing, as long as the printf series all support this feature, such as the following interface:
- Tb_printf
- Tb_snprintf
- Tb_vsnprintf
- Tb_wprintf
- Tb_swprintf
- Tb_vswprintf
- Tb_trace_ I
- Tb_trace_d
- Tb_trace_e
- Tb_trace_w
And so on. All these are customizable parameterized object printing. Each object only needs to be registered once.
- Tbox project details
- Tbox project source code
- Tbox Project Document
Use printf to customize printing objects