LCOV - code coverage report
Current view: top level - app/platform - fd_config_extract.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 42 95 44.2 %
Date: 2025-12-06 04:45:29 Functions: 5 12 41.7 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_app_platform_fd_config_extract_h
       2             : #define HEADER_fd_src_app_platform_fd_config_extract_h
       3             : 
       4             : #include "../../util/fd_util.h"
       5             : #include "../../util/pod/fd_pod.h"
       6             : 
       7             : FD_PROTOTYPES_BEGIN
       8             : 
       9             : /* fdctl_pod_find_leftover recursively descends a pod and logs a
      10             :    WARNING for each remaining leaf item.  Intended for warning the user
      11             :    about unrecognized config options, after loading config files into a
      12             :    pod. */
      13             : 
      14             : int
      15             : fdctl_pod_find_leftover( uchar * pod );
      16             : 
      17             : /* Pod query utils ****************************************************/
      18             : 
      19             : static inline int
      20             : fdctl_cfg_get_cstr_( char *                out,
      21             :                      ulong                 out_sz,
      22             :                      fd_pod_info_t const * info,
      23         162 :                      char const *          path ) {
      24         162 :   if( FD_UNLIKELY( info->val_type != FD_POD_VAL_TYPE_CSTR ) ) {
      25           0 :     FD_LOG_WARNING(( "invalid value for `%s`", path ));
      26           0 :     return 0;
      27           0 :   }
      28         162 :   char const * str = info->val;
      29         162 :   ulong        sz  = strlen( str ) + 1;
      30         162 :   if( FD_UNLIKELY( sz > out_sz ) ) {
      31           0 :     FD_LOG_WARNING(( "`%s`: too long (max %ld)", path, (long)out_sz-1L ));
      32           0 :     return 0;
      33           0 :   }
      34         162 :   fd_memcpy( out, str, sz );
      35         162 :   return 1;
      36         162 : }
      37             : 
      38             : #define fdctl_cfg_get_cstr( out, out_sz, info, path ) \
      39           6 :   fdctl_cfg_get_cstr_( *out, out_sz, info, path )
      40             : 
      41             : static inline int
      42             : fdctl_cfg_get_ulong( ulong *               out,
      43             :                      ulong                 out_sz FD_PARAM_UNUSED,
      44             :                      fd_pod_info_t const * info,
      45         180 :                      char const *          path ) {
      46             : 
      47         180 :   ulong num;
      48         180 :   switch( info->val_type ) {
      49         180 :   case FD_POD_VAL_TYPE_LONG:
      50         180 :     fd_ulong_svw_dec( (uchar const *)info->val, &num );
      51         180 :     long snum = fd_long_zz_dec( num );
      52         180 :     if( snum < 0L ) {
      53           0 :       FD_LOG_WARNING(( "`%s` cannot be negative", path ));
      54           0 :       return 0;
      55           0 :     }
      56         180 :     num = (ulong)snum;
      57         180 :     break;
      58           0 :   case FD_POD_VAL_TYPE_ULONG:
      59           0 :     fd_ulong_svw_dec( (uchar const *)info->val, &num );
      60           0 :     break;
      61           0 :   default:
      62           0 :     FD_LOG_WARNING(( "invalid value for `%s`", path ));
      63           0 :     return 0;
      64         180 :   }
      65             : 
      66         180 :   *out = num;
      67         180 :   return 1;
      68         180 : }
      69             : 
      70             : static inline int
      71             : fdctl_cfg_get_uint( uint *                out,
      72             :                     ulong                 out_sz FD_PARAM_UNUSED,
      73             :                     fd_pod_info_t const * info,
      74         108 :                     char const *          path ) {
      75         108 :   ulong num;
      76         108 :   if( FD_UNLIKELY( !fdctl_cfg_get_ulong( &num, sizeof(num), info, path ) ) ) return 0;
      77         108 :   if( num > UINT_MAX ) {
      78           0 :     FD_LOG_WARNING(( "`%s` is out of bounds (%lx)", path, num ));
      79           0 :     return 0;
      80           0 :   }
      81         108 :   *out = (uint)num;
      82         108 :   return 1;
      83         108 : }
      84             : 
      85             : static inline int
      86             : fdctl_cfg_get_ushort( ushort *              out,
      87             :                       ulong                 out_sz FD_PARAM_UNUSED,
      88             :                       fd_pod_info_t const * info,
      89          24 :                       char const *          path ) {
      90          24 :   ulong num;
      91          24 :   if( FD_UNLIKELY( !fdctl_cfg_get_ulong( &num, sizeof(num), info, path ) ) ) return 0;
      92          24 :   if( num > USHORT_MAX ) {
      93           0 :     FD_LOG_WARNING(( "`%s` is out of bounds (%lx)", path, num ));
      94           0 :     return 0;
      95           0 :   }
      96          24 :   *out = (ushort)num;
      97          24 :   return 1;
      98          24 : }
      99             : 
     100             : static inline int
     101             : fdctl_cfg_get_bool( int *                 out,
     102             :                     ulong                 out_sz FD_PARAM_UNUSED,
     103             :                     fd_pod_info_t const * info,
     104         111 :                     char const *          path ) {
     105         111 :   if( FD_UNLIKELY( info->val_type != FD_POD_VAL_TYPE_INT ) ) {
     106           0 :     FD_LOG_WARNING(( "invalid value for `%s`", path ));
     107           0 :     return 0;
     108           0 :   }
     109         111 :   ulong u; fd_ulong_svw_dec( (uchar const *)info->val, &u );
     110         111 :   *out = fd_int_zz_dec( (uint)u );
     111         111 :   return 1;
     112         111 : }
     113             : 
     114             : static inline int
     115             : fdctl_cfg_get_float( float *               out,
     116             :                      ulong                 out_sz FD_PARAM_UNUSED,
     117             :                      fd_pod_info_t const * info,
     118           0 :                      char const *          path ) {
     119           0 : 
     120           0 :   ulong unum;
     121           0 :   float num;
     122           0 :   switch( info->val_type ) {
     123           0 :   case FD_POD_VAL_TYPE_LONG:
     124           0 :     fd_ulong_svw_dec( (uchar const *)info->val, &unum );
     125           0 :     long snum = fd_long_zz_dec( unum );
     126           0 :     if( snum < 0L ) {
     127           0 :       FD_LOG_WARNING(( "`%s` cannot be negative", path ));
     128           0 :       return 0;
     129           0 :     }
     130           0 :     num = (float)snum;
     131           0 :     break;
     132           0 :   case FD_POD_VAL_TYPE_ULONG:
     133           0 :     fd_ulong_svw_dec( (uchar const *)info->val, &unum );
     134           0 :     num = (float)unum;
     135           0 :     break;
     136           0 :   case FD_POD_VAL_TYPE_FLOAT:
     137           0 :     num = FD_LOAD( float, info->val );
     138           0 :     break;
     139           0 :   default:
     140           0 :     FD_LOG_WARNING(( "invalid value for `%s`", path ));
     141           0 :     return 0;
     142           0 :   }
     143           0 : 
     144           0 :   *out = num;
     145           0 :   return 1;
     146           0 : }
     147             : 
     148             : FD_PROTOTYPES_END
     149             : 
     150             : #endif /* HEADER_fd_src_app_platform_fd_config_extract_h */

Generated by: LCOV version 1.14