CLibs
Loading...
Searching...
No Matches
item_print_functions.h
Go to the documentation of this file.
1/*
2 * Functions for printing simple data.
3 *
4 * Comes with the most common types pre-defined, and macros to define more.
5 */
6
7#ifndef CLIBS_PRINT_FUNCTIONS_H
8#define CLIBS_PRINT_FUNCTIONS_H
9
10#include "headers/attributes.h"
11#include "headers/errors.h"
12
13#include <stdbool.h>
14
15
17typedef void ( *PrintFunction )( const void *, size_t );
18
21
22#ifndef TYPE_SEPARATOR
24#define TYPE_SEPARATOR ": "
25#endif //TYPE_SEPARATOR
26
27
29#define ITEM_PRINT_FUNCTION_NAME( type ) Print_##type
30
32#define DECLARE_PRINT_FUNC( name ) \
33 void ITEM_PRINT_FUNCTION_NAME( name )( const void *, size_t )
34
35/* Default */
37
38/* Basic types */
43DECLARE_PRINT_FUNC( int8_t );
48
49/* Special */
51DECLARE_PRINT_FUNC( string_direct );
52
53
60#define DEFINE_PRINT_FUNC( type, format_str ) \
61 void ITEM_PRINT_FUNCTION_NAME( type )( const void *data, const size_t nbytes ) \
62 { \
63 if ( nbytes != sizeof( type ) ) \
64 { \
65 fwarnx( "invalid data" ); \
66 return; \
67 } \
68 \
69 if ( PrintFunctions_DoPrintType ) \
70 { \
71 printf( "‹" ); \
72 printf( #type TYPE_SEPARATOR ); \
73 } \
74 printf( format_str, *( ( type * ) data ) ); \
75 if ( PrintFunctions_DoPrintType ) \
76 printf( "›" ); \
77 }
78
79#endif //CLIBS_PRINT_FUNCTIONS_H
Function and variable attributes.
#define LibraryDefined
Definition attributes.h:107
#define DECLARE_PRINT_FUNC(name)
Declares a PrintFunction
Definition item_print_functions.h:32
LibraryDefined bool PrintFunctions_DoPrintType
If true, the functions defined by DEFINE_PRINT_FUNC also print the type name.
Definition item_print_functions.h:20
void(* PrintFunction)(const void *, size_t)
Interface for functions that print data.
Definition item_print_functions.h:17