diff options
author | Jeremie Guichard <djebrest@gmail.com> | 2018-04-09 10:09:34 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-04-09 07:59:51 -0700 |
commit | 7753352e6210ae69c2a79bdf2387eaca46becc64 (patch) | |
tree | 88c2594af6ff7c87b7923c7b3625653f3b5d7cfe /core | |
parent | f1830cd44e75ac552e09dfd79f6ec9e0d90f8808 (diff) | |
download | subsurface-7753352e6210ae69c2a79bdf2387eaca46becc64.tar.gz |
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 <djebrest@gmail.com>
Diffstat (limited to 'core')
-rw-r--r-- | core/dive.c | 33 | ||||
-rw-r--r-- | core/dive.h | 9 | ||||
-rw-r--r-- | core/qthelper.cpp | 8 | ||||
-rw-r--r-- | core/qthelper.h | 1 | ||||
-rw-r--r-- | core/subsurface-qt/DiveObjectHelper.cpp | 4 |
5 files changed, 32 insertions, 23 deletions
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) diff --git a/core/dive.h b/core/dive.h index 62fe31917..ca0325192 100644 --- a/core/dive.h +++ b/core/dive.h @@ -264,10 +264,13 @@ struct tag_entry *taglist_added(struct tag_entry *original_list, struct tag_entr void dump_taglist(const char *intro, struct tag_entry *tl); /* - * Writes all divetags in tag_list to buffer, limited by the buffer's (len)gth. - * Returns the characters written + * Writes all divetags form tag_list into internally allocated buffer + * Function returns pointer to allocated buffer + * Buffer contains comma separated list of tags names or null terminated string + * + * NOTE! The returned buffer must be freed once used. */ -int taglist_get_tagstring(struct tag_entry *tag_list, char *buffer, int len); +char *taglist_get_tagstring(struct tag_entry *tag_list); /* cleans up a list: removes empty tags and duplicates */ void taglist_cleanup(struct tag_entry **tag_list); diff --git a/core/qthelper.cpp b/core/qthelper.cpp index 1cef38184..d63ee3568 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -1282,6 +1282,14 @@ QString get_divepoint_gas_string(struct dive *d, const divedatapoint &p) return get_gas_string(d->cylinder[idx].gasmix); } +QString get_taglist_string(struct tag_entry *tag_list) +{ + char *buffer = taglist_get_tagstring(tag_list); + QString ret = QString::fromUtf8(buffer); + free(buffer); + return ret; +} + weight_t string_to_weight(const char *str) { const char *end; diff --git a/core/qthelper.h b/core/qthelper.h index 978493e1c..c699c5653 100644 --- a/core/qthelper.h +++ b/core/qthelper.h @@ -30,6 +30,7 @@ bool gpsHasChanged(struct dive *dive, struct dive *master, const QString &gps_te QList<int> getDivesInTrip(dive_trip_t *trip); QString get_gas_string(struct gasmix gas); QString get_divepoint_gas_string(struct dive *d, const divedatapoint& dp); +QString get_taglist_string(struct tag_entry *tag_list); void read_hashes(); void write_hashes(); void updateHash(struct picture *picture); diff --git a/core/subsurface-qt/DiveObjectHelper.cpp b/core/subsurface-qt/DiveObjectHelper.cpp index 093451ad3..4c1c66e21 100644 --- a/core/subsurface-qt/DiveObjectHelper.cpp +++ b/core/subsurface-qt/DiveObjectHelper.cpp @@ -192,9 +192,7 @@ QString DiveObjectHelper::notes() const QString DiveObjectHelper::tags() const { - static char buffer[256]; - taglist_get_tagstring(m_dive->tag_list, buffer, 256); - return QString(buffer); + return get_taglist_string(m_dive->tag_list); } QString DiveObjectHelper::gas() const |