Error handling

These functions are used manipulating with error. libcfg+ itself remember 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 error. Similar function cfg_fprint_error() prints error message to specified stream as second parameter fh.

The last two error manipulating functions returns error message string. First one cfg_get_error_str() returns dynamically allocated NUL array of characters filled with error message. Returned pointer must be freed using free() system call. Second function returns pointer to local static buffer with error message. It needs error code passed as parameter errorcode to work.

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

Table 2-4. 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 are provided also in its shorter forms. Use _ERR_ substring in constant name instead _ERROR_ substring. So in example you can use CFG_ERR_BADOPT as a substitution for CFG_ERROR_BADOPT.