Line data Source code
1 : #ifndef HEADER_fd_src_disco_topo_fd_pod_format_h
2 : #define HEADER_fd_src_disco_topo_fd_pod_format_h
3 :
4 : #include "../../util/pod/fd_pod.h"
5 :
6 : #include <stdarg.h>
7 : #include <stdio.h>
8 :
9 : /* fd_pod_insertf_[type] inserts the [type] val into the pod at the
10 : given path. The path is constructed from the given format string.
11 : Returns offset where val was inserted, 0 on failure. The inserted
12 : representation might be compressed. This offset is valid for the
13 : pod's lifetime or an invalidating operation is done on the pod.
14 :
15 : IMPORTANT! THIS IS AN INVALIDATING OPERATION */
16 :
17 : #define FD_POD_IMPL(type,TYPE) \
18 : __attribute__ ((format (printf, 3, 4))) \
19 : static inline ulong \
20 : fd_pod_insertf_##type( uchar * FD_RESTRICT pod, \
21 : type val, \
22 282 : char const * FD_RESTRICT fmt, ... ) { \
23 282 : va_list ap; \
24 282 : va_start( ap, fmt ); \
25 282 : char buf[ 128UL ]; \
26 282 : int ret = vsnprintf( buf, 128UL, fmt, ap ); \
27 282 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, 128UL-1UL ) ); \
28 282 : buf[ len ] = '\0'; \
29 282 : va_end( ap ); \
30 282 : if( FD_UNLIKELY( ret<0 || (ulong)ret>=128UL ) ) return 0UL; \
31 282 : return fd_pod_insert_##type( pod, buf, val ); \
32 282 : }
33 :
34 : FD_POD_IMPL( ushort, USHORT )
35 : FD_POD_IMPL( uint, UINT )
36 : FD_POD_IMPL( ulong, ULONG )
37 : FD_POD_IMPL( short, SHORT )
38 : FD_POD_IMPL( int, INT )
39 : FD_POD_IMPL( long, LONG )
40 : FD_POD_IMPL( char, CHAR )
41 : FD_POD_IMPL( schar, SCHAR )
42 : FD_POD_IMPL( uchar, UCHAR )
43 : FD_POD_IMPL( float, FLOAT )
44 : #if FD_HAS_DOUBLE
45 : FD_POD_IMPL( double, DOUBLE )
46 : #endif
47 :
48 : #undef FD_POD_IMPL
49 :
50 : __attribute__ ((format (printf, 3, 4)))
51 : static inline ulong
52 : fd_pod_insertf_cstr( uchar * FD_RESTRICT pod,
53 : char const * FD_RESTRICT str,
54 0 : char const * FD_RESTRICT fmt, ... ) {
55 0 : va_list ap;
56 0 : va_start( ap, fmt );
57 0 : char buf[ 128UL ];
58 0 : int ret = vsnprintf( buf, 128UL, fmt, ap );
59 0 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, 128UL-1UL ) );
60 0 : buf[ len ] = '\0';
61 0 : va_end( ap );
62 0 : if( FD_UNLIKELY( ret<0 || (ulong)ret>=128UL ) ) return 0UL;
63 0 : return fd_pod_insert_cstr( pod, buf, str );
64 0 : }
65 :
66 : /* fd_pod_replacef_[type] replaces the [type] val into the pod at the
67 : given path. The path is constructed from the given format string.
68 : If the path does not exist, it is created. Returns FD_POD_SUCCESS
69 : on success, or FD_POD_ERR_* on failure.
70 :
71 : IMPORTANT! THIS IS AN INVALIDATING OPERATION */
72 :
73 : #define FD_POD_IMPL(type,TYPE) \
74 : __attribute__ ((format (printf, 3, 4))) \
75 : static inline int \
76 : fd_pod_replacef_##type( uchar * FD_RESTRICT pod, \
77 : type val, \
78 102 : char const * FD_RESTRICT fmt, ... ) { \
79 102 : va_list ap; \
80 102 : va_start( ap, fmt ); \
81 102 : char buf[ 128UL ]; \
82 102 : int ret = vsnprintf( buf, 128UL, fmt, ap ); \
83 102 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, 128UL-1UL ) ); \
84 102 : buf[ len ] = '\0'; \
85 102 : va_end( ap ); \
86 102 : if( FD_UNLIKELY( ret<0 || (ulong)ret>=128UL ) ) return 0UL; \
87 102 : int result = fd_pod_remove( pod, buf ); \
88 102 : if( FD_UNLIKELY( result!=FD_POD_SUCCESS && result!=FD_POD_ERR_RESOLVE ) ) \
89 102 : return result; \
90 102 : if( FD_UNLIKELY( !fd_pod_insert_##type( pod, buf, val ) ) ) \
91 102 : return FD_POD_ERR_FULL; \
92 102 : return FD_POD_SUCCESS; \
93 102 : }
94 :
95 : FD_POD_IMPL( ushort, USHORT )
96 : FD_POD_IMPL( uint, UINT )
97 : FD_POD_IMPL( ulong, ULONG )
98 : FD_POD_IMPL( short, SHORT )
99 : FD_POD_IMPL( int, INT )
100 : FD_POD_IMPL( long, LONG )
101 : FD_POD_IMPL( char, CHAR )
102 : FD_POD_IMPL( schar, SCHAR )
103 : FD_POD_IMPL( uchar, UCHAR )
104 : FD_POD_IMPL( float, FLOAT )
105 : #if FD_HAS_DOUBLE
106 : FD_POD_IMPL( double, DOUBLE )
107 : #endif
108 :
109 : #undef FD_POD_IMPL
110 :
111 : /* fd_pod_queryf_[type] queries for the [type] in pod at path. The path
112 : is constructed from the given format string. Returns the query
113 : result on success or def on failure. */
114 :
115 : #define FD_POD_IMPL(type,TYPE) \
116 : __attribute__ ((format (printf, 3, 4))) \
117 : static inline type \
118 : fd_pod_queryf_##type( uchar const * FD_RESTRICT pod, \
119 : type def, \
120 717 : char const * FD_RESTRICT fmt, ... ) { \
121 717 : va_list ap; \
122 717 : va_start( ap, fmt ); \
123 717 : char buf[ 128UL ]; \
124 717 : int ret = vsnprintf( buf, 128UL, fmt, ap ); \
125 717 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, 128UL-1UL ) ); \
126 717 : buf[ len ] = '\0'; \
127 717 : va_end( ap ); \
128 717 : if( FD_UNLIKELY( ret<0 || (ulong)ret>=128UL ) ) return 0UL; \
129 717 : return fd_pod_query_##type( pod, buf, def ); \
130 717 : }
131 :
132 : FD_POD_IMPL( ushort, USHORT )
133 : FD_POD_IMPL( uint, UINT )
134 : FD_POD_IMPL( ulong, ULONG )
135 : FD_POD_IMPL( short, SHORT )
136 : FD_POD_IMPL( int, INT )
137 : FD_POD_IMPL( long, LONG )
138 : FD_POD_IMPL( char, CHAR )
139 : FD_POD_IMPL( schar, SCHAR )
140 : FD_POD_IMPL( uchar, UCHAR )
141 : FD_POD_IMPL( float, FLOAT )
142 : #if FD_HAS_DOUBLE
143 : FD_POD_IMPL( double, DOUBLE )
144 : #endif
145 :
146 : #undef FD_POD_IMPL
147 :
148 : __attribute__ ((format (printf, 3, 4)))
149 : static inline char const *
150 : fd_pod_queryf_cstr( uchar const * FD_RESTRICT pod,
151 : char const * FD_RESTRICT def,
152 0 : char const * FD_RESTRICT fmt, ... ) {
153 0 : va_list ap;
154 0 : va_start( ap, fmt );
155 0 : char buf[ 128UL ];
156 0 : int ret = vsnprintf( buf, 128UL, fmt, ap );
157 0 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, 128UL-1UL ) );
158 0 : buf[ len ] = '\0';
159 0 : va_end( ap );
160 0 : if( FD_UNLIKELY( ret<0 || (ulong)ret>=128UL ) ) return 0UL;
161 0 : return fd_pod_query_cstr( pod, buf, def );
162 0 : }
163 :
164 : #endif /* HEADER_fd_src_disco_topo_fd_pod_format_h */
|