From efec9a315fb0acbcf1d641c7fc0e5bc951dc90fd Mon Sep 17 00:00:00 2001 From: MooZ Date: Sat, 7 Sep 2024 23:24:44 +0200 Subject: [PATCH] Bring back comments (still have to work on loading) --- comment.c | 152 +++++++++++++++++++++++++------------------- comment.h | 135 ++++++++++++++++++--------------------- label.h | 2 +- test/CMakeLists.txt | 11 ++++ test/comment.c | 121 ++++++++++------------------------- 5 files changed, 194 insertions(+), 227 deletions(-) diff --git a/comment.c b/comment.c index 53dd907..49c8bda 100644 --- a/comment.c +++ b/comment.c @@ -41,23 +41,20 @@ #define COMMENT_ARRAY_INC 16 -/** - * Comment repository. - */ -struct comment_repository_impl { - size_t size; /**< Size of comment repository */ - size_t last; /**< Last element in the repository */ - comment_t *comments; /**< Comments */ +/// Comment repository. +struct CommentRepositoryImpl { + size_t size; //< Size of comment repository. + size_t last; //< Last element in the repository. + Comment *comments; //< Comments. }; -/** - * Get comment index by its address. - * \param [in] repository Coment repository. - * \param [in] logical Logical address. - * \param [in] page Memory page. - * \return comment index or -1 if the label was not found. - */ -static int comment_repository_index(comment_repository_t* repository, uint16_t logical, uint8_t page) { +/// Get comment index by its address. +/// \param [in] repository Coment repository. +/// \param [in] logical Logical address. +/// \param [in] page Memory page. +/// \return comment index. +/// \return -1 if the label was not found. +static int comment_repository_index(CommentRepository* repository, uint16_t logical, uint8_t page) { size_t i; for(i=0; ilast; i++) { if( (repository->comments[i].page == page) && @@ -68,10 +65,10 @@ static int comment_repository_index(comment_repository_t* repository, uint16_t l return -1; } -/* Create comment repository. */ -comment_repository_t* comment_repository_create() { - comment_repository_t *repository; - repository = (comment_repository_t*)malloc(sizeof(comment_repository_t)); +// Create comment repository. +CommentRepository* comment_repository_create() { + CommentRepository *repository; + repository = (CommentRepository*)malloc(sizeof(CommentRepository)); if(repository == NULL) { ERROR_MSG("Failed to create comment repository: %s", strerror(errno)); } else { @@ -79,7 +76,7 @@ comment_repository_t* comment_repository_create() { repository->comments = NULL; repository->size = COMMENT_ARRAY_INC; - repository->comments = (comment_t*)malloc(repository->size * sizeof(comment_t)); + repository->comments = (Comment*)malloc(repository->size * sizeof(Comment)); if(repository->comments == NULL) { ERROR_MSG("Failed to create comments: %s", strerror(errno)); comment_repository_destroy(repository); @@ -90,43 +87,48 @@ comment_repository_t* comment_repository_create() { return repository; } -/* Release comment repository resources. */ -void comment_repository_destroy(comment_repository_t* repository) { +// Release comment repository resources. +void comment_repository_destroy(CommentRepository* repository) { + assert(repository != NULL); + repository->size = 0; repository->last = 0; if(repository->comments != NULL) { - for(int i=0; ilast; i++) { - if(repository->comments[i].text) { - free(repository->comments[i].text); - } + for(size_t i=0; ilast; i++) { + free(repository->comments[i].text); } free(repository->comments); repository->comments = NULL; } } -/* Add comment to repository. */ -int comment_repository_add(comment_repository_t* repository, uint16_t logical, uint8_t page, const char *text) { - int ret = 1; +// Add comment to repository. +bool comment_repository_add(CommentRepository* repository, uint16_t logical, uint8_t page, const char *text) { + assert(repository != NULL); + assert(text != NULL); + + bool ret = true; int index = comment_repository_index(repository, logical, page); if(index >= 0) { WARNING_MSG("Duplicate comment for logical address $%04x in page $%02x", logical, page); } else { - /* Expand arrays if necessary */ + // Expand arrays if necessary. if(repository->last >= repository->size) { - comment_t *ptr; - repository->size += COMMENT_ARRAY_INC; - ptr = (comment_t*)realloc(repository->comments, repository->size * sizeof(comment_t)); + Comment *ptr; + size_t new_size = repository->size + COMMENT_ARRAY_INC; + ptr = (Comment*)realloc(repository->comments, new_size * sizeof(Comment)); if(ptr == NULL) { + ERROR_MSG("Failed to expand comment buffer: %s", strerror(errno)); comment_repository_destroy(repository); - ret = 0; + ret = false; } else { repository->comments = ptr; + repository->size = new_size; } } - if(ret != 0) { - /* Push addresses & text */ + if(ret) { + // Push addresses & text. repository->comments[repository->last].logical = logical; repository->comments[repository->last].page = page; repository->comments[repository->last].text = strdup(text); @@ -137,37 +139,47 @@ int comment_repository_add(comment_repository_t* repository, uint16_t logical, u return ret; } -/* Find a comment by its address. */ -int comment_repository_find(comment_repository_t* repository, uint16_t logical, uint8_t page, comment_t *out) { +// Find a comment by its address. +bool comment_repository_find(CommentRepository* repository, uint16_t logical, uint8_t page, Comment *out) { + assert(repository != NULL); + assert(out != NULL); + int index = comment_repository_index(repository, logical, page); - if(index < 0) { - memset(out, 0, sizeof(comment_t)); - return 0; + bool ret = (index >= 0); + if(ret) { + memcpy(out, &repository->comments[index], sizeof(Comment)); + } else { + memset(out, 0, sizeof(Comment)); } - memcpy(out, &repository->comments[index], sizeof(comment_t)); - return 1; + return ret; } -/* Get the number of comments stored in the repository. */ -int comment_repository_size(comment_repository_t* repository) { - return repository ? (int)repository->last : 0; +// Get the number of comments stored in the repository. +int comment_repository_size(CommentRepository* repository) { + assert(repository != NULL); + return (int)repository->last; } -/* Retrieve the comment at the specified index. */ -int comment_repository_get(comment_repository_t* repository, int index, comment_t *out) { - if((repository != NULL) && ((index >= 0) && (index < (int)repository->last))) { - memcpy(out, &repository->comments[index], sizeof(comment_t)); - return 1; +// Retrieve the comment at the specified index. +bool comment_repository_get(CommentRepository* repository, int index, Comment *out) { + assert(repository != NULL); + assert(out != NULL); + + bool ret = false; + if((index >= 0) && (index < (int)repository->last)) { + memcpy(out, &repository->comments[index], sizeof(Comment)); + ret = true; } else { - memset(out, 0, sizeof(comment_t)); - return 0; - } + memset(out, 0, sizeof(Comment)); + } + return ret; } -/* Delete comments. */ -int comment_repository_delete(comment_repository_t* repository, uint16_t first, uint16_t end, uint8_t page) { - size_t i; - for(i=0; ilast; i++) { +// Delete comments. +void comment_repository_delete(CommentRepository* repository, uint16_t first, uint16_t end, uint8_t page) { + assert(repository != NULL); + + for(size_t i=0; ilast; i++) { if( (repository->comments[i].page == page) && (repository->comments[i].logical >= first) && (repository->comments[i].logical < end) ) { @@ -176,27 +188,34 @@ int comment_repository_delete(comment_repository_t* repository, uint16_t first, if(repository->comments[i].text) { free(repository->comments[i].text); } - memcpy(&repository->comments[i], &repository->comments[repository->last], sizeof(comment_t)); + memcpy(&repository->comments[i], &repository->comments[repository->last], sizeof(Comment)); i--; } } } - return 1;} +} + +#if 0 +// [todo] move to comment/load +// Load comments from file. +bool comment_repository_load(CommentRepository* repository, const char* filename) { + assert(filename != NULL); + assert(repository != NULL); + + bool ret = false; -/* Load comments from file. */ -int comment_repository_load(const char* filename, comment_repository_t* repository) { json_t* root; json_error_t err; json_t* value; - int ret = 0, index = 0; + int index = 0; root = json_load_file(filename, 0, &err); if(!root) { ERROR_MSG("Failed to parse %s:%d:%d: %s", filename, err.line, err.column, err.text); } else { if(!json_is_array(root)) { ERROR_MSG("Array expected."); - } else for (index = 0, ret = 1; ret && (index < json_array_size(root)) && (value = json_array_get(root, index)); index++) { - ret = 0; + } else for (index = 0, ret = true; ret && (index < json_array_size(root)) && (value = json_array_get(root, index)); index++) { + ret = false; if(!json_is_object(value)) { ERROR_MSG("Expected object."); } else { @@ -221,7 +240,7 @@ int comment_repository_load(const char* filename, comment_repository_t* reposito } else if((num < 0) || (num > 0xff)) { ERROR_MSG("Page value out of range."); } else if(comment_repository_add(repository, logical, (uint8_t)num, text)) { - ret = 1; + ret = true; } free(text); } @@ -232,3 +251,4 @@ int comment_repository_load(const char* filename, comment_repository_t* reposito } return ret; } +#endif \ No newline at end of file diff --git a/comment.h b/comment.h index 45e15e9..1087f52 100644 --- a/comment.h +++ b/comment.h @@ -38,80 +38,67 @@ #include "config.h" -/** - * Comment. - */ +/// Comment. typedef struct { - uint16_t logical; /**< Logical address */ - uint8_t page; /**< Memory page */ - char* text; /**< Comment text */ -} comment_t; - -typedef struct comment_repository_impl comment_repository_t; - -/** - * Create comment repository. - * \return A pointer to a comment repository or NULL if an error occured. - */ -comment_repository_t* comment_repository_create(); - -/** - * Release comment repository resources. - * \param [in,out] repository Comment repository. - */ -void comment_repository_destroy(comment_repository_t* repository); - -/** - * Add comment to repository. - * \param [in,out] repository Comment repository. - * \param [in] logical Logical address. - * \param [in] page Memory page. - * \param [in] text Comment text. - */ -int comment_repository_add(comment_repository_t* repository, uint16_t logical, uint8_t page, const char *text); - -/** - * Find a comment by its address. - * \param [in] repository Comment repository. - * \param [in] logical Logical address. - * \param [in] page Memory page. - * \param [out] out Associated comment (if any). - * \return 1 if a comment was found, 0 otherwise. - */ -int comment_repository_find(comment_repository_t* repository, uint16_t logical, uint8_t page, comment_t *out); - -/** - * Get the number of comments stored in the repository. - * \param [in] repository Comment repository. - * \return Comment count. - */ -int comment_repository_size(comment_repository_t* repository); - -/** - * Retrieve the comment at the specified index. - * \param [in] repository Comment repository. - * \param [in] index Comment index. - * \param [out] out Comment (if any). - * \return 1 if a comment exists for the specified index, 0 otherwise. - */ -int comment_repository_get(comment_repository_t* repository, int index, comment_t *out); - -/** - * Delete comments. - * \param [in] repository Comment repository. - * \param [in] first Start of the logical address range. - * \param [in] end End of the logical address range. - * \param [in] page Memory page. - */ -int comment_repository_delete(comment_repository_t* repository, uint16_t first, uint16_t end, uint8_t page); - -/** - * Load comments from file. - * \param [in] filename Input filename. - * \param [out] repository Comment repository. - * \return 1 if the comments contained in the file was succesfully added to the repository. - * 0 if an error occured. - */ -int comment_repository_load(const char* filename, comment_repository_t* repository); + uint16_t logical; //< Logical address. + uint8_t page; //< Memory page. + char* text; //< Comment text. +} Comment; + +typedef struct CommentRepositoryImpl CommentRepository; + +/// Create comment repository. +/// \return A pointer to a comment repository. +/// \return NULL if an error occured. +CommentRepository* comment_repository_create(); + +/// Release comment repository resources. +/// \param [in,out] repository Comment repository. +void comment_repository_destroy(CommentRepository* repository); + +/// Add comment to repository. +/// \param [in,out] repository Comment repository. +/// \param [in] logical Logical address. +/// \param [in] page Memory page. +/// \param [in] text Comment text. +/// \return true if the comment was successfully added to the repository. +/// \return false if an error occured. +bool comment_repository_add(CommentRepository* repository, uint16_t logical, uint8_t page, const char *text); + +/// Find a comment by its address. +/// \param [in] repository Comment repository. +/// \param [in] logical Logical address. +/// \param [in] page Memory page. +/// \param [out] out Associated comment (if any). +/// \return true if a comment was found. +/// \return 0 otherwise. +bool comment_repository_find(CommentRepository* repository, uint16_t logical, uint8_t page, Comment *out); + +/// Get the number of comments stored in the repository. +/// \param [in] repository Comment repository. +/// \return Comment count. +int comment_repository_size(CommentRepository* repository); + +/// Retrieve the comment at the specified index. +/// \param [in] repository Comment repository. +/// \param [in] index Comment index. +/// \param [out] out Comment (if any). +/// \return true if a comment exists for the specified index. +/// \return false otherwise. +bool comment_repository_get(CommentRepository* repository, int index, Comment *out); + +/// Delete comments. +/// \param [in] repository Comment repository. +/// \param [in] first Start of the logical address range. +/// \param [in] end End of the logical address range. +/// \param [in] page Memory page. +void comment_repository_delete(CommentRepository* repository, uint16_t first, uint16_t end, uint8_t page); + +/// Load comments from file. +/// \param [in] filename Input filename. +/// \param [out] repository Comment repository. +/// \return true if the comments contained in the file was succesfully added to the repository. +/// \return false if an error occured. +bool comment_repository_load(CommentRepository* repository, const char* filename); #endif // ETRIPATOR_COMMENT_H \ No newline at end of file diff --git a/label.h b/label.h index e785df1..86824cf 100644 --- a/label.h +++ b/label.h @@ -63,7 +63,7 @@ void label_repository_destroy(LabelRepository* repository); /// \param [in] logical Logical address. /// \param [in] page Memory page. /// \param [in] description Description (optional if name is set, mandatory otherwise). -/// \return true if the entry was successfully added in the repository. +/// \return true if the entry was successfully added to the repository. /// \return false if an error occured. bool label_repository_add(LabelRepository* repository, const char* name, uint16_t logical, uint8_t page, const char *description); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f706dda..64e40c6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -100,3 +100,14 @@ add_unit_test( LIBRARIES cwalk INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR} ) + +add_unit_test( + NAME comment + SOURCES + comment.c + ${PROJECT_SOURCE_DIR}/comment.c + ${PROJECT_SOURCE_DIR}/message.c + ${PROJECT_SOURCE_DIR}/message/console.c + LIBRARIES cwalk + INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR} +) diff --git a/test/comment.c b/test/comment.c index 0c39627..550ca80 100644 --- a/test/comment.c +++ b/test/comment.c @@ -37,118 +37,67 @@ #include "comment.h" #include "message.h" #include "message/console.h" -#include "message/file.h" -void* setup(const MunitParameter params[], void* user_data) { - (void) params; - (void) user_data; - - console_msg_printer_t *printer = (console_msg_printer_t*)malloc(sizeof(console_msg_printer_t)); - - msg_printer_init(); - console_msg_printer_init(printer); - msg_printer_add((msg_printer_t*)printer); - - return (void*)printer; +void* setup(const MunitParameter params[] __attribute__((unused)), void* user_data __attribute__((unused))) { + message_printer_init(); + console_message_printer_init(); + return NULL; } -void tear_down(void* fixture) { - msg_printer_destroy(); - free(fixture); +void tear_down(void* fixture __attribute__((unused))) { + message_printer_destroy(); } -MunitResult comment_add_test(const MunitParameter params[], void* fixture) { - (void)params; - (void)fixture; - - int ret; - comment_t comment; - comment_repository_t *repository; - - repository = comment_repository_create(); +MunitResult comment_add_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) { + Comment comment = {0}; + CommentRepository *repository = comment_repository_create(); munit_assert_not_null(repository); - ret = comment_repository_add(repository, 0x0002, 0x00, "comment #0"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_add(repository, 0x0001, 0x00, "comment #1"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_add(repository, 0x0003, 0x00, "comment #3"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_add(repository, 0x0001, 0x01, "comment #2"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_find(repository, 0x0001, 0x01, &comment); - munit_assert_int(ret, !=, 0); + munit_assert_true(comment_repository_add(repository, 0x0002, 0x00, "comment #0")); + munit_assert_true(comment_repository_add(repository, 0x0001, 0x00, "comment #1")); + munit_assert_true(comment_repository_add(repository, 0x0003, 0x00, "comment #3")); + munit_assert_true(comment_repository_add(repository, 0x0001, 0x01, "comment #2")); + + munit_assert_true(comment_repository_find(repository, 0x0001, 0x01, &comment)); munit_assert_int(comment.logical, ==, 0x0001); - munit_assert_int(comment.page, ==, 0x01); + munit_assert_int(comment.page, ==, 0x01); munit_assert_string_equal(comment.text, "comment #2"); - ret = comment_repository_find(repository, 0x0001, 0x00, &comment); - munit_assert_int(ret, !=, 0); + munit_assert_true(comment_repository_find(repository, 0x0001, 0x00, &comment)); munit_assert_int(comment.logical, ==, 0x0001); - munit_assert_int(comment.page, ==, 0x00); + munit_assert_int(comment.page, ==, 0x00); munit_assert_string_equal(comment.text, "comment #1"); - ret = comment_repository_find(repository, 0x0003, 0x00, &comment); - munit_assert_int(ret, !=, 0); + munit_assert_true(comment_repository_find(repository, 0x0003, 0x00, &comment)); munit_assert_int(comment.logical, ==, 0x0003); - munit_assert_int(comment.page, ==, 0x00); + munit_assert_int(comment.page, ==, 0x00); munit_assert_string_equal(comment.text, "comment #3"); - ret = comment_repository_size(repository); - munit_assert_int(ret, ==, 4); + munit_assert_int(comment_repository_size(repository), ==, 4); comment_repository_destroy(repository); return MUNIT_OK; } -MunitResult comment_delete_test(const MunitParameter params[], void* fixture) { - (void)params; - (void)fixture; - - int ret; - comment_t comment; - comment_repository_t *repository; - - repository = comment_repository_create(); +MunitResult comment_delete_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) { + Comment comment = {0}; + CommentRepository *repository = comment_repository_create(); munit_assert_not_null(repository); - ret = comment_repository_add(repository, 0x0002, 0x03, "comment #7"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_add(repository, 0x0002, 0x01, "comment #6"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_add(repository, 0x0002, 0x02, "comment #5"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_add(repository, 0x000a, 0x00, "comment #4"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_add(repository, 0x0008, 0x00, "comment #3"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_add(repository, 0x0004, 0x00, "comment #2"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_add(repository, 0x0002, 0x00, "comment #1"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_add(repository, 0x0000, 0x00, "comment #0"); - munit_assert_int(ret, !=, 0); - - ret = comment_repository_size(repository); - munit_assert_int(ret, ==, 8); + munit_assert_true(comment_repository_add(repository, 0x0002, 0x03, "comment #7")); + munit_assert_true(comment_repository_add(repository, 0x0002, 0x01, "comment #6")); + munit_assert_true(comment_repository_add(repository, 0x0002, 0x02, "comment #5")); + munit_assert_true(comment_repository_add(repository, 0x000a, 0x00, "comment #4")); + munit_assert_true(comment_repository_add(repository, 0x0008, 0x00, "comment #3")); + munit_assert_true(comment_repository_add(repository, 0x0004, 0x00, "comment #2")); + munit_assert_true(comment_repository_add(repository, 0x0002, 0x00, "comment #1")); + munit_assert_true(comment_repository_add(repository, 0x0000, 0x00, "comment #0")); + munit_assert_int(comment_repository_size(repository), ==, 8); - ret = comment_repository_delete(repository, 0x0001, 0x0009, 0x00); - munit_assert_int(ret, !=, 0); + comment_repository_delete(repository, 0x0001, 0x0009, 0x00); - ret = comment_repository_size(repository); - munit_assert_int(ret, ==, 5); + munit_assert_int(comment_repository_size(repository), ==, 5); comment_repository_destroy(repository);