tags: Begin creating the tag database

This patch adds classes for Artist, Album, Genre and Library.

Signed-off-by: Anna Schumaker <anna@ocarinaproject.net>
This commit is contained in:
Anna Schumaker 2014-03-22 21:44:51 -04:00 committed by Anna Schumaker
parent d61dbae47f
commit 134348f253
6 changed files with 370 additions and 11 deletions

22
DESIGN
View File

@ -622,13 +622,13 @@ Artist Tag:
Initialize an invalid Artist instance.
Artist(const std::string &artist_name);
Set artist_name and add to the filter.
Set artist_name and find the lowercase form.
const std::string Artist :: primary_key();
Use artist name as primary key.
void Artist :: read(File &f);
Read artist name from file and add to the filter.
Read artist name from file and find the lowercase form.
void Artist :: write(File &f);
Write artist name to file.
@ -660,15 +660,15 @@ Album Tag:
Initialize an invalid Album instance.
Album(const std::string &album_name, unsigned int album_year);
Set name and year from album name and album_year. Add album
name to the filter.
Set name and year from album name and album_year. Find the
lowercase form of the album name.
const std::string Album :: primary_key();
Return the string: "$year.$name"
void Album :: read(File &f);
Read year, and name from file. Then add album name
to the filter.
Read year, and name from file. Then set the lowercase form
of the album name.
void Artist :: write(File &f);
Write album information to file.
@ -700,13 +700,13 @@ Genre Tag:
Initialize an invalid Genre instance.
Genre(const std::string &genre_name);
Set genre from genre name and add to the filter.
Set genre from genre name and find the lowercase form.
const std::string Genre :: primary_key();
Use genre as primary key.
void Genre :: read(File &f);
Read genre from file and add to the filter.
Read genre from file and find the lowercase form.
void Genre :: write(File &f);
Write genre to file.
@ -739,17 +739,19 @@ Library Tag:
- API:
Library();
Initialize an invalid Library instance.
Set count = 0.
Set enabled = false.
Library(const std::string &path);
Set root_path from the provided path.
Set enabled = true.
Set count = 0.
Set enabled = true.
const std::string Library :: primary_key();
Use root_path as the primary key,
void read(File &f);
Read a library path from file, set count = 0.
Read a library path from file.
void write(File &f);
Write a library path to file.

63
include/tags.h Normal file
View File

@ -0,0 +1,63 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#ifndef OCARINA_TAGS_H
#define OCARINA_TAGS_H
#include <database.h>
class Artist : public DatabaseEntry {
public:
std::string name;
std::string lower;
Artist();
Artist(const std::string &);
const std::string primary_key();
void read(File &);
void write(File &);
};
class Album : public DatabaseEntry {
public:
std::string name;
std::string lower;
unsigned int year;
Album();
Album(const std::string &, unsigned int);
const std::string primary_key();
void read(File &);
void write(File &);
};
class Genre : public DatabaseEntry {
public:
std::string name;
std::string lower;
Genre();
Genre(const std::string &);
const std::string primary_key();
void read(File &);
void write(File &);
};
class Library : public DatabaseEntry {
public:
std::string root_path;
unsigned int count;
bool enabled;
Library();
Library(const std::string &);
const std::string primary_key();
void read(File &);
void write(File &);
};
#endif /* OCARINA_TAGS_H */

137
lib/tags.cpp Normal file
View File

@ -0,0 +1,137 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <tags.h>
#include <filter.h>
#include <sstream>
/**
*
* Artist tag
*
*/
Artist :: Artist() {}
Artist :: Artist(const std::string &s)
: name(s), lower(filter :: lowercase(name))
{
}
const std::string Artist :: primary_key()
{
return name;
}
void Artist :: read(File &f)
{
name = f.getline();
lower = filter :: lowercase(name);
}
void Artist :: write(File &f)
{
f << name;
}
/**
*
* Album tag
*
*/
Album :: Album() {}
Album :: Album(const std::string &s, unsigned int y)
: name(s), lower(filter :: lowercase(name)), year(y)
{
}
const std::string Album :: primary_key()
{
std::stringstream ss;
ss << year << "." << name;
return ss.str();
}
void Album :: read(File &f)
{
f >> year;
name = f.getline();
lower = filter :: lowercase(name);
}
void Album :: write(File &f)
{
f << year << " " << name;
}
/**
*
* Genre tag
*
*/
Genre :: Genre() {}
Genre :: Genre(const std::string &s)
: name(s), lower(filter :: lowercase(name))
{
}
const std::string Genre :: primary_key()
{
return name;
}
void Genre :: read(File &f)
{
name = f.getline();
lower = filter :: lowercase(name);
}
void Genre :: write(File &f)
{
f << name;
}
/**
*
* Library tag
*
*/
Library :: Library()
: count(0), enabled(false)
{
}
Library :: Library(const std::string &s)
: root_path(s), count(0), enabled(true)
{
}
const std::string Library :: primary_key()
{
return root_path;
}
void Library :: read(File &f)
{
f >> enabled;
root_path = f.getline();
}
void Library :: write(File &f)
{
f << enabled << " " << root_path;
}

View File

@ -8,7 +8,8 @@ if sys.argv.count("tests") > 0:
src = SConscript("src/Sconscript")
tests = [ "version", "file", "db_entry", "database", "index", "filter", "idle" ]
tests = [ "version", "file", "db_entry", "database", "index",
"filter", "idle", "tag_db" ]
#scripts = [ "playlist", "library", "playqueue", "deck", "audio", "gui" ]
prev = None

138
tests/src/tags.cpp Normal file
View File

@ -0,0 +1,138 @@
/*
* Copyright 2014 (c) Anna Schumaker.
* Test a DatabaseEntry
*/
#include <tags.h>
#include <print.h>
#include <stdlib.h>
#include <unistd.h>
unsigned int test_num = 0;
void test_results(bool success, unsigned int line)
{
print(" %u: ", test_num);
if (success)
print("Success!\n");
else {
print("FAILED (%u) =(\n", line);
exit(1);
}
test_num++;
}
template <class T>
void save_tag(const std::string &file, T &tag)
{
File f(file, FILE_TYPE_DATA);
f.open(OPEN_WRITE);
tag.write(f);
f.close();
}
template <class T>
void load_tag(const std::string &file, T &tag)
{
File f(file, FILE_TYPE_DATA);
f.open(OPEN_READ);
tag.read(f);
f.close();
}
void artist_test_tags(Artist &artist)
{
test_results(artist.name == "Artist Name", __LINE__);
test_results(artist.lower == "artist name", __LINE__);
test_results(artist.primary_key() == "Artist Name", __LINE__);
}
void artist_test()
{
Artist artist("Artist Name"), artist2;
artist_test_tags(artist);
save_tag("artist.txt", artist);
load_tag("artist.txt", artist2);
artist_test_tags(artist2);
}
void album_test_tags(Album &album)
{
test_results(album.name == "Album Name", __LINE__);
test_results(album.lower == "album name", __LINE__);
test_results(album.year == 2014, __LINE__);
test_results(album.primary_key() == "2014.Album Name", __LINE__);
}
void album_test()
{
Album album("Album Name", 2014), album2;
album_test_tags(album);
save_tag("album.txt", album);
load_tag("album.txt", album2);
album_test_tags(album2);
}
void genre_test_tags(Genre &genre)
{
test_results(genre.name == "Genre Name", __LINE__);
test_results(genre.lower == "genre name", __LINE__);
test_results(genre.primary_key() == "Genre Name", __LINE__);
}
void genre_test()
{
Genre genre("Genre Name"), genre2;
genre_test_tags(genre);
save_tag("genre.txt", genre);
load_tag("genre.txt", genre2);
genre_test_tags(genre2);
}
void library_test_tags(Library &library)
{
test_results(library.root_path == "/home/user/Music", __LINE__);
test_results(library.count == 0, __LINE__);
test_results(library.enabled == true, __LINE__);
test_results(library.primary_key() == "/home/user/Music", __LINE__);
}
void library_test()
{
Library library("/home/user/Music"), library2;
library_test_tags(library);
test_results(library2.enabled == false, __LINE__);
save_tag("library.txt", library);
load_tag("library.txt", library2);
library_test_tags(library2);
}
int main(int argc, char **argv)
{
char c;
while ((c = getopt(argc, argv, "aAgl")) != -1) {
switch (c) {
case 'a':
artist_test();
break;
case 'A':
album_test();
break;
case 'g':
genre_test();
break;
case 'l':
library_test();
break;
}
}
return 0;
}

18
tests/tag_db Executable file
View File

@ -0,0 +1,18 @@
#!/bin/bash
# Copyright 2014 (c) Anna Schumaker
. $(dirname $0)/_functions
function test_tag
{
new_test "Test $1 Tag"
./src/tags.run "$2"
}
test_tag "Artist" "-a"
echo
test_tag "Album" "-A"
echo
test_tag "Genre" "-g"
echo
test_tag "Library" "-l"