2.5. Error handling

The information about the last occurred error is stored within the context in the CFG_CONTEXT structure. The following functions provide a convenient interface for interpreting, querying and printing that information.

#include <cfg+.h>

void cfg_print_error(const CFG_CONTEXT con);

void cfg_fprint_error(const CFG_CONTEXT con, FILE *fh);

char *cfg_get_error_str(const CFG_CONTEXT con);

The function cfg_print_error() prints an error message describing the last error. To print the message to the specified stream, use cfg_fprint_error(), which takes a stream pointer fh as its second argument.

Lastly, cfg_get_error_str(), returns an error message in a dynamically allocated string. Be sure to free the memory taken by the string when you do not need it anymore.

The following Error codes table describes the error constants used by the parsing functions. See Parsing section for reference.

Table 2-3. Error codes

ValueDescription
CFG_OKEverything is OK
CFG_ERROR_NOARGAn argument is missing for the option
CFG_ERROR_NOTALLOWEDARGAn argument is not allowed for the option
CFG_ERROR_BADOPTThe option's argument could not be parsed
CFG_ERROR_BADQUOTEError in quotations
CFG_ERROR_BADNUMBERThe option could not be converted to an appropriate numeric type
CFG_ERROR_OVERFLOWA given numeric value was too big or too small
CFG_ERROR_MULTIMultiple arguments used for an option that takes only one argument
CFG_ERROR_NOMEMNot enough memory
CFG_ERROR_STOP_STR, CFG_ERROR_STOP_STR_FOUND Stop string was found
CFG_ERROR_NOEQUALEqual sign expected on the line, but no one was found
CFG_ERROR_UNKNOWNAn unknown option
CFG_ERROR_FILE_NOT_FOUNDFile not found
CFG_ERROR_SEEK_ERRORSeek error (fseek() failure)
CFG_ERROR_INTERNALInternal error

All of the constants have an alternative, short form, which can be derived by replacing _ERROR_ with _ERR_ in the name of the constant. So for example you can use CFG_ERR_BADOPT as a substitution for CFG_ERROR_BADOPT.