summaryrefslogtreecommitdiffstats
path: root/core/picture.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-04-11 17:41:56 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-05-06 13:58:09 -0700
commit989d6a3f96b818e5eacc5a2ccb1cc82e6dd8354c (patch)
tree006daeb578ac4d3e68044ecfc36e7e12b1604ee8 /core/picture.c
parent282041e228d4a60ff7108fbfd1fc23caffd59ba4 (diff)
downloadsubsurface-989d6a3f96b818e5eacc5a2ccb1cc82e6dd8354c.tar.gz
media: use table instead of linked list for media
For consistency with equipment, use our table macros for pictures. Generally tables (arrays) are preferred over linked lists, because they allow random access. This is mostly copy & paste of the equipment code. Sadly, our table macros are quite messy and need some revamping. Therefore, the resulting code is likewise somewhat messy. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/picture.c')
-rw-r--r--core/picture.c70
1 files changed, 60 insertions, 10 deletions
diff --git a/core/picture.c b/core/picture.c
index 3f887efe8..727500afd 100644
--- a/core/picture.c
+++ b/core/picture.c
@@ -1,21 +1,71 @@
// SPDX-License-Identifier: GPL-2.0
#include "picture.h"
+#include "table.h"
+#include "subsurface-string.h"
#include <stdlib.h>
#include <string.h>
-struct picture *alloc_picture()
+static void free_picture(struct picture picture)
{
- struct picture *pic = malloc(sizeof(struct picture));
- if (!pic)
- exit(1);
- memset(pic, 0, sizeof(struct picture));
- return pic;
+ free(picture.filename);
+ picture.filename = NULL;
}
-void free_picture(struct picture *picture)
+static int comp_pictures(struct picture a, struct picture b)
{
- if (picture) {
- free(picture->filename);
- free(picture);
+ if (a.offset.seconds < b.offset.seconds)
+ return -1;
+ if (a.offset.seconds > b.offset.seconds)
+ return 1;
+ return strcmp(a.filename ?: "", b.filename ?: "");
+}
+
+static bool picture_less_than(struct picture a, struct picture b)
+{
+ return comp_pictures(a, b) < 0;
+}
+
+/* picture table functions */
+//static MAKE_GET_IDX(picture_table, struct picture, pictures)
+static MAKE_GROW_TABLE(picture_table, struct picture, pictures)
+static MAKE_GET_INSERTION_INDEX(picture_table, struct picture, pictures, picture_less_than)
+MAKE_ADD_TO(picture_table, struct picture, pictures)
+static MAKE_REMOVE_FROM(picture_table, pictures)
+//MAKE_SORT(picture_table, struct picture, pictures, comp_pictures)
+//MAKE_REMOVE(picture_table, struct picture, picture)
+MAKE_CLEAR_TABLE(picture_table, pictures, picture)
+
+/* Add a clone of a picture to the end of a picture table.
+ * Cloned means that the filename-string is copied. */
+static void add_cloned_picture(struct picture_table *t, struct picture pic)
+{
+ pic.filename = copy_string(pic.filename);
+ int idx = picture_table_get_insertion_index(t, pic);
+ add_to_picture_table(t, idx, pic);
+}
+
+void copy_pictures(const struct picture_table *s, struct picture_table *d)
+{
+ int i;
+ clear_picture_table(d);
+ for (i = 0; i < s->nr; i++)
+ add_cloned_picture(d, s->pictures[i]);
+}
+
+void add_picture(struct picture_table *t, struct picture newpic)
+{
+ int idx = picture_table_get_insertion_index(t, newpic);
+ add_to_picture_table(t, idx, newpic);
+}
+
+// Return true if picture was found and deleted
+bool remove_picture(struct picture_table *t, const char *filename)
+{
+ for (int i = 0; i < t->nr; ++i) {
+ if (same_string(t->pictures[i].filename, filename)) {
+ remove_from_picture_table(t, i);
+ return true;
+ }
}
+ return false;
}