ECONF_GETVALUEDEF(3)

✨ ECONF_GETVALUEDEF ✨

okay bestie, gather round, because today we're talking about econf_get<type>ValueDef from the libeconf manual β€” page 3, the libeconf section, third one down πŸ’…


🌸 NAME 🌸

econf_get<type>ValueDef β€” grab the value for a group + key you asked for, and if it's not there, just hand you the default instead. no drama, no tears 🫢


πŸŽ€ SYNOPSIS πŸŽ€

bestie, it's C, so of course we start with the include:

#include <libeconf.h>

and here come the whole squad, in order, no cap:

econf_err econf_getBoolValueDef(econf_file *kf, const char *group,
const char *key, bool *result, bool def);

econf_err econf_getDoubleValueDef(econf_file *kf, const char *group,
const char *key, double *result, double def);

econf_err econf_getFloatValueDef(econf_file *kf, const char *group,
const char *key, float *result, float def);

econf_err econf_getIntValueDef(econf_file *kf, const char *group,
const char *key, int32_t *result, int32_t def);

econf_err econf_getInt64ValueDef(econf_file *kf, const char *group,
const char *key, int64_t *result, int64_t def);

econf_err econf_getStringValueDef(econf_file *kf, const char *group,
const char *key, char **result, const char *def);

econf_err econf_getUInt64ValueDef(econf_file *kf, const char *group,
const char *key, uint64_t *result, uint64_t def);

econf_err econf_getUIntValueDef(econf_file *kf, const char *group,
const char *key, uint32_t *result, uint32_t def);

eight functions. bool, double, float, int32_t, int64_t, char **, uint64_t, uint32_t β€” the whole type buffet πŸ”₯


πŸ’– DESCRIPTION πŸ’–

so the vibe is this: these functions go look up the value for a specific group and key inside the econf_file structure you pass as kf. if that key exists, the value gets parsed and stored in result. if the key is not found? girl, no problem β€” the default value from def goes straight into result. no sad error-handling arc required 😭

about group: it's the section name in the config file, that's literally it. but plot twist β€” if group is NULL, the lookup happens in the upper part of the file, the bit that lives before any group declaration. and key? that's just the name of the entry you're tryna retrieve. simple.

now for econf_getStringValueDef specifically β€” memory gets allocated for the result string, even if the default value is what ends up getting used. so you, the caller, have to free it yourself with free(3). this is not a suggestion, bestie, it's giving "you own this now" πŸ’…

and for econf_getBoolValue(): it parses the common boolean strings β€” think "true", "yes", "1", "false", "no", "0" β€” and it does not care about case. uppercase, lowercase, mixed, whatever, it ate and left no crumbs ✨

real talk though: these functions are essentially wrappers around the standard econf_get*Value functions. the whole point is convenience β€” you get a chill way to deal with missing keys without manually checking return codes for ECONF_NOKEY every single time. it's the low-effort, high-reward friend of the group 🫢


πŸ‘‘ RETURN VALUE πŸ‘‘

  • key found and parsed all cute and successfully? you get ECONF_SUCCESS back. slay ⭐
  • key not found? result gets set to def, and the function returns ECONF_NOKEY. see, not a disaster, just a heads-up.
  • something went wrong β€” like the value can't be parsed, or the arguments are invalid? you get an error code back. and if you need to turn that code into something human-readable, hit up econf_errString(3). πŸ’œ

πŸ’€ ERRORS πŸ’€

here's the full roster of ways things can go sideways. know them, respect them:

  • ECONF_KEY_HAS_NULL_VALUE β€” the given key has a NULL value. yeah. that one.
  • ECONF_NOMEM β€” memory allocation failed. (specifically applicable to econf_getStringValueDef.)
  • ECONF_VALUE_CONVERSION_ERROR β€” the value cannot be converted.
  • ECONF_NOKEY β€” the specified key could not be found within the group.
  • ECONF_ARGUMENT_IS_NULL_VALUE β€” the given argument is NULL.
  • ECONF_PARSE_ERROR β€” parse error. (specifically applicable to econf_getBooleanValueDef.)
  • ECONF_ERROR β€” general error. the catch-all. the "something happened" of it all πŸ™ˆ

πŸ¦‹ SEE ALSO πŸ¦‹

libeconf(3), econf_getValue(3), econf_setValue(3), econf_errString(3).

go read them, iykyk 🌈


stay hydrated, keep your group pointers valid, and remember: free that string πŸ’–

this has been the libeconf manual, page ECONF_GETVALUEDEF(3), libeconf edition, dated 2024-12-14. periodt. πŸŽ€

πŸ“¦ where this page comes from