From b8b215be3503075787ad68a7d84bed9f8799f300 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 23 Dec 2013 10:37:22 -0500 Subject: [PATCH] deck: Save and restore the playlist deck The read and write functions will be called by a higher up layer, so I need to open the file manually in the test. Signed-off-by: Anna Schumaker --- design/deck.txt | 2 +- include/deck.h | 4 ++++ include/playlist.h | 1 + lib/deck.cpp | 28 ++++++++++++++++++++++++++++ lib/playlist.cpp | 5 +++++ tests/deck/deck.cpp | 30 ++++++++++++++++++++++++++++++ tests/deck/deck.good | 8 ++++++++ 7 files changed, 77 insertions(+), 1 deletion(-) diff --git a/design/deck.txt b/design/deck.txt index 69224b6e..eec2bec8 100644 --- a/design/deck.txt +++ b/design/deck.txt @@ -16,7 +16,7 @@ Deck: (lib/deck.cpp) - Deck: list deck; - File << current_track << deck.size() << endl; + File << deck.size() << endl; File << deck[0] << endl; File << deck[N] << endl; diff --git a/include/deck.h b/include/deck.h index 658a159f..0a00ab4a 100644 --- a/include/deck.h +++ b/include/deck.h @@ -9,6 +9,9 @@ namespace deck { + void read(File &); + void write(File &); + Playlist *create(); void remove(unsigned int); Playlist *get(unsigned int); @@ -16,6 +19,7 @@ namespace deck unsigned int next(); #ifdef CONFIG_DEBUG + void reset(); void print_info(); #endif /* CONFIG_DEBUG */ diff --git a/include/playlist.h b/include/playlist.h index 74e45858..99ca519f 100644 --- a/include/playlist.h +++ b/include/playlist.h @@ -20,6 +20,7 @@ private: unsigned int cur; public: + Playlist(); Playlist(playlist_flags); ~Playlist(); void write(File &); diff --git a/lib/deck.cpp b/lib/deck.cpp index 26687634..83a6724d 100644 --- a/lib/deck.cpp +++ b/lib/deck.cpp @@ -8,6 +8,29 @@ static std::list playlist_deck; +void deck :: read(File &f) +{ + unsigned int num; + std::list::iterator it; + + f >> num; + playlist_deck.resize(num); + + for (it = playlist_deck.begin(); it != playlist_deck.end(); it++) + it->read(f); +} + +void deck :: write(File &f) +{ + std::list::iterator it; + + f << playlist_deck.size() << std :: endl; + for (it = playlist_deck.begin(); it != playlist_deck.end(); it++) { + it->write(f); + f << std::endl; + } +} + Playlist *deck :: create() { playlist_deck.push_back(Playlist(PL_ENABLED)); @@ -66,6 +89,11 @@ unsigned int deck :: next() } #ifdef CONFIG_DEBUG +void deck :: reset() +{ + playlist_deck.clear(); +} + void deck :: print_info() { unsigned int i = 0; diff --git a/lib/playlist.cpp b/lib/playlist.cpp index c2b8c341..26f1254b 100644 --- a/lib/playlist.cpp +++ b/lib/playlist.cpp @@ -4,6 +4,11 @@ #include #include +Playlist :: Playlist() + : flags(0), cur(-1) +{ +} + Playlist :: Playlist(playlist_flags f) : flags(f), cur(-1) { diff --git a/tests/deck/deck.cpp b/tests/deck/deck.cpp index d649ae18..382fba27 100644 --- a/tests/deck/deck.cpp +++ b/tests/deck/deck.cpp @@ -88,6 +88,35 @@ void test_4() } } + deck :: print_info(); + print("\n"); +} + +/* Test load / save functions */ +void test_5() +{ + print("Test 5:\n"); + + deck :: get(1)->set_flag(PL_ENABLED); + deck :: get(2)->set_flag(PL_ENABLED); + deck :: get(2)->set_flag(PL_RANDOM); + + File f("playlist.lst", FILE_TYPE_DATA); + + print("Saving playlist deck\n"); + f.open(OPEN_WRITE); + deck :: write(f); + f.close(); + + print("Clearing deck\n"); + deck :: reset(); + deck :: print_info(); + + print("Reading back playlist deck\n"); + f.open(OPEN_READ); + deck :: read(f); + f.close(); + deck :: print_info(); } @@ -98,5 +127,6 @@ int main(int argc, char **argv) test_2(); test_3(); test_4(); + test_5(); return 0; } diff --git a/tests/deck/deck.good b/tests/deck/deck.good index 24974519..69590dca 100644 --- a/tests/deck/deck.good +++ b/tests/deck/deck.good @@ -113,3 +113,11 @@ No playable tracks! deck[0] = Playlist { size = 16, flags = 0 } deck[1] = Playlist { size = 19, flags = 0 } deck[2] = Playlist { size = 15, flags = 0 } + +Test 5: +Saving playlist deck +Clearing deck +Reading back playlist deck +deck[0] = Playlist { size = 16, flags = 0 } +deck[1] = Playlist { size = 19, flags = 1 } +deck[2] = Playlist { size = 15, flags = 3 }