From 134348f2534ee64ef5b32c1f7e15aca377b094e0 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 22 Mar 2014 21:44:51 -0400 Subject: [PATCH] tags: Begin creating the tag database This patch adds classes for Artist, Album, Genre and Library. Signed-off-by: Anna Schumaker --- DESIGN | 22 ++++---- include/tags.h | 63 +++++++++++++++++++++ lib/tags.cpp | 137 ++++++++++++++++++++++++++++++++++++++++++++ tests/Sconscript | 3 +- tests/src/tags.cpp | 138 +++++++++++++++++++++++++++++++++++++++++++++ tests/tag_db | 18 ++++++ 6 files changed, 370 insertions(+), 11 deletions(-) create mode 100644 include/tags.h create mode 100644 lib/tags.cpp create mode 100644 tests/src/tags.cpp create mode 100755 tests/tag_db diff --git a/DESIGN b/DESIGN index 575fc09d..7d813719 100644 --- a/DESIGN +++ b/DESIGN @@ -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. diff --git a/include/tags.h b/include/tags.h new file mode 100644 index 00000000..1aba8396 --- /dev/null +++ b/include/tags.h @@ -0,0 +1,63 @@ +/* + * Copyright 2014 (c) Anna Schumaker. + */ +#ifndef OCARINA_TAGS_H +#define OCARINA_TAGS_H + +#include + + +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 */ diff --git a/lib/tags.cpp b/lib/tags.cpp new file mode 100644 index 00000000..b80b3f6d --- /dev/null +++ b/lib/tags.cpp @@ -0,0 +1,137 @@ +/* + * Copyright 2014 (c) Anna Schumaker. + */ + +#include +#include + +#include + + +/** + * + * 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; +} diff --git a/tests/Sconscript b/tests/Sconscript index 25fbea6c..887c4420 100644 --- a/tests/Sconscript +++ b/tests/Sconscript @@ -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 diff --git a/tests/src/tags.cpp b/tests/src/tags.cpp new file mode 100644 index 00000000..e330a6d2 --- /dev/null +++ b/tests/src/tags.cpp @@ -0,0 +1,138 @@ +/* + * Copyright 2014 (c) Anna Schumaker. + * Test a DatabaseEntry + */ + +#include +#include + +#include +#include + + +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 +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 +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; +} diff --git a/tests/tag_db b/tests/tag_db new file mode 100755 index 00000000..007945b9 --- /dev/null +++ b/tests/tag_db @@ -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"