From 0e6355329f241a3e3c8b38cb4146fda4470e6ed0 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 26 Mar 2018 15:33:06 -0400 Subject: Whitespace changes --- cadence.cpp | 54 +++++++++++++++++++++++------------------------ generator/database.cpp | 14 ++++++------- generator/database.h | 2 +- generator/field.h | 12 +++++------ generator/generator.cpp | 56 ++++++++++++++++++++++++------------------------- generator/generator.h | 36 +++++++++++++++---------------- generator/main.cpp | 2 +- generator/mood.cpp | 42 ++++++++++++++++++------------------- generator/mood.h | 22 +++++++++---------- generator/progress.h | 16 +++++++------- util.h | 24 ++++++++++----------- 11 files changed, 140 insertions(+), 140 deletions(-) diff --git a/cadence.cpp b/cadence.cpp index c91ca4a..80e4a05 100644 --- a/cadence.cpp +++ b/cadence.cpp @@ -24,16 +24,16 @@ int main(int argc, char** argv) std::string configfile(argv[1]); YAML::Node config = YAML::LoadFile(configfile); - + // Set up the Twitter client. twitter::auth auth; auth.setConsumerKey(config["consumer_key"].as()); auth.setConsumerSecret(config["consumer_secret"].as()); auth.setAccessKey(config["access_key"].as()); auth.setAccessSecret(config["access_secret"].as()); - + twitter::client client(auth); - + // Read in the forms file. std::map> groups; std::ifstream datafile(config["forms"].as()); @@ -42,7 +42,7 @@ int main(int argc, char** argv) std::cout << "Could not find forms file." << std::endl; return 1; } - + bool newgroup = true; std::string line; std::string curgroup; @@ -52,7 +52,7 @@ int main(int argc, char** argv) { line.pop_back(); } - + if (newgroup) { curgroup = line; @@ -70,10 +70,10 @@ int main(int argc, char** argv) // Initialize the random number generator. std::random_device random_device; 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 @@ -82,19 +82,19 @@ int main(int argc, char** argv) sqlite3_close_v2(ppdb); std::cout << "Could not open acoustic datafile." << std::endl; - + return 1; } for (;;) { std::cout << "Generating tweet..." << std::endl; - + 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"; @@ -102,12 +102,12 @@ int main(int argc, char** argv) { 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)); @@ -115,10 +115,10 @@ int main(int argc, char** argv) } else { std::cout << "Error reading from acoustic datafile:" << std::endl; std::cout << sqlite3_errmsg(ppdb) << std::endl; - + return 1; } - + sqlite3_finalize(ppstmt); std::string action = "{" + cadence::uppercase(mood) + "}"; @@ -133,12 +133,12 @@ int main(int argc, char** argv) modifier = token.substr(modloc+1); token = token.substr(0, modloc); } - + std::string canontkn; std::transform(std::begin(token), std::end(token), std::back_inserter(canontkn), [] (char ch) { return std::toupper(ch); }); - + std::string result; if (canontkn == "\\N") { @@ -157,10 +157,10 @@ int main(int argc, char** argv) } else { std::cout << "Badly formatted forms file:" << std::endl; std::cout << "No such form as " + canontkn << std::endl; - + return 1; } - + if (modifier == "indefinite") { if ((result.length() > 1) && (isupper(result[0])) && (isupper(result[1]))) @@ -173,7 +173,7 @@ int main(int argc, char** argv) result = "a " + result; } } - + std::string finalresult; if (islower(token[0])) { @@ -187,21 +187,21 @@ int main(int argc, char** argv) { word[0] = std::toupper(word[0]); } - + finalresult = cadence::implode(std::begin(words), std::end(words), " "); } else { finalresult = result; } - + action.replace(tknloc, action.find("}")-tknloc+1, finalresult); } - + if (action.size() <= 140) { try { client.updateStatus(action); - + std::cout << action << std::endl; } catch (const twitter::twitter_error& e) { @@ -209,9 +209,9 @@ int main(int argc, char** argv) } std::cout << "Waiting..." << std::endl; - + std::this_thread::sleep_for(std::chrono::hours(1)); - + std::cout << std::endl; } else { std::cout << "Tweet was too long, regenerating." << std::endl; diff --git a/generator/database.cpp b/generator/database.cpp index f326f46..b46a0d1 100644 --- a/generator/database.cpp +++ b/generator/database.cpp @@ -73,19 +73,19 @@ namespace cadence { { sqlite3_close_v2(ppdb_); } - + void database::runQuery(std::string query) { // This can only happen when doing bad things with move semantics. assert(ppdb_ != nullptr); - + sqlite3_stmt* ppstmt; if (sqlite3_prepare_v2(ppdb_, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) { throw sqlite3_error("Error writing to database", sqlite3_errmsg(ppdb_)); } - + int result = sqlite3_step(ppstmt); sqlite3_finalize(ppstmt); @@ -99,10 +99,10 @@ namespace cadence { { // This can only happen when doing bad things with move semantics. assert(ppdb_ != nullptr); - + // This shouldn't happen. assert(!fields.empty()); - + std::list fieldNames; std::list qs; for (field& f : fields) @@ -110,7 +110,7 @@ namespace cadence { fieldNames.push_back(f.getName()); qs.push_back("?"); } - + std::ostringstream query; query << "INSERT INTO "; query << table; @@ -119,7 +119,7 @@ namespace cadence { query << ") VALUES ("; query << implode(std::begin(qs), std::end(qs), ", "); query << ")"; - + std::string query_str = query.str(); sqlite3_stmt* ppstmt; diff --git a/generator/database.h b/generator/database.h index 7aa8dfa..e4f3ba2 100644 --- a/generator/database.h +++ b/generator/database.h @@ -52,7 +52,7 @@ namespace cadence { ~database(); // Actions - + void runQuery(std::string query); void insertIntoTable(std::string table, std::list fields); diff --git a/generator/field.h b/generator/field.h index 9d6ed9a..836c079 100644 --- a/generator/field.h +++ b/generator/field.h @@ -32,27 +32,27 @@ namespace cadence { ~field(); // Generic accessors - + type getType() const { return type_; } - + std::string getName() const { return name_; } // Integer - + field(std::string name, int arg); - + int getInteger() const; // String - + field(std::string name, std::string arg); - + std::string getString() const; private: diff --git a/generator/generator.cpp b/generator/generator.cpp index eb2750c..54f5d69 100644 --- a/generator/generator.cpp +++ b/generator/generator.cpp @@ -11,7 +11,7 @@ namespace cadence { namespace generator { - + generator::generator( std::string inputpath, std::string outputpath) : @@ -23,23 +23,23 @@ namespace cadence { { inputpath_ += '/'; } - + inputpath_ += "highlevel/"; } - + void generator::run() { // Creates the datafile. writeSchema(); - + // Scans the AcousticBrainz data dump and generates a list of all of the // files in the dump. scanDirectories(); - + // Parses each data file and enters it into the database. parseData(); } - + void generator::writeSchema() { std::ifstream file("schema.sql"); @@ -47,7 +47,7 @@ namespace cadence { { throw std::invalid_argument("Could not find database schema"); } - + std::ostringstream schemaBuilder; std::string line; while (std::getline(file, line)) @@ -56,10 +56,10 @@ namespace cadence { { line.pop_back(); } - + schemaBuilder << line; } - + std::string schema = schemaBuilder.str(); auto queries = split>(schema, ";"); progress ppgs("Writing database schema...", queries.size()); @@ -69,73 +69,73 @@ namespace cadence { { db_.runQuery(query); } - + ppgs.update(); } } - + void generator::scanDirectories() { std::cout << "Scanning AcousticBrainz dump..." << std::endl; - + DIR* topdir; if ((topdir = opendir(inputpath_.c_str())) == nullptr) { throw std::invalid_argument("Invalid AcousticBrainz data directory"); } - + struct dirent* topent; while ((topent = readdir(topdir)) != nullptr) { if (topent->d_name[0] != '.') { std::string directory = inputpath_ + topent->d_name + "/"; - + DIR* subdir; if ((subdir = opendir(directory.c_str())) == nullptr) { throw std::invalid_argument("Invalid AcousticBrainz data directory"); } - + struct dirent* subent; while ((subent = readdir(subdir)) != nullptr) { if (subent->d_name[0] != '.') { std::string subdirectory = directory + subent->d_name + "/"; - + DIR* subsubdir; if ((subsubdir = opendir(subdirectory.c_str())) == nullptr) { throw std::invalid_argument("Invalid AcousticBrainz data directory"); } - + struct dirent* subsubent; while ((subsubent = readdir(subsubdir)) != nullptr) { if (subsubent->d_name[0] != '.') { std::string datafile = subdirectory + subsubent->d_name; - + datafiles_.push_back(datafile); } } - + closedir(subsubdir); } } - + closedir(subdir); } } - + closedir(topdir); } - + void generator::parseData() { progress ppgs("Parsing AcousticBrainz data files...", datafiles_.size()); - + for (std::string datafile : datafiles_) { ppgs.update(); @@ -145,7 +145,7 @@ namespace cadence { std::ifstream dataStream(datafile); dataStream >> jsonData; } - + try { std::vector moods; @@ -158,16 +158,16 @@ namespace cadence { moods.emplace_back(mood::type::relaxed, jsonData["highlevel"]["mood_relaxed"]["all"]["relaxed"]); moods.emplace_back(mood::type::sad, jsonData["highlevel"]["mood_sad"]["all"]["sad"]); moods.emplace_back(mood::type::instrumental, jsonData["highlevel"]["voice_instrumental"]["all"]["instrumental"]); - + std::sort(std::begin(moods), std::end(moods), [] (const mood& left, const mood& right) -> bool { return left.getProbability() > right.getProbability(); }); - + std::list fields; fields.emplace_back("title", jsonData["metadata"]["tags"]["title"][0].get()); fields.emplace_back("artist", jsonData["metadata"]["tags"]["artist"][0].get()); fields.emplace_back("category", moods.front().getCategory()); - + db_.insertIntoTable("songs", std::move(fields)); } catch (const std::domain_error& ex) { @@ -175,6 +175,6 @@ namespace cadence { } } } - + }; }; diff --git a/generator/generator.h b/generator/generator.h index d911c61..dff8eeb 100644 --- a/generator/generator.h +++ b/generator/generator.h @@ -7,44 +7,44 @@ namespace cadence { namespace generator { - + class generator { public: - + // Constructor - + generator( std::string inputpath, std::string outputpath); - + // Action - + void run(); - + // Subroutines - + void writeSchema(); - + void scanDirectories(); - + void parseData(); - + private: - + // Input - + std::string inputpath_; - + // Output - + database db_; - + // Cache - + std::list datafiles_; - + }; - + }; }; diff --git a/generator/main.cpp b/generator/main.cpp index 5a3d2dd..318770e 100644 --- a/generator/main.cpp +++ b/generator/main.cpp @@ -16,7 +16,7 @@ int main(int argc, char** argv) try { cadence::generator::generator app(argv[1], argv[2]); - + try { app.run(); diff --git a/generator/mood.cpp b/generator/mood.cpp index 9f53dce..b74211c 100644 --- a/generator/mood.cpp +++ b/generator/mood.cpp @@ -2,7 +2,7 @@ namespace cadence { namespace generator { - + // The categories are: // - party (+danceable, -acoustic, +electronic, +party) = ~.21 // - chill (-danceable, +acoustic, -aggressive, -electronic, -party, +relaxed) = ~.49 @@ -11,7 +11,7 @@ namespace cadence { // - sad (-happy, +sad) = ~.02 // - instrumental (+instrumental) = ~.12 // - vocal (-instrumental) = ~.10 - + mood::mood(type t, double prob) : type_(t) { if (prob >= 0.5) @@ -22,73 +22,73 @@ namespace cadence { probability_ = 1.0 - prob; positive_ = false; } - + switch (t) { case type::danceable: { category_ = (positive_ ? "party" : "chill"); - + break; } - + case type::acoustic: { category_ = (positive_ ? "chill" : "party"); - + break; } - + case type::aggressive: { category_ = (positive_ ? "crazy" : "chill"); - + break; } - + case type::electronic: { category_ = (positive_ ? "party" : "chill"); - + break; } - + case type::happy: { category_ = (positive_ ? "happy" : "sad"); - + break; } - + case type::party: { category_ = (positive_ ? "party" : "chill"); - + break; } - + case type::relaxed: { category_ = (positive_ ? "chill" : "crazy"); - + break; } - + case type::sad: { category_ = (positive_ ? "sad" : "happy"); - + break; } - + case type::instrumental: { category_ = (positive_ ? "instrumental" : "vocal"); - + break; } } } - + }; }; diff --git a/generator/mood.h b/generator/mood.h index df5d28f..c36e5ee 100644 --- a/generator/mood.h +++ b/generator/mood.h @@ -5,7 +5,7 @@ namespace cadence { namespace generator { - + class mood { public: enum class type { @@ -19,41 +19,41 @@ namespace cadence { sad, instrumental }; - + // Constructor - + mood(type t, double prob); - + // Accessors - + type getType() const { return type_; } - + double getProbability() const { return probability_; } - + bool getPositive() const { return positive_; } - + std::string getCategory() const { return category_; } - + private: type type_; double probability_; bool positive_; std::string category_; - + }; - + }; }; diff --git a/generator/progress.h b/generator/progress.h index f42f701..e5cc13d 100644 --- a/generator/progress.h +++ b/generator/progress.h @@ -5,20 +5,20 @@ namespace cadence { namespace generator { - + class progress { private: std::string message; int total; int cur = 0; int lprint = 0; - + public: progress(std::string message, int total) : message(message), total(total) { std::cout << message << " 0%" << std::flush; } - + void update(int val) { if (val <= total) @@ -27,29 +27,29 @@ namespace cadence { } else { cur = total; } - + int pp = cur * 100 / total; if (pp != lprint) { lprint = pp; - + std::cout << "\b\b\b\b" << std::right; std::cout.width(3); std::cout << pp << "%" << std::flush; } } - + void update() { update(cur+1); } - + ~progress() { std::cout << "\b\b\b\b100%" << std::endl; } }; - + }; }; diff --git a/util.h b/util.h index 8a476da..5d16649 100644 --- a/util.h +++ b/util.h @@ -8,7 +8,7 @@ #include namespace cadence { - + inline std::string uppercase(std::string in) { std::string result; @@ -16,28 +16,28 @@ namespace cadence { { return std::toupper(ch); }); - + return result; } - + template std::string implode(InputIterator first, InputIterator last, std::string delimiter) { std::stringstream result; - + for (InputIterator it = first; it != last; it++) { if (it != first) { result << delimiter; } - + result << *it; } - + return result.str(); } - + template void split(std::string input, std::string delimiter, OutputIterator out) { @@ -48,12 +48,12 @@ namespace cadence { { *out = input; out++; - + input = ""; } else { *out = input.substr(0, divider); out++; - + input = input.substr(divider+delimiter.length()); } } @@ -63,12 +63,12 @@ namespace cadence { Container split(std::string input, std::string delimiter) { Container result; - + split(input, delimiter, std::back_inserter(result)); - + return result; } - + }; #endif /* end of include guard: UTIL_H_CED7A66D */ -- cgit 1.4.1