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 <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2013-12-23 10:37:22 -05:00 committed by Anna Schumaker
parent 83a8b5ae1d
commit b8b215be35
7 changed files with 77 additions and 1 deletions

View File

@ -16,7 +16,7 @@ Deck: (lib/deck.cpp)
- Deck:
list<Playlist> deck;
File << current_track << deck.size() << endl;
File << deck.size() << endl;
File << deck[0] << endl;
File << deck[N] << endl;

View File

@ -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 */

View File

@ -20,6 +20,7 @@ private:
unsigned int cur;
public:
Playlist();
Playlist(playlist_flags);
~Playlist();
void write(File &);

View File

@ -8,6 +8,29 @@
static std::list<Playlist> playlist_deck;
void deck :: read(File &f)
{
unsigned int num;
std::list<Playlist>::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<Playlist>::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;

View File

@ -4,6 +4,11 @@
#include <playlist.h>
#include <stdlib.h>
Playlist :: Playlist()
: flags(0), cur(-1)
{
}
Playlist :: Playlist(playlist_flags f)
: flags(f), cur(-1)
{

View File

@ -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;
}

View File

@ -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 }