This is an automated email from the ASF dual-hosted git repository. jinterrante pushed a commit to branch runtime2-2202 in repository https://gitbox.apache.org/repos/asf/daffodil.git
commit 519b8f597c96e74945edff9fe21b57c595cec956 Author: John Interrante <[email protected]> AuthorDate: Sat Apr 10 11:31:37 2021 -0400 Refactor into add_diagnostic In errors.[ch], define new function add_diagnostic to add an error to a Diagnostics struct. In parsers.c and unparsers.c, make (un)parse_validate_fixed call add_diagnostic instead of adding error directly to Diagnostics struct. --- .../src/main/resources/c/libruntime/errors.c | 17 +++++++++++++++++ .../src/main/resources/c/libruntime/errors.h | 4 ++++ .../src/main/resources/c/libruntime/parsers.c | 13 ++++--------- .../src/main/resources/c/libruntime/unparsers.c | 13 ++++--------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/daffodil-runtime2/src/main/resources/c/libruntime/errors.c b/daffodil-runtime2/src/main/resources/c/libruntime/errors.c index d624b0a..868b364 100644 --- a/daffodil-runtime2/src/main/resources/c/libruntime/errors.c +++ b/daffodil-runtime2/src/main/resources/c/libruntime/errors.c @@ -135,6 +135,23 @@ need_diagnostics(void) return &validati; } +// add_diagnostic - add a new error to validation diagnostics + +void +add_diagnostic(Diagnostics *validati, const Error *error) +{ + if (validati && error) + { + if (validati->length < + sizeof(validati->array) / sizeof(*validati->array)) + { + Error *err = &validati->array[validati->length++]; + err->code = error->code; + err->s = error->s; + } + } +} + // print_diagnostics - print any validation diagnostics void diff --git a/daffodil-runtime2/src/main/resources/c/libruntime/errors.h b/daffodil-runtime2/src/main/resources/c/libruntime/errors.h index da55fb4..f3153c7 100644 --- a/daffodil-runtime2/src/main/resources/c/libruntime/errors.h +++ b/daffodil-runtime2/src/main/resources/c/libruntime/errors.h @@ -98,6 +98,10 @@ typedef struct UState extern Diagnostics *need_diagnostics(void); +// add_diagnostic - add a new error to validation diagnostics + +extern void add_diagnostic(Diagnostics *validati, const Error *error); + // print_diagnostics - print any validation diagnostics extern void print_diagnostics(const Diagnostics *validati); diff --git a/daffodil-runtime2/src/main/resources/c/libruntime/parsers.c b/daffodil-runtime2/src/main/resources/c/libruntime/parsers.c index c9040da..e10a22d 100644 --- a/daffodil-runtime2/src/main/resources/c/libruntime/parsers.c +++ b/daffodil-runtime2/src/main/resources/c/libruntime/parsers.c @@ -19,7 +19,7 @@ #include <endian.h> // for be32toh, le32toh, be16toh, be64toh, le16toh, le64toh #include <stdbool.h> // for bool, false, true #include <stdio.h> // for fread -#include "errors.h" // for PState, eof_or_error, Error, ERR_PARSE_BOOL, Error::(anonymous), Diagnostics, need_diagnostics, ERR_FIXED_VALUE +#include "errors.h" // for PState, eof_or_error, Error, ERR_PARSE_BOOL, Error::(anonymous), add_diagnostic, need_diagnostics, ERR_FIXED_VALUE, Diagnostics // Macros not defined by <endian.h> which we need for uniformity @@ -160,14 +160,9 @@ parse_validate_fixed(bool same, const char *element, PState *pstate) if (!same) { Diagnostics *validati = need_diagnostics(); - pstate->validati = validati; + const Error error = {ERR_FIXED_VALUE, {element}}; - if (validati->length < - sizeof(validati->array) / sizeof(*validati->array)) - { - Error *error = &validati->array[validati->length++]; - error->code = ERR_FIXED_VALUE; - error->s = element; - } + add_diagnostic(validati, &error); + pstate->validati = validati; } } diff --git a/daffodil-runtime2/src/main/resources/c/libruntime/unparsers.c b/daffodil-runtime2/src/main/resources/c/libruntime/unparsers.c index de65700..22555ab 100644 --- a/daffodil-runtime2/src/main/resources/c/libruntime/unparsers.c +++ b/daffodil-runtime2/src/main/resources/c/libruntime/unparsers.c @@ -19,7 +19,7 @@ #include <endian.h> // for htobe32, htole32, htobe16, htobe64, htole16, htole64 #include <stdbool.h> // for bool #include <stdio.h> // for fwrite -#include "errors.h" // for UState, eof_or_error, Diagnostics, Error, need_diagnostics, ERR_FIXED_VALUE, Error::(anonymous) +#include "errors.h" // for UState, eof_or_error, add_diagnostic, need_diagnostics, ERR_FIXED_VALUE, Diagnostics, Error // Macros not defined by <endian.h> which we need for uniformity @@ -146,14 +146,9 @@ unparse_validate_fixed(bool same, const char *element, UState *ustate) if (!same) { Diagnostics *validati = need_diagnostics(); - ustate->validati = validati; + const Error error = {ERR_FIXED_VALUE, {element}}; - if (validati->length < - sizeof(validati->array) / sizeof(*validati->array)) - { - Error *error = &validati->array[validati->length++]; - error->code = ERR_FIXED_VALUE; - error->s = element; - } + add_diagnostic(validati, &error); + ustate->validati = validati; } }
