]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - afs.c
afs: Replace ->init of afs tables by table operations.
[paraslash.git] / afs.c
diff --git a/afs.c b/afs.c
index 36c679fc85d6a1c3b29c216d187b3c7a56dc501f..b017a7afcc0f01030f70e9f3133b27003f530ee3 100644 (file)
--- a/afs.c
+++ b/afs.c
 #include "sideband.h"
 #include "command.h"
 
-static struct afs_table afs_tables[] = {
-       {.init = aft_init, .name = "audio_files"},
-       {.init = attribute_init, .name = "attributes"},
-       {.init = score_init, .name = "scores"},
-       {.init = moods_init, .name = "moods"},
-       {.init = lyrics_init, .name = "lyrics"},
-       {.init = images_init, .name = "images"},
-       {.init = playlists_init, .name = "playlists"},
+/**
+ * The array of tables of the audio file selector.
+ *
+ * We organize them in an array to be able to loop over all tables.
+ */
+static const struct afs_table {
+       /** The name is no table operation, so define it here. */
+       const char * const name;
+       /** The only way to invoke the ops is via this pointer. */
+       const struct afs_table_operations *ops;
+} afs_tables[] = {
+       {.name = "audio_files", .ops = &aft_ops},
+       {.name = "attributes", .ops = &attr_ops},
+       {.name = "scores", .ops = &score_ops},
+       {.name = "moods", .ops = &moods_ops},
+       {.name = "lyrics", .ops = &lyrics_ops},
+       {.name = "images", .ops = &images_ops},
+       {.name = "playlists", .ops = &playlists_ops},
 };
-
+/** Used to loop over the afs tables. */
 #define NUM_AFS_TABLES ARRAY_SIZE(afs_tables)
 
 struct command_task {
@@ -620,7 +630,7 @@ static void close_afs_tables(void)
        int i;
        PARA_NOTICE_LOG("closing afs tables\n");
        for (i = 0; i < NUM_AFS_TABLES; i++)
-               afs_tables[i].close();
+               afs_tables[i].ops->close();
        free(database_dir);
        database_dir = NULL;
 }
@@ -659,7 +669,7 @@ static int open_afs_tables(void)
        PARA_NOTICE_LOG("opening %zu osl tables in %s\n", NUM_AFS_TABLES,
                database_dir);
        for (i = 0; i < NUM_AFS_TABLES; i++) {
-               ret = afs_tables[i].open(database_dir);
+               ret = afs_tables[i].ops->open(database_dir);
                if (ret >= 0)
                        continue;
                PARA_ERROR_LOG("could not open %s\n", afs_tables[i].name);
@@ -668,7 +678,7 @@ static int open_afs_tables(void)
        if (ret >= 0)
                return ret;
        while (i)
-               afs_tables[--i].close();
+               afs_tables[--i].ops->close();
        return ret;
 }
 
@@ -960,12 +970,10 @@ static int afs_poll(struct pollfd *fds, nfds_t nfds, int timeout)
 __noreturn void afs_init(int socket_fd)
 {
        static struct sched s;
-       int i, ret;
+       int ret;
 
        register_signal_task(&s);
        init_list_head(&afs_client_list);
-       for (i = 0; i < NUM_AFS_TABLES; i++)
-               afs_tables[i].init(&afs_tables[i]);
        ret = open_afs_tables();
        if (ret < 0)
                goto out;
@@ -1008,13 +1016,13 @@ static int com_init_callback(struct afs_callback_arg *aca)
        close_afs_tables();
        get_database_dir();
        for (i = 0; i < NUM_AFS_TABLES; i++) {
-               struct afs_table *t = &afs_tables[i];
+               const struct afs_table *t = afs_tables + i;
 
                if (!(table_mask & (1 << i)))
                        continue;
-               if (!t->create)
+               if (!t->ops->create)
                        continue;
-               ret = t->create(database_dir);
+               ret = t->ops->create(database_dir);
                if (ret < 0) {
                        para_printf(&aca->pbout, "cannot create table %s\n",
                                t->name);
@@ -1046,7 +1054,7 @@ static int com_init(struct command_context *cc, struct lls_parse_result *lpr)
                table_mask = 0;
                for (i = 0; i < num_inputs; i++) {
                        for (j = 0; j < NUM_AFS_TABLES; j++) {
-                               struct afs_table *t = &afs_tables[j];
+                               const struct afs_table *t = afs_tables + j;
 
                                if (strcmp(lls_input(i, lpr), t->name))
                                        continue;
@@ -1120,10 +1128,10 @@ __must_check int afs_event(enum afs_events event, struct para_buffer *pb,
        int i, ret;
 
        for (i = 0; i < NUM_AFS_TABLES; i++) {
-               struct afs_table *t = &afs_tables[i];
-               if (!t->event_handler)
+               const struct afs_table *t = afs_tables + i;
+               if (!t->ops->event_handler)
                        continue;
-               ret = t->event_handler(event, pb, data);
+               ret = t->ops->event_handler(event, pb, data);
                if (ret < 0) {
                        PARA_CRIT_LOG("table %s, event %u: %s\n", t->name,
                                event, para_strerror(-ret));