From c2178bc265c15ffb224280a2c2d17c90aef1d2d7 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 27 Jul 2016 08:15:15 -0400 Subject: [PATCH] core: Cut back on hardcoded dbe_index uses The dbe_index of a given database item might change in the future, so we can't rely on it everywhere. Let's just use it for saving and loading files, with the expectation that changes will happen sometime after startup. Implements #69: Reduce use of dbe_index in Ocarina code Signed-off-by: Anna Schumaker --- core/audio.c | 2 +- core/queue.c | 2 +- core/tags/track.c | 10 +++++----- include/core/tags/album.h | 6 ++++++ include/core/tags/artist.h | 6 ++++++ include/core/tags/genre.h | 6 ++++++ include/core/tags/library.h | 6 ++++++ include/core/tags/track.h | 6 ++++++ tests/core/audio.c | 14 +++++++------- tests/core/queue.c | 20 ++++++++++---------- 10 files changed, 54 insertions(+), 24 deletions(-) diff --git a/core/audio.c b/core/audio.c index 47efb3c7..4ec6cc3a 100644 --- a/core/audio.c +++ b/core/audio.c @@ -17,7 +17,7 @@ static guint audio_bus = 0; static void __audio_save() { file_open(&audio_file, OPEN_WRITE); - file_writef(&audio_file, "%u\n", audio_track->tr_dbe.dbe_index); + file_writef(&audio_file, "%u\n", track_index(audio_track)); file_close(&audio_file); } diff --git a/core/queue.c b/core/queue.c index 9f0fc55a..28d2a822 100644 --- a/core/queue.c +++ b/core/queue.c @@ -174,7 +174,7 @@ void queue_save_tracks(struct queue *queue, struct file *file) file_writef(file, "%u", queue_size(queue)); queue_for_each(queue, &it) - file_writef(file, " %u", queue_iter_val(&it)->tr_dbe.dbe_index); + file_writef(file, " %u", track_index(queue_iter_val(&it))); } void queue_load_flags(struct queue *queue, struct file *file) diff --git a/core/tags/track.c b/core/tags/track.c index d7a996c1..ce433097 100644 --- a/core/tags/track.c +++ b/core/tags/track.c @@ -17,7 +17,7 @@ static gchar *__track_key(struct library *library, gchar *path) gchar *res; if (library) - res = g_strdup_printf("%u/%s", library->li_dbe.dbe_index, path); + res = g_strdup_printf("%u/%s", library_index(library), path); else res = g_strdup(""); @@ -157,10 +157,10 @@ static void track_write(struct file *file, struct db_entry *dbe) struct track *track = TRACK(dbe); gchar *path = __track_path(track); - file_writef(file, "%u %u %u %u %u ", track->tr_library->li_dbe.dbe_index, - track->tr_artist->ar_dbe.dbe_index, - track->tr_album->al_dbe.dbe_index, - track->tr_genre->ge_dbe.dbe_index, + file_writef(file, "%u %u %u %u %u ", library_index(track->tr_library), + artist_index(track->tr_artist), + album_index(track->tr_album), + genre_index(track->tr_genre), track->tr_track); date_write(file, &track->tr_date); file_writef(file, " %hu %hu %s\n%s\n", track->tr_count, diff --git a/include/core/tags/album.h b/include/core/tags/album.h index 7ac8ae0d..04d09890 100644 --- a/include/core/tags/album.h +++ b/include/core/tags/album.h @@ -51,6 +51,12 @@ int album_compare_year(struct album *, struct album *); /* Called to check if an artist has a token that matches the given string. */ bool album_match_token(struct album *album, const gchar *); +/* Called to find the database index of the album tag. */ +static inline unsigned int album_index(struct album *album) +{ + return album->al_dbe.dbe_index; +} + /* Called to check if album artwork has been downloaded. */ bool album_artwork_exists(struct album *); diff --git a/include/core/tags/artist.h b/include/core/tags/artist.h index ea754482..9858d769 100644 --- a/include/core/tags/artist.h +++ b/include/core/tags/artist.h @@ -52,6 +52,12 @@ int artist_compare(struct artist *, struct artist *); /* Called to check if an artist has a token that matches the given string. */ bool artist_match_token(struct artist *artist, const gchar *); +/* Called to find the database index of the artist tag. */ +static inline unsigned int artist_index(struct artist *artist) +{ + return artist->ar_dbe.dbe_index; +} + #ifdef CONFIG_TESTING const struct db_ops *test_artist_ops(); #endif /* CONFIG_TESTING */ diff --git a/include/core/tags/genre.h b/include/core/tags/genre.h index ca08ac2c..a1d370d6 100644 --- a/include/core/tags/genre.h +++ b/include/core/tags/genre.h @@ -43,6 +43,12 @@ int genre_compare(struct genre *, struct genre *); /* Called to check if a genre has a token that matches the given string. */ bool genre_match_token(struct genre *, const gchar *); +/* Called to find the database index of the genre tag. */ +static inline unsigned int genre_index(struct genre *genre) +{ + return genre->ge_dbe.dbe_index; +} + #ifdef CONFIG_TESTING const struct db_ops *test_genre_ops(); #endif /* CONFIG_TESTING */ diff --git a/include/core/tags/library.h b/include/core/tags/library.h index 273dc3d9..d59897ca 100644 --- a/include/core/tags/library.h +++ b/include/core/tags/library.h @@ -52,6 +52,12 @@ void library_remove(struct library *); /* Called to configure if the library tag is enabled. */ void library_set_enabled(struct library *, bool); +/* Called to find the database index of the library tag. */ +static inline unsigned int library_index(struct library *library) +{ + return library->li_dbe.dbe_index; +} + /* * Called to find the full path of files under the library directory. * This function allocates a new string that MUST be freed with g_free(). diff --git a/include/core/tags/track.h b/include/core/tags/track.h index 0756996f..ff442327 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -102,6 +102,12 @@ int track_compare(struct track *, struct track *, enum compare_t); /* Called to check if a track has a token that matches the given string */ bool track_match_token(struct track *, const gchar *); +/* Called to find the database index of the track tag. */ +static inline unsigned int track_index(struct track *track) +{ + return track->tr_dbe.dbe_index; +} + /* * Called to find the full path of the track tag. * This function returns a new string that MUST be freed with g_free(). diff --git a/tests/core/audio.c b/tests/core/audio.c index 555b7084..eaeae3ab 100644 --- a/tests/core/audio.c +++ b/tests/core/audio.c @@ -135,7 +135,7 @@ static void test_next() for (i = 2; i >= 0; i--) { test_loop_equal(queue_size(temp_q), i + 1, i); if (i > 0) { - test_loop_equal(audio_next()->tr_dbe.dbe_index, i, i); + test_loop_equal(audio_next()->tr_track, track_get(i)->tr_track, i); } else /* Simulate an error. */ test_send_error(); test_loop_equal((void *)queue_at(history_q, 0), @@ -179,22 +179,22 @@ static void test_prev() test_equal(state_count, 2); test_equal(audio_pause(), (bool)true); - test_equal(audio_prev()->tr_dbe.dbe_index, 0); + test_equal(audio_prev()->tr_track, track_get(0)->tr_track); test_equal((void *)queue_at(history_q, 0), (void *)track); test_equal(audio_cur_state(), GST_STATE_PLAYING); - test_equal(audio_cur_track()->tr_dbe.dbe_index, 0); + test_equal(audio_cur_track()->tr_track, track_get(0)->tr_track); test_equal(state_count, 4); - test_equal(audio_prev()->tr_dbe.dbe_index, 1); + test_equal(audio_prev()->tr_track, track_get(1)->tr_track); test_equal((void *)queue_at(history_q, 0), (void *)track); test_equal(audio_cur_state(), GST_STATE_PLAYING); - test_equal(audio_cur_track()->tr_dbe.dbe_index, 1); + test_equal(audio_cur_track()->tr_track, track_get(1)->tr_track); test_equal(state_count, 5); - test_equal(audio_prev()->tr_dbe.dbe_index, 2); + test_equal(audio_prev()->tr_track, track_get(2)->tr_track); test_equal((void *)queue_at(history_q, 0), (void *)track); test_equal(audio_cur_state(), GST_STATE_PLAYING); - test_equal(audio_cur_track()->tr_dbe.dbe_index, 2); + test_equal(audio_cur_track()->tr_track, track_get(2)->tr_track); test_equal(state_count, 6); } diff --git a/tests/core/queue.c b/tests/core/queue.c index 04d879e8..c7a630f2 100644 --- a/tests/core/queue.c +++ b/tests/core/queue.c @@ -404,9 +404,9 @@ static void test_rand_select() static void test_sorting() { - unsigned int ex_count[] = { 6, 7, 8, 9, 10, 11, 12, 0, 1, 2, 3, 4, 5 }; - unsigned int ex_title[] = { 6, 9, 8, 3, 2, 5, 1, 4, 11, 10, 12, 0, 7 }; - unsigned int ex_co_ti[] = { 3, 2, 5, 1, 4, 0, 6, 9, 8, 11, 10, 12, 7 }; + unsigned int ex_count[] = { 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6 }; + unsigned int ex_title[] = { 7, 10, 9, 4, 3, 6, 2, 5, 12, 11, 13, 1, 8 }; + unsigned int ex_co_ti[] = { 4, 3, 6, 2, 5, 1, 7, 10, 9, 12, 11, 13, 8 }; struct track *track; unsigned int i; struct queue q; @@ -414,7 +414,7 @@ static void test_sorting() queue_init(&q, Q_SAVE_SORT | Q_ADD_FRONT, &test_ops, NULL); for (i = 0; i < 13; i++) { track = track_get(i); - track->tr_count = (i < 6) ? 4 : 2; + track->tr_count = (track->tr_track <= 6) ? 4 : 2; queue_add(&q, track); } @@ -429,7 +429,7 @@ static void test_sorting() for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal((void *)track, NULL, i); - test_loop_equal(track->tr_dbe.dbe_index, i, i); + test_loop_equal(track->tr_track, i + 1, i); } test_loop_passed(); test_equal(count_sort, 1); @@ -437,7 +437,7 @@ static void test_sorting() for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal((void *)track, NULL, i); - test_loop_equal(track->tr_dbe.dbe_index, ex_count[i], i); + test_loop_equal(track->tr_track, ex_count[i], i); } test_loop_passed(); test_equal(count_sort, 2); @@ -446,7 +446,7 @@ static void test_sorting() for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal((void *)track, NULL, i); - test_loop_equal(track->tr_dbe.dbe_index, ex_count[i], i); + test_loop_equal(track->tr_track, ex_count[i], i); } test_loop_passed(); test_equal(count_sort, 2); @@ -455,7 +455,7 @@ static void test_sorting() for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal((void *)track, NULL, i); - test_loop_equal(track->tr_dbe.dbe_index, ex_title[i], i); + test_loop_equal(track->tr_track, ex_title[i], i); } test_loop_passed(); test_equal(count_sort, 3); @@ -465,7 +465,7 @@ static void test_sorting() for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal((void *)track, NULL, i); - test_loop_equal(track->tr_dbe.dbe_index, ex_co_ti[i], i); + test_loop_equal(track->tr_track, ex_co_ti[i], i); } test_loop_passed(); test_equal(count_sort, 6); @@ -477,7 +477,7 @@ static void test_sorting() for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal((void *)track, NULL, i); - test_loop_equal(track->tr_dbe.dbe_index, 12 - i, i); + test_loop_equal(track->tr_track, 13 - i, i); } test_loop_passed(); test_equal(count_sort, 6);