mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-28 05:00:20 +00:00
Get rid of crazy empty tag_list element at the start
So this is totally unrelated to the git repository format, except for the fact that I noticed it while writing the git saving code. The subsurface divetag list handling is being stupid, and has a initial dummy entry at the head of the list for no good reason. I say "no good reason", because there *is* a reason for it: it allows code to avoid the special case of empty list and adding entries to before the first entry etc etc. But that reason is a really *bad* reason, because it's valid only because people don't understand basic list manipulation and pointers to pointers. So get rid of the dummy element, and do things right instead - by passing a *pointer* to the list, instead of the list. And then when traversing the list and looking for a place to insert things, don't go to the next entry - just update the "pointer to pointer" to point to the address of the next entry. Each entry in a C linked list is no different than the list itself, so you can use the pointer to the pointer to the next entry as a pointer to the list. This is a pet peeve of mine. The real beauty of pointers can never be understood unless you understand the indirection they allow. People who grew up with Pascal and were corrupted by that mindset are mentally stunted. Niklaus Wirth has a lot to answer for! But never fear. You too can overcome that mental limitation, it just needs some brain exercise. Reading this patch may help. In particular, contemplate the new "taglist_add_divetag()". Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
e8ee701d35
commit
eb47b2a8d8
8 changed files with 61 additions and 108 deletions
107
dive.c
107
dive.c
|
@ -206,7 +206,6 @@ struct dive *alloc_dive(void)
|
||||||
if (!dive)
|
if (!dive)
|
||||||
exit(1);
|
exit(1);
|
||||||
memset(dive, 0, sizeof(*dive));
|
memset(dive, 0, sizeof(*dive));
|
||||||
taglist_init(&(dive->tag_list));
|
|
||||||
return dive;
|
return dive;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1954,7 +1953,7 @@ int taglist_get_tagstring(struct tag_entry *tag_list, char *buffer, int len)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
struct tag_entry *tmp;
|
struct tag_entry *tmp;
|
||||||
tmp = tag_list->next;
|
tmp = tag_list;
|
||||||
memset(buffer, 0, len);
|
memset(buffer, 0, len);
|
||||||
while (tmp != NULL) {
|
while (tmp != NULL) {
|
||||||
int newlength = strlen(tmp->tag->name);
|
int newlength = strlen(tmp->tag->name);
|
||||||
|
@ -1976,21 +1975,6 @@ int taglist_get_tagstring(struct tag_entry *tag_list, char *buffer, int len)
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct divetag *taglist_get_tag(struct tag_entry *tag_list, const char *tag)
|
|
||||||
{
|
|
||||||
struct tag_entry *tmp;
|
|
||||||
tmp = tag_list->next;
|
|
||||||
while (tmp != NULL) {
|
|
||||||
if (tmp->tag != NULL) {
|
|
||||||
if (strcmp(tmp->tag->name, tag) == 0)
|
|
||||||
return tmp->tag;
|
|
||||||
else
|
|
||||||
tmp = tmp->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void taglist_free_divetag(struct divetag *tag)
|
static inline void taglist_free_divetag(struct divetag *tag)
|
||||||
{
|
{
|
||||||
if (tag->name != NULL)
|
if (tag->name != NULL)
|
||||||
|
@ -2001,29 +1985,32 @@ static inline void taglist_free_divetag(struct divetag *tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add a tag to the tag_list, keep the list sorted */
|
/* Add a tag to the tag_list, keep the list sorted */
|
||||||
static struct divetag *taglist_add_divetag(struct tag_entry *tag_list, struct divetag *tag)
|
static struct divetag *taglist_add_divetag(struct tag_entry **tag_list, struct divetag *tag)
|
||||||
{
|
{
|
||||||
struct tag_entry *tmp, *last;
|
struct tag_entry *next, *entry;
|
||||||
last = tag_list;
|
|
||||||
tmp = tag_list->next;
|
while ((next = *tag_list) != NULL) {
|
||||||
while (1) {
|
int cmp = strcmp(next->tag->name, tag->name);
|
||||||
if (tmp == NULL || strcmp(tmp->tag->name, tag->name) > 0) {
|
|
||||||
/* Insert in front of it */
|
/* Already have it? */
|
||||||
last->next = malloc(sizeof(struct tag_entry));
|
if (!cmp)
|
||||||
last->next->next = tmp;
|
return next->tag;
|
||||||
last->next->tag = tag;
|
/* Is the entry larger? If so, insert here */
|
||||||
return last->next->tag;
|
if (cmp > 0)
|
||||||
} else if (strcmp(tmp->tag->name, tag->name) == 0) {
|
break;
|
||||||
/* Already in list */
|
/* Continue traversing the list */
|
||||||
return tmp->tag;
|
tag_list = &next->next;
|
||||||
} else {
|
|
||||||
last = tmp;
|
|
||||||
tmp = tmp->next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Insert in front of it */
|
||||||
|
entry = malloc(sizeof(struct tag_entry));
|
||||||
|
entry->next = next;
|
||||||
|
entry->tag = tag;
|
||||||
|
*tag_list = entry;
|
||||||
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct divetag *taglist_add_tag(struct tag_entry *tag_list, const char *tag)
|
struct divetag *taglist_add_tag(struct tag_entry **tag_list, const char *tag)
|
||||||
{
|
{
|
||||||
int i = 0, is_default_tag = 0;
|
int i = 0, is_default_tag = 0;
|
||||||
struct divetag *ret_tag, *new_tag;
|
struct divetag *ret_tag, *new_tag;
|
||||||
|
@ -2049,8 +2036,8 @@ struct divetag *taglist_add_tag(struct tag_entry *tag_list, const char *tag)
|
||||||
memcpy(new_tag->name, tag, strlen(tag) + 1);
|
memcpy(new_tag->name, tag, strlen(tag) + 1);
|
||||||
}
|
}
|
||||||
/* Try to insert new_tag into g_tag_list if we are not operating on it */
|
/* Try to insert new_tag into g_tag_list if we are not operating on it */
|
||||||
if (tag_list != g_tag_list) {
|
if (tag_list != &g_tag_list) {
|
||||||
ret_tag = taglist_add_divetag(g_tag_list, new_tag);
|
ret_tag = taglist_add_divetag(&g_tag_list, new_tag);
|
||||||
/* g_tag_list already contains new_tag, free the duplicate */
|
/* g_tag_list already contains new_tag, free the duplicate */
|
||||||
if (ret_tag != new_tag)
|
if (ret_tag != new_tag)
|
||||||
taglist_free_divetag(new_tag);
|
taglist_free_divetag(new_tag);
|
||||||
|
@ -2063,49 +2050,33 @@ struct divetag *taglist_add_tag(struct tag_entry *tag_list, const char *tag)
|
||||||
return ret_tag;
|
return ret_tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
void taglist_init(struct tag_entry **tag_list)
|
|
||||||
{
|
|
||||||
*tag_list = malloc(sizeof(struct tag_entry));
|
|
||||||
(*tag_list)->next = NULL;
|
|
||||||
(*tag_list)->tag = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Clear everything but the first element */
|
/* Clear everything but the first element */
|
||||||
void taglist_clear(struct tag_entry *tag_list)
|
void taglist_free(struct tag_entry *entry)
|
||||||
{
|
{
|
||||||
struct tag_entry *current_tag_entry, *next;
|
while (entry) {
|
||||||
current_tag_entry = tag_list->next;
|
struct tag_entry *next = entry->next;
|
||||||
while (current_tag_entry != NULL) {
|
free(entry);
|
||||||
next = current_tag_entry->next;
|
entry = next;
|
||||||
free(current_tag_entry);
|
|
||||||
current_tag_entry = next;
|
|
||||||
}
|
}
|
||||||
tag_list->next = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Merge src1 and src2, write to *dst */
|
/* Merge src1 and src2, write to *dst */
|
||||||
static void taglist_merge(struct tag_entry *dst, struct tag_entry *src1, struct tag_entry *src2)
|
static void taglist_merge(struct tag_entry **dst, struct tag_entry *src1, struct tag_entry *src2)
|
||||||
{
|
{
|
||||||
struct tag_entry *current_tag_entry;
|
struct tag_entry *entry;
|
||||||
current_tag_entry = src1->next;
|
|
||||||
while (current_tag_entry != NULL) {
|
for (entry = src1; entry; entry = entry->next)
|
||||||
taglist_add_divetag(dst, current_tag_entry->tag);
|
taglist_add_divetag(dst, entry->tag);
|
||||||
current_tag_entry = current_tag_entry->next;
|
for (entry = src2; entry; entry = entry->next)
|
||||||
}
|
taglist_add_divetag(dst, entry->tag);
|
||||||
current_tag_entry = src2->next;
|
|
||||||
while (current_tag_entry != NULL) {
|
|
||||||
taglist_add_divetag(dst, current_tag_entry->tag);
|
|
||||||
current_tag_entry = current_tag_entry->next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void taglist_init_global()
|
void taglist_init_global()
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
taglist_init(&g_tag_list);
|
|
||||||
|
|
||||||
for (i = 0; i < sizeof(default_tags) / sizeof(char *); i++)
|
for (i = 0; i < sizeof(default_tags) / sizeof(char *); i++)
|
||||||
taglist_add_tag(g_tag_list, default_tags[i]);
|
taglist_add_tag(&g_tag_list, default_tags[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer_downloaded)
|
struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer_downloaded)
|
||||||
|
@ -2144,7 +2115,7 @@ struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer
|
||||||
MERGE_MAX(res, a, b, number);
|
MERGE_MAX(res, a, b, number);
|
||||||
MERGE_NONZERO(res, a, b, cns);
|
MERGE_NONZERO(res, a, b, cns);
|
||||||
MERGE_NONZERO(res, a, b, visibility);
|
MERGE_NONZERO(res, a, b, visibility);
|
||||||
taglist_merge(res->tag_list, a->tag_list, b->tag_list);
|
taglist_merge(&res->tag_list, a->tag_list, b->tag_list);
|
||||||
merge_equipment(res, a, b);
|
merge_equipment(res, a, b);
|
||||||
merge_airtemps(res, a, b);
|
merge_airtemps(res, a, b);
|
||||||
if (dl) {
|
if (dl) {
|
||||||
|
|
6
dive.h
6
dive.h
|
@ -315,7 +315,7 @@ struct tag_entry {
|
||||||
|
|
||||||
extern struct tag_entry *g_tag_list;
|
extern struct tag_entry *g_tag_list;
|
||||||
|
|
||||||
struct divetag *taglist_add_tag(struct tag_entry *tag_list, const char *tag);
|
struct divetag *taglist_add_tag(struct tag_entry **tag_list, const char *tag);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Writes all divetags in tag_list to buffer, limited by the buffer's (len)gth.
|
* Writes all divetags in tag_list to buffer, limited by the buffer's (len)gth.
|
||||||
|
@ -323,10 +323,8 @@ struct divetag *taglist_add_tag(struct tag_entry *tag_list, const char *tag);
|
||||||
*/
|
*/
|
||||||
int taglist_get_tagstring(struct tag_entry *tag_list, char *buffer, int len);
|
int taglist_get_tagstring(struct tag_entry *tag_list, char *buffer, int len);
|
||||||
|
|
||||||
void taglist_init(struct tag_entry **tag_list);
|
|
||||||
void taglist_clear(struct tag_entry *tag_list);
|
|
||||||
void taglist_init_global();
|
void taglist_init_global();
|
||||||
|
void taglist_free(struct tag_entry *tag_list);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Events are currently pretty meaningless. This is
|
* Events are currently pretty meaningless. This is
|
||||||
|
|
|
@ -744,11 +744,8 @@ void delete_single_dive(int idx)
|
||||||
free((void *)dive->buddy);
|
free((void *)dive->buddy);
|
||||||
if (dive->suit)
|
if (dive->suit)
|
||||||
free((void *)dive->suit);
|
free((void *)dive->suit);
|
||||||
if (dive->tag_list) {
|
if (dive->tag_list)
|
||||||
taglist_clear(dive->tag_list);
|
taglist_free(dive->tag_list);
|
||||||
/* Remove head of list */
|
|
||||||
free((void *)dive->tag_list);
|
|
||||||
}
|
|
||||||
free(dive);
|
free(dive);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -166,7 +166,7 @@ static void parse_dive_tags(char *line, struct membuffer *str, void *_dive)
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int taglen = strlen(tag);
|
int taglen = strlen(tag);
|
||||||
if (taglen)
|
if (taglen)
|
||||||
taglist_add_tag(dive->tag_list, tag);
|
taglist_add_tag(&dive->tag_list, tag);
|
||||||
len -= taglen;
|
len -= taglen;
|
||||||
if (!len)
|
if (!len)
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -221,7 +221,7 @@ enum ParseState {
|
||||||
};
|
};
|
||||||
static void divetags(char *buffer, void *_tags)
|
static void divetags(char *buffer, void *_tags)
|
||||||
{
|
{
|
||||||
struct tag_entry *tags = _tags;
|
struct tag_entry **tags = _tags;
|
||||||
int i = 0, start = 0, end = 0;
|
int i = 0, start = 0, end = 0;
|
||||||
enum ParseState state = FINDEND;
|
enum ParseState state = FINDEND;
|
||||||
int len = buffer ? strlen(buffer) : 0;
|
int len = buffer ? strlen(buffer) : 0;
|
||||||
|
@ -1089,7 +1089,7 @@ static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
|
||||||
|
|
||||||
if (MATCH("number", get_index, &dive->number))
|
if (MATCH("number", get_index, &dive->number))
|
||||||
return;
|
return;
|
||||||
if (MATCH("tags", divetags, dive->tag_list))
|
if (MATCH("tags", divetags, &dive->tag_list))
|
||||||
return;
|
return;
|
||||||
if (MATCH("tripflag", get_tripflag, &dive->tripflag))
|
if (MATCH("tripflag", get_tripflag, &dive->tripflag))
|
||||||
return;
|
return;
|
||||||
|
@ -1738,7 +1738,7 @@ extern int dm4_events(void *handle, int columns, char **data, char **column)
|
||||||
extern int dm4_tags(void *handle, int columns, char **data, char **column)
|
extern int dm4_tags(void *handle, int columns, char **data, char **column)
|
||||||
{
|
{
|
||||||
if (data[0])
|
if (data[0])
|
||||||
taglist_add_tag(cur_dive->tag_list, data[0]);
|
taglist_add_tag(&cur_dive->tag_list, data[0]);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -873,9 +873,10 @@ void MainTab::saveTags()
|
||||||
{
|
{
|
||||||
EDIT_SELECTED_DIVES(
|
EDIT_SELECTED_DIVES(
|
||||||
QString tag;
|
QString tag;
|
||||||
taglist_clear(mydive->tag_list);
|
taglist_free(mydive->tag_list);
|
||||||
|
mydive->tag_list = NULL;
|
||||||
foreach(tag, ui.tagWidget->getBlockStringList())
|
foreach(tag, ui.tagWidget->getBlockStringList())
|
||||||
taglist_add_tag(mydive->tag_list, tag.toUtf8().data()););
|
taglist_add_tag(&mydive->tag_list, tag.toUtf8().data()););
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainTab::on_tagWidget_textChanged()
|
void MainTab::on_tagWidget_textChanged()
|
||||||
|
|
|
@ -109,10 +109,6 @@ static void save_tags(struct membuffer *b, struct tag_entry *tags)
|
||||||
{
|
{
|
||||||
const char *sep = " ";
|
const char *sep = " ";
|
||||||
|
|
||||||
if (!tags)
|
|
||||||
return;
|
|
||||||
/* The first entry is a dummy, because people don't understand pointers to pointers */
|
|
||||||
tags = tags->next;
|
|
||||||
if (!tags)
|
if (!tags)
|
||||||
return;
|
return;
|
||||||
put_string(b, "tags");
|
put_string(b, "tags");
|
||||||
|
|
32
save-xml.c
32
save-xml.c
|
@ -332,27 +332,18 @@ static void save_events(struct membuffer *b, struct event *ev)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void save_tags(struct membuffer *b, struct tag_entry *tag_list)
|
static void save_tags(struct membuffer *b, struct tag_entry *entry)
|
||||||
{
|
{
|
||||||
int more = 0;
|
if (entry) {
|
||||||
struct tag_entry *tmp = tag_list->next;
|
const char *sep = " tags='";
|
||||||
|
do {
|
||||||
/* Only write tag attribute if the list contains at least one item */
|
struct divetag *tag = entry->tag;
|
||||||
if (tmp != NULL) {
|
put_string(b, sep);
|
||||||
put_format(b, " tags='");
|
|
||||||
|
|
||||||
while (tmp != NULL) {
|
|
||||||
if (more)
|
|
||||||
put_format(b, ", ");
|
|
||||||
/* If the tag has been translated, write the source to the xml file */
|
/* If the tag has been translated, write the source to the xml file */
|
||||||
if (tmp->tag->source != NULL)
|
quote(b, tag->source ?: tag->name, 0);
|
||||||
quote(b, tmp->tag->source, 0);
|
sep = ", ";
|
||||||
else
|
} while ((entry = entry->next) != NULL);
|
||||||
quote(b, tmp->tag->name, 0);
|
put_string(b, "'");
|
||||||
tmp = tmp->next;
|
|
||||||
more = 1;
|
|
||||||
}
|
|
||||||
put_format(b, "'");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -416,8 +407,7 @@ void save_one_dive(struct membuffer *b, struct dive *dive)
|
||||||
put_format(b, " rating='%d'", dive->rating);
|
put_format(b, " rating='%d'", dive->rating);
|
||||||
if (dive->visibility)
|
if (dive->visibility)
|
||||||
put_format(b, " visibility='%d'", dive->visibility);
|
put_format(b, " visibility='%d'", dive->visibility);
|
||||||
if (dive->tag_list != NULL)
|
save_tags(b, dive->tag_list);
|
||||||
save_tags(b, dive->tag_list);
|
|
||||||
|
|
||||||
show_date(b, dive->when);
|
show_date(b, dive->when);
|
||||||
put_format(b, " duration='%u:%02u min'>\n",
|
put_format(b, " duration='%u:%02u min'>\n",
|
||||||
|
|
Loading…
Reference in a new issue