diff --git a/lib/c/labcomm.c b/lib/c/labcomm.c index e02cf3f9b65d5cd7b583a4f80b55b0b257991f5c..a32d3422279e542cfc3182ea96d7d44cfea09776 100644 --- a/lib/c/labcomm.c +++ b/lib/c/labcomm.c @@ -88,39 +88,28 @@ struct labcomm_decoder_context { void labcomm_register_error_handler_encoder(struct labcomm_encoder *encoder, labcomm_error_handler_callback callback) { encoder->on_error = callback; - encoder->writer->on_error = callback; } void labcomm_register_error_handler_decoder(struct labcomm_decoder *decoder, labcomm_error_handler_callback callback) { decoder->on_error = callback; - decoder->reader->on_error = callback; -} - -/* Error strings. _must_ be the same order as in enum labcomm_error */ -const char *labcomm_error_strings[] = { - "Enum begin guard. DO NO use this as an error.", - "Encoder has no registration for this signature.", - "Encoder is missing do_register", - "Encoder is missing do_encode", - "The labcomm buffer is full and it.", - "Decoder is missing do_register", - "Decoder is missing do_decode_one", - "Decoder: Unknown datatype", - "Decoder: index mismatch", - "Decoder: type not found", - "This function is not yet implemented.", - "User defined error.", - "Could not allocate memory.", - "Enum end guard. DO NO use this as an error." +} + +static const char *labcomm_error_string[] = { +#define LABCOMM_ERROR(name, description) description , +#include "labcomm_error.h" +#undef LABCOMM_ERROR }; +static const int labcomm_error_string_count = (sizeof(labcomm_error_string) / + sizeof(labcomm_error_string[0])); + const char *labcomm_error_get_str(enum labcomm_error error_id) { const char *error_str = NULL; // Check if this is a known error ID. - if (error_id >= LABCOMM_ERROR_ENUM_BEGIN_GUARD && error_id <= LABCOMM_ERROR_ENUM_END_GUARD) { - error_str = labcomm_error_strings[error_id]; + if (0 <= error_id && error_id < labcomm_error_string_count) { + error_str = labcomm_error_string[error_id]; } return error_str; } @@ -380,7 +369,6 @@ struct labcomm_encoder *labcomm_encoder_new( result->writer->error = 0; result->lock.action = lock; result->lock.context = lock_context; - result->writer->on_error = on_error_fprintf; result->on_error = on_error_fprintf; result->writer->action->alloc(result->writer, LABCOMM_VERSION); } @@ -567,7 +555,6 @@ struct labcomm_decoder *labcomm_decoder_new( result->reader->data_size = 0; result->reader->count = 0; result->reader->pos = 0; - result->reader->on_error = on_error_fprintf; result->lock.action = lock; result->lock.context = lock_context; result->on_error = on_error_fprintf; @@ -620,7 +607,6 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d) .pos = 0, .error = 0, .action = labcomm_dynamic_buffer_writer_action, - .on_error = NULL }; struct labcomm_signature signature; struct labcomm_sample_entry *entry = NULL; diff --git a/lib/c/labcomm.h b/lib/c/labcomm.h index e306493fe1b6938fdcd4fcfb3608ef65ce9aef28..933137ebcdc18479b3fc2f80fd6146f335beaeb7 100644 --- a/lib/c/labcomm.h +++ b/lib/c/labcomm.h @@ -3,6 +3,7 @@ #include <stdarg.h> #include <unistd.h> +#include <labcomm_error.h> /* Forward declaration */ struct labcomm_encoder; @@ -24,27 +25,6 @@ struct labcomm_signature { * Error handling. */ -/* Error IDs */ -enum labcomm_error { - LABCOMM_ERROR_ENUM_BEGIN_GUARD, // _must_ be the first enum element. labcomm_error_get_str() depends on this. - LABCOMM_ERROR_ENC_NO_REG_SIGNATURE, - LABCOMM_ERROR_ENC_MISSING_DO_REG, - LABCOMM_ERROR_ENC_MISSING_DO_ENCODE, - LABCOMM_ERROR_ENC_BUF_FULL, - LABCOMM_ERROR_DEC_MISSING_DO_REG, - LABCOMM_ERROR_DEC_MISSING_DO_DECODE_ONE, - LABCOMM_ERROR_DEC_UNKNOWN_DATATYPE, - LABCOMM_ERROR_DEC_INDEX_MISMATCH, - LABCOMM_ERROR_DEC_TYPE_NOT_FOUND, - LABCOMM_ERROR_UNIMPLEMENTED_FUNC, - LABCOMM_ERROR_MEMORY, - LABCOMM_ERROR_USER_DEF, - LABCOMM_ERROR_ENUM_END_GUARD // _must_ be the last enum element. labcomm_error_get_str() depends on this. -}; - -/* Error strings. _must_ be the same order as in enum labcomm_error */ -extern const char *labcomm_error_strings[]; - /* The callback prototype for error handling. * First parameter is the error ID. * The second paramters is the number of va_args that comes after this @@ -79,6 +59,7 @@ void labcomm_decoder_register_new_datatype_handler(struct labcomm_decoder *d, /* * Locking support (optional) */ +struct labcomm_lock; struct labcomm_lock_action { int (*alloc)(void *context); diff --git a/lib/c/labcomm_private.h b/lib/c/labcomm_private.h index 0f08ffbed338cdee5afe9bac5bd288b583fa4b33..b6af148b70b2634676b819c74b15a0768fc35c90 100644 --- a/lib/c/labcomm_private.h +++ b/lib/c/labcomm_private.h @@ -71,7 +71,6 @@ struct labcomm_reader { int count; int pos; int error; - labcomm_error_handler_callback on_error; }; @@ -201,7 +200,6 @@ struct labcomm_writer { int count; int pos; int error; - labcomm_error_handler_callback on_error; }; void labcomm_internal_encoder_register( diff --git a/lib/c/test/test_labcomm_basic_type_encoding.c b/lib/c/test/test_labcomm_basic_type_encoding.c index 9ca863eafcff28456ef944084035ac0b4e0eac08..7a12c3f3d30d65a021d2439a25a47e99a1e00750 100644 --- a/lib/c/test/test_labcomm_basic_type_encoding.c +++ b/lib/c/test/test_labcomm_basic_type_encoding.c @@ -15,7 +15,6 @@ static struct labcomm_writer writer = { .pos = 0, .error = 0, .action = NULL, - .on_error = NULL, }; static struct labcomm_reader reader = { @@ -25,7 +24,6 @@ static struct labcomm_reader reader = { .count = 0, .pos = 0, .action = NULL, - .on_error = NULL, }; typedef uint32_t packed32; diff --git a/lib/c/test/test_labcomm_generated_encoding.c b/lib/c/test/test_labcomm_generated_encoding.c index 1b0dd7fa522242f9be3558a335c4ce6ce9c1eb5f..36bb24bfa5f2757d51f9460ec9504e2a8ae577a0 100644 --- a/lib/c/test/test_labcomm_generated_encoding.c +++ b/lib/c/test/test_labcomm_generated_encoding.c @@ -118,7 +118,6 @@ static struct labcomm_writer buffer_writer = { .pos = 0, .error = 0, .action = &writer_action, - .on_error = NULL, }; void dump_encoder(struct labcomm_encoder *encoder)