2.5. Error handling

These functions are used for manipulation with libcfg+ errors. Parsing context (cfg_context) itself remembers last occured error in internal data structures.

#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);

Function cfg_print_error() prints error message of last occured error. Similar function cfg_fprint_error() prints error message to specified stream, which must be passed as second parameter fh.

The last error manipulating function, cfg_get_error_str(), returns error message string. String is dynamically allocated. It is NULL terminated array of characters filled with error message. Returned pointer must be freed using free() system call.

In following Error codes table are defined possible error constants returned by parsing function, which has been described in Parsing chapter.

Table 2-3. Error codes

ValueDescription
CFG_OK OK, all is right
CFG_ERROR_NOARG An argument is missing for an option
CFG_ERROR_NOTALLOWEDARG An argument is not allowed for an option
CFG_ERROR_BADOPT An option's argument could not be parsed
CFG_ERROR_BADQUOTE Error in quotations
CFG_ERROR_BADNUMBER An option could not be converted to appropriate numeric type
CFG_ERROR_OVERFLOW A given number was too big or too small
CFG_ERROR_MULTI Multiple arguments used for single option
CFG_ERROR_NOMEM Not enough memory
CFG_ERROR_STOP_STR, CFG_ERROR_STOP_STR_FOUND Stop string was found
CFG_ERROR_NOEQUAL No equal sign on the line
CFG_ERROR_UNKNOWN An unknown option
CFG_ERROR_FILE_NOT_FOUND File not found error
CFG_ERROR_SEEK_ERROR Seek error (fseek() failure)
CFG_ERROR_INTERNAL Internal error

All constants exist also in their shorter forms. Use _ERR_ substring in constant name instead _ERROR_ substring. So for example you can use CFG_ERR_BADOPT as a substitution for CFG_ERROR_BADOPT.