From 7753352e6210ae69c2a79bdf2387eaca46becc64 Mon Sep 17 00:00:00 2001 From: Jeremie Guichard Date: Mon, 9 Apr 2018 10:09:34 +0200 Subject: Change taglist_get_tagstring to support 'unlimited' tag list size Previous taglist_get_tagstring signature/implementation did not allow handling of cases where inputted buffer could not contain all tags. New implementation allocates buffer based on pre-computed size allowing to insert all tags in the returned string. Added get_taglist_string in qthelper to handle conversion to QString Added TestTagList with tests for taglist_get_tagstring Signed-off-by: Jeremie Guichard --- core/dive.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'core/dive.c') diff --git a/core/dive.c b/core/dive.c index 1a26ac5d7..811c0270c 100644 --- a/core/dive.c +++ b/core/dive.c @@ -12,6 +12,7 @@ #include "divelist.h" #include "qthelper.h" #include "metadata.h" +#include "membuffer.h" /* one could argue about the best place to have this variable - * it's used in the UI, but it seems to make the most sense to have it @@ -3020,30 +3021,28 @@ void taglist_cleanup(struct tag_entry **tag_list) } } -int taglist_get_tagstring(struct tag_entry *tag_list, char *buffer, int len) +char *taglist_get_tagstring(struct tag_entry *tag_list) { - int i = 0; - struct tag_entry *tmp; - tmp = tag_list; - memset(buffer, 0, len); + bool first_tag = true; + struct membuffer b = { 0 }; + struct tag_entry *tmp = tag_list; while (tmp != NULL) { - int newlength = strlen(tmp->tag->name); - if (i > 0) - newlength += 2; - if ((i + newlength) < len) { - if (i > 0) { - strcpy(buffer + i, ", "); - strcpy(buffer + i + 2, tmp->tag->name); + if (!empty_string(tmp->tag->name)) { + if (first_tag) { + put_format(&b, "%s", tmp->tag->name); + first_tag = false; } else { - strcpy(buffer, tmp->tag->name); + put_format(&b, ", %s", tmp->tag->name); } - } else { - return i; } - i += newlength; tmp = tmp->next; } - return i; + /* Ensures we do return null terminated empty string for: + * - empty tag list + * - tag list with empty tag only + */ + mb_cstring(&b); + return detach_buffer(&b); } static inline void taglist_free_divetag(struct divetag *tag) -- cgit v1.2.3-70-g09d2