Skip to content

Commit

Permalink
Bring back comments (still have to work on loading)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlockoS committed Sep 7, 2024
1 parent 388b5cc commit efec9a3
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 227 deletions.
152 changes: 86 additions & 66 deletions comment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; i<repository->last; i++) {
if( (repository->comments[i].page == page) &&
Expand All @@ -68,18 +65,18 @@ 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 {
repository->last = 0;
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);
Expand All @@ -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; i<repository->last; i++) {
if(repository->comments[i].text) {
free(repository->comments[i].text);
}
for(size_t i=0; i<repository->last; 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);
Expand All @@ -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; i<repository->last; 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; i<repository->last; i++) {
if( (repository->comments[i].page == page) &&
(repository->comments[i].logical >= first) &&
(repository->comments[i].logical < end) ) {
Expand All @@ -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 {
Expand All @@ -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);
}
Expand All @@ -232,3 +251,4 @@ int comment_repository_load(const char* filename, comment_repository_t* reposito
}
return ret;
}
#endif
135 changes: 61 additions & 74 deletions comment.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit efec9a3

Please sign in to comment.