diff --git a/include/playlist.h b/include/playlist.h index 97fb5ac3..96d9082b 100644 --- a/include/playlist.h +++ b/include/playlist.h @@ -4,6 +4,7 @@ #ifndef OCARINA_PLAYLIST_H #define OCARINA_PLAYLIST_H +#include #include enum playlist_flags { @@ -20,6 +21,8 @@ private: public: Playlist(playlist_flags); ~Playlist(); + void write(File &); + void read(File &); void set_flag(playlist_flags); void unset_flag(playlist_flags); diff --git a/lib/playlist.cpp b/lib/playlist.cpp index 685511a6..da9fdfea 100644 --- a/lib/playlist.cpp +++ b/lib/playlist.cpp @@ -13,6 +13,22 @@ Playlist :: ~Playlist() { } +void Playlist :: write(File &f) +{ + f << flags << " " << tracks.size(); + for (unsigned int i = 0; i < tracks.size(); i++) + f << " " << tracks[i]; +} + +void Playlist :: read(File &f) +{ + unsigned int n; + f >> flags >> n; + tracks.resize(n); + for (unsigned int i = 0; i < n; i++) + f >> tracks[i]; +} + void Playlist :: set_flag(playlist_flags f) { flags |= f; diff --git a/tests/playlist/playlist.cpp b/tests/playlist/playlist.cpp index 101717c4..38b481ee 100644 --- a/tests/playlist/playlist.cpp +++ b/tests/playlist/playlist.cpp @@ -71,11 +71,44 @@ void test_1() test_add_tracks("1a", plist, 10, 49); print("Test 1b: Plist size: %u\n", plist.size()); test_rm_tracks("1c", plist, 10); + print("\n"); +} + +/* Test read / write */ +void test_2() +{ + Playlist plist(PL_ENABLED); + Playlist plist2(PL_ENABLED); + + File f("plist.lst", FILE_TYPE_DATA); + + plist.set_flag(PL_RANDOM); + test_add_tracks("2a", plist, 0, 99); + + f.open(OPEN_WRITE); + plist.write(f); + f.close(); + + f.open(OPEN_READ); + plist2.read(f); + f.close(); + + print("Test 2b: "); + if (plist.get_flags() != plist2.get_flags()) { + print("FAILED: flag mismatch\n"); + return; + } else if (plist.size() != plist2.size()) { + print("FAILED: size mismatch\n"); + return; + } + + print("SUCCESS\n"); } int main(int argc, char **argv) { test_0(); test_1(); + test_2(); return 0; } diff --git a/tests/playlist/playlist.good b/tests/playlist/playlist.good index a2d509d6..3bd31c5f 100644 --- a/tests/playlist/playlist.good +++ b/tests/playlist/playlist.good @@ -7,3 +7,6 @@ Test 0f: SUCCESS Test 1a: SUCCESS Test 1b: Plist size: 40 Test 1c: SUCCESS + +Test 2a: SUCCESS +Test 2b: SUCCESS