diff options
| author | Clecio Jung | 2023-03-19 13:40:37 -0300 |
|---|---|---|
| committer | Clecio Jung | 2023-03-19 13:40:37 -0300 |
| commit | 26f25a80f790a014115dd25e9619d059716bdfbd (patch) | |
| tree | 51605e8ef0b09eaeba581c0286460ac52ef40bf2 | |
| parent | 852e58a3e6d82362b66ace7f35056ba6693795c3 (diff) | |
| download | libini-26f25a80f790a014115dd25e9619d059716bdfbd.tar.xz libini-26f25a80f790a014115dd25e9619d059716bdfbd.tar.zst | |
Added column to error reporting
| -rw-r--r-- | examples/ini_file_read.c | 4 | ||||
| -rw-r--r-- | ini_file.c | 24 | ||||
| -rw-r--r-- | ini_file.h | 2 |
3 files changed, 19 insertions, 11 deletions
diff --git a/examples/ini_file_read.c b/examples/ini_file_read.c index 7752f36..4f660ae 100644 --- a/examples/ini_file_read.c +++ b/examples/ini_file_read.c @@ -8,8 +8,8 @@ #include "../ini_file.h" -int error_callback(const char *const filename, const size_t line_number, const char *const line, const enum Ini_File_Errors error) { - fprintf(stderr, "%s:%lu %s:\n%s\n", filename, line_number, ini_file_error_to_string(error), line); +int error_callback(const char *const filename, const size_t line_number, const size_t column, const char *const line, const enum Ini_File_Errors error) { + fprintf(stderr, "%s:%lu:%lu %s:\n%s\n", filename, line_number, column, ini_file_error_to_string(error), line); return 0; } @@ -222,13 +222,13 @@ static char *copy_sized_string(const char *const sized_str, const size_t len) { } #endif -static int ini_file_parse_handle_error(Ini_File_Error_Callback callback, const char *const filename, const size_t line_number, const char *const line, const enum Ini_File_Errors error) { +static int ini_file_parse_handle_error(Ini_File_Error_Callback callback, const char *const filename, const size_t line_number, const size_t column, const char *const line, const enum Ini_File_Errors error) { /* This function is called when we found an error in the parsing. * So, we report it to the user using the callback provided. * If the callback returns an integer different from zero, * we end the parsing and return NULL. */ if (callback != NULL) { - return callback(filename, line_number, line, error); + return callback(filename, line_number, column, line, error); } return 0; } @@ -244,12 +244,14 @@ struct Ini_File *ini_file_parse(const char *const filename, Ini_File_Error_Callb FILE *file; struct Ini_File *ini_file = ini_file_new(); if (ini_file == NULL) { - ini_file_parse_handle_error(callback, filename, 0, NULL, ini_allocation); + /* This is a critical error, so we don't proceed, even if the callback returns 0 */ + ini_file_parse_handle_error(callback, filename, 0, 0, NULL, ini_allocation); return NULL; } file = fopen(filename, "rb"); if (file == NULL) { - ini_file_parse_handle_error(callback, filename, 0, NULL, ini_couldnt_open_file); + /* This is a critical error, so we don't proceed, even if the callback returns 0 */ + ini_file_parse_handle_error(callback, filename, 0, 0, NULL, ini_couldnt_open_file); ini_file_free(ini_file); return NULL; } @@ -271,7 +273,7 @@ struct Ini_File *ini_file_parse(const char *const filename, Ini_File_Error_Callb name = cursor; advance_string_until(&cursor, "]#;\r\n"); if (*cursor != ']') { - if (ini_file_parse_handle_error(callback, filename, line_number, line, ini_expected_clocing_bracket) != 0) { + if (ini_file_parse_handle_error(callback, filename, line_number, (size_t)(cursor-line+1), line, ini_expected_clocing_bracket) != 0) { goto ini_file_parse_error; } continue; @@ -283,7 +285,7 @@ struct Ini_File *ini_file_parse(const char *const filename, Ini_File_Error_Callb } error = ini_file_add_section_sized(ini_file, name, name_len); if (error != ini_no_error) { - if (ini_file_parse_handle_error(callback, filename, line_number, line, error) != 0) { + if (ini_file_parse_handle_error(callback, filename, line_number, (size_t)(cursor-line+1), line, error) != 0) { goto ini_file_parse_error; } } @@ -294,9 +296,15 @@ struct Ini_File *ini_file_parse(const char *const filename, Ini_File_Error_Callb advance_string_until(&cursor, "=#; \t\r\n"); /* Compute length of the string name */ key_len = (size_t)(cursor - key); + if (key_len == 0) { + if (ini_file_parse_handle_error(callback, filename, line_number, (size_t)(cursor-line+1), line, ini_key_not_provided) != 0) { + goto ini_file_parse_error; + } + continue; + } advance_white_spaces(&cursor); if (*cursor != '=') { - if (ini_file_parse_handle_error(callback, filename, line_number, line, ini_expected_equals) != 0) { + if (ini_file_parse_handle_error(callback, filename, line_number, (size_t)(cursor-line+1), line, ini_expected_equals) != 0) { goto ini_file_parse_error; } continue; @@ -312,7 +320,7 @@ struct Ini_File *ini_file_parse(const char *const filename, Ini_File_Error_Callb } error = ini_file_add_property_sized(ini_file, key, key_len, value, value_len); if (error != ini_no_error) { - if (ini_file_parse_handle_error(callback, filename, line_number, line, error) != 0) { + if (ini_file_parse_handle_error(callback, filename, line_number, (size_t)(cursor-line+1), line, error) != 0) { goto ini_file_parse_error; } } @@ -114,7 +114,7 @@ enum Ini_File_Errors { /* Callback used to handle errors and warnings in the parsing of INI files (function ini_file_parse). * In case of an error, this callback is called, and if it returns an integer different from zero, * we end the parsing and return NULL. */ -typedef int (*Ini_File_Error_Callback)(const char *const filename, const size_t line_number, const char *const line, const enum Ini_File_Errors error); +typedef int (*Ini_File_Error_Callback)(const char *const filename, const size_t line_number, const size_t column, const char *const line, const enum Ini_File_Errors error); size_t get_file_size(FILE *const file); /* Remember to free the memory allocated for the returned string */ |
