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) \
18 : __attribute__ ((format (printf, 3, 4))) \
19 : static inline ulong \
20 : fd_pod_insertf_##type( uchar * FD_RESTRICT pod, \
21 : type val, \
22 495 : char const * FD_RESTRICT fmt, ... ) { \
23 495 : va_list ap; \
24 495 : va_start( ap, fmt ); \
25 495 : char buf[ 128UL ]; \
26 495 : int ret = vsnprintf( buf, 128UL, fmt, ap ); \
27 495 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, 128UL-1UL ) ); \
28 495 : buf[ len ] = '\0'; \
29 495 : va_end( ap ); \
30 495 : if( FD_UNLIKELY( ret<0 || (ulong)ret>=128UL ) ) return 0UL; \
31 495 : return fd_pod_insert_##type( pod, buf, val ); \
32 495 : }
33 :
34 : FD_POD_IMPL( ushort )
35 : FD_POD_IMPL( uint )
36 : FD_POD_IMPL( ulong )
37 : FD_POD_IMPL( short )
38 : FD_POD_IMPL( int )
39 : FD_POD_IMPL( long )
40 : FD_POD_IMPL( char )
41 : FD_POD_IMPL( schar )
42 : FD_POD_IMPL( uchar )
43 : FD_POD_IMPL( float )
44 : #if FD_HAS_DOUBLE
45 : FD_POD_IMPL( 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) \
74 : __attribute__ ((format (printf, 3, 4))) \
75 : static inline int \
76 : fd_pod_replacef_##type( uchar * FD_RESTRICT pod, \
77 : type val, \
78 144 : char const * FD_RESTRICT fmt, ... ) { \
79 144 : va_list ap; \
80 144 : va_start( ap, fmt ); \
81 144 : char buf[ 128UL ]; \
82 144 : int ret = vsnprintf( buf, 128UL, fmt, ap ); \
83 144 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, 128UL-1UL ) ); \
84 144 : buf[ len ] = '\0'; \
85 144 : va_end( ap ); \
86 144 : if( FD_UNLIKELY( ret<0 || (ulong)ret>=128UL ) ) return 0UL; \
87 144 : int result = fd_pod_remove( pod, buf ); \
88 144 : if( FD_UNLIKELY( result!=FD_POD_SUCCESS && result!=FD_POD_ERR_RESOLVE ) ) \
89 144 : return result; \
90 144 : if( FD_UNLIKELY( !fd_pod_insert_##type( pod, buf, val ) ) ) \
91 144 : return FD_POD_ERR_FULL; \
92 144 : return FD_POD_SUCCESS; \
93 144 : }
94 :
95 : FD_POD_IMPL( ushort )
96 : FD_POD_IMPL( uint )
97 : FD_POD_IMPL( ulong )
98 : FD_POD_IMPL( short )
99 : FD_POD_IMPL( int )
100 : FD_POD_IMPL( long )
101 : FD_POD_IMPL( char )
102 : FD_POD_IMPL( schar )
103 : FD_POD_IMPL( uchar )
104 : FD_POD_IMPL( float )
105 : #if FD_HAS_DOUBLE
106 : FD_POD_IMPL( 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) \
116 : __attribute__ ((format (printf, 3, 4))) \
117 : static inline type \
118 : fd_pod_queryf_##type( uchar const * FD_RESTRICT pod, \
119 : type def, \
120 1218 : char const * FD_RESTRICT fmt, ... ) { \
121 1218 : va_list ap; \
122 1218 : va_start( ap, fmt ); \
123 1218 : char buf[ 128UL ]; \
124 1218 : int ret = vsnprintf( buf, 128UL, fmt, ap ); \
125 1218 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, 128UL-1UL ) ); \
126 1218 : buf[ len ] = '\0'; \
127 1218 : va_end( ap ); \
128 1218 : if( FD_UNLIKELY( ret<0 || (ulong)ret>=128UL ) ) return 0UL; \
129 1218 : return fd_pod_query_##type( pod, buf, def ); \
130 1218 : }
131 :
132 : FD_POD_IMPL( ushort )
133 : FD_POD_IMPL( uint )
134 : FD_POD_IMPL( ulong )
135 : FD_POD_IMPL( short )
136 : FD_POD_IMPL( int )
137 : FD_POD_IMPL( long )
138 : FD_POD_IMPL( char )
139 : FD_POD_IMPL( schar )
140 : FD_POD_IMPL( uchar )
141 : FD_POD_IMPL( float )
142 : #if FD_HAS_DOUBLE
143 : FD_POD_IMPL( double )
144 : #endif
145 :
146 : __attribute__ ((format (printf, 2, 3)))
147 : static inline uchar const *
148 : fd_pod_queryf_subpod( uchar const * FD_RESTRICT pod,
149 0 : char const * FD_RESTRICT fmt, ... ) {
150 0 : va_list ap;
151 0 : va_start( ap, fmt );
152 0 : char buf[ 128UL ];
153 0 : int ret = vsnprintf( buf, 128UL, fmt, ap );
154 0 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, 128UL-1UL ) );
155 0 : buf[ len ] = '\0';
156 0 : va_end( ap );
157 0 : if( FD_UNLIKELY( ret<0 || (ulong)ret>=128UL ) ) return 0UL;
158 0 : return fd_pod_query_subpod( pod, buf );
159 0 : }
160 :
161 : #undef FD_POD_IMPL
162 :
163 : __attribute__ ((format (printf, 3, 4)))
164 : static inline char const *
165 : fd_pod_queryf_cstr( uchar const * FD_RESTRICT pod,
166 : char const * FD_RESTRICT def,
167 0 : char const * FD_RESTRICT fmt, ... ) {
168 0 : va_list ap;
169 0 : va_start( ap, fmt );
170 0 : char buf[ 128UL ];
171 0 : int ret = vsnprintf( buf, 128UL, fmt, ap );
172 0 : ulong len = fd_ulong_if( ret<0, 0UL, fd_ulong_min( (ulong)ret, 128UL-1UL ) );
173 0 : buf[ len ] = '\0';
174 0 : va_end( ap );
175 0 : if( FD_UNLIKELY( ret<0 || (ulong)ret>=128UL ) ) return 0UL;
176 0 : return fd_pod_query_cstr( pod, buf, def );
177 0 : }
178 :
179 : #endif /* HEADER_fd_src_disco_topo_fd_pod_format_h */
|