From af49d3435302118d036c512aef1def848fb88089 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 27 Mar 2018 17:13:37 -0400 Subject: Migrated to hkutil --- cadence.cpp | 58 ++++++++++++++-------------------------------------------- 1 file changed, 14 insertions(+), 44 deletions(-) (limited to 'cadence.cpp') diff --git a/cadence.cpp b/cadence.cpp index 80e4a05..704f946 100644 --- a/cadence.cpp +++ b/cadence.cpp @@ -10,7 +10,8 @@ #include #include #include -#include "util.h" +#include +#include const std::vector moods = {"party", "chill", "crazy", "happy", "sad", "instrumental", "vocal"}; @@ -72,19 +73,9 @@ int main(int argc, char** argv) std::mt19937 random_engine{random_device()}; // Connect to the AcousticBrainz data file. - sqlite3* ppdb = nullptr; - - if (sqlite3_open_v2(config["acoustic_datafile"].as().c_str(), &ppdb, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) - { - // We still have to free the resources allocated. In the event that - // allocation failed, ppdb will be null and sqlite3_close_v2 will just - // ignore it. - sqlite3_close_v2(ppdb); - - std::cout << "Could not open acoustic datafile." << std::endl; - - return 1; - } + hatkirby::database abdb( + config["acoustic_datafile"].as(), + hatkirby::dbmode::read); for (;;) { @@ -92,36 +83,15 @@ int main(int argc, char** argv) std::string mood = moods[std::uniform_int_distribution(0, moods.size()-1)(random_engine)]; - std::string songTitle; - std::string songArtist; - - sqlite3_stmt* ppstmt; - std::string query = "SELECT title, artist FROM songs WHERE category = ? ORDER BY RANDOM() LIMIT 1"; - - if (sqlite3_prepare_v2(ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) - { - std::cout << "Error reading from acoustic datafile:" << std::endl; - std::cout << sqlite3_errmsg(ppdb) << std::endl; - - return 1; - } - - sqlite3_bind_text(ppstmt, 1, mood.c_str(), mood.length(), SQLITE_TRANSIENT); - - if (sqlite3_step(ppstmt) == SQLITE_ROW) - { - songTitle = reinterpret_cast(sqlite3_column_blob(ppstmt, 0)); - songArtist = reinterpret_cast(sqlite3_column_blob(ppstmt, 1)); - } else { - std::cout << "Error reading from acoustic datafile:" << std::endl; - std::cout << sqlite3_errmsg(ppdb) << std::endl; - - return 1; - } + hatkirby::row song = + abdb.queryFirst( + "SELECT title, artist FROM songs WHERE category = ? ORDER BY RANDOM() LIMIT 1", + { mood }); - sqlite3_finalize(ppstmt); + std::string songTitle = mpark::get(song[0]); + std::string songArtist = mpark::get(song[1]); - std::string action = "{" + cadence::uppercase(mood) + "}"; + std::string action = "{" + hatkirby::uppercase(mood) + "}"; int tknloc; while ((tknloc = action.find("{")) != std::string::npos) { @@ -182,13 +152,13 @@ int main(int argc, char** argv) }); } else if (isupper(token[0]) && !isupper(token[1])) { - auto words = cadence::split>(result, " "); + auto words = hatkirby::split>(result, " "); for (auto& word : words) { word[0] = std::toupper(word[0]); } - finalresult = cadence::implode(std::begin(words), std::end(words), " "); + finalresult = hatkirby::implode(std::begin(words), std::end(words), " "); } else { finalresult = result; } -- cgit 1.4.1