From 9cf038ebc07cff3f7557852672ddb026743b7a8a Mon Sep 17 00:00:00 2001 From: Nicholas O'Connor Date: Wed, 22 Mar 2017 19:02:14 -0700 Subject: [PATCH] list becomes get_bottles, which returns a map --- src/bottles.cpp | 50 ++++++++++++++++++++++++++----------------------- src/bottles.hpp | 22 ++++++++++++++++++++-- src/cellar.cpp | 26 ++++++++++++++++++++++--- 3 files changed, 70 insertions(+), 28 deletions(-) diff --git a/src/bottles.cpp b/src/bottles.cpp index f251e13..7d714e0 100644 --- a/src/bottles.cpp +++ b/src/bottles.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -12,31 +13,38 @@ #include "fs.hpp" using namespace std; +using namespace cellar::bottles; using json = nlohmann::json; -vector cellar::bottles::list() { - stringstream sstr_output; - vector result; +Bottle::Bottle() { + // define a null bottle + // strings handle themselves + config = json({}); + type = bottle_anonymous; +} + +map cellar::bottles::get_bottles() { + map result; string homepath = getenv("HOME"); vector homedir = cellar::fs::listdir(homepath); for (string item : homedir) { if (item.substr(0,5) == ".wine") { - sstr_output << item; - sstr_output << " "; - + Bottle output; + string fullitem = homepath + "/" + item; + output.path = fullitem; + boost::filesystem::file_status fullitem_status = boost::filesystem::symlink_status(fullitem); bool symlink = boost::filesystem::is_symlink(fullitem_status); if (symlink) { - sstr_output << "- symlink to "; boost::filesystem::path realpath = boost::filesystem::canonical(fullitem); - sstr_output << realpath.string(); - result.push_back(sstr_output.str()); - sstr_output.str(""); + output.canonical_path = realpath.string(); + output.type = bottle_symlink; } else { + output.canonical_path = fullitem; string jsonpath = fullitem + "/cellar.json"; if (boost::filesystem::exists(jsonpath)) { try { @@ -44,25 +52,21 @@ vector cellar::bottles::list() { ifstream configstream(jsonpath); stringstream sstr_config; sstr_config << configstream.rdbuf(); - config = json::parse(sstr_config.str()); - - sstr_output << "- " << config["name"]; - result.push_back(sstr_output.str()); - sstr_output.str(""); // clear it for the next item + config = json::parse(sstr_config.str()); + + output.config = config; + output.type = bottle_labelled; } catch (const exception &exc) { - sstr_output << "- bogus cellar.json file"; - result.push_back(sstr_output.str()); - sstr_output.str(""); + output.type = bottle_error; } } - else { - sstr_output << "- anonymous wine bottle"; - result.push_back(sstr_output.str()); - sstr_output.str(""); + else { + output.type = bottle_anonymous; } } - } + result[item] = output; + } } return result; diff --git a/src/bottles.hpp b/src/bottles.hpp index afe2c31..e5192ee 100644 --- a/src/bottles.hpp +++ b/src/bottles.hpp @@ -2,14 +2,32 @@ #define __BOTTLES_HPP #pragma once -#include +#include #include +#include "json.hpp" + using namespace std; +using json = nlohmann::json; + namespace cellar { namespace bottles { - vector list(); + enum bottle_type { + bottle_error, + bottle_anonymous, + bottle_labelled, + bottle_symlink + }; + class Bottle { + public: + bottle_type type; + json config; + string path; + string canonical_path; + Bottle(); + }; + map get_bottles(); } } diff --git a/src/cellar.cpp b/src/cellar.cpp index 0fd08ed..bd03324 100644 --- a/src/cellar.cpp +++ b/src/cellar.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -7,12 +8,31 @@ #include "bottles.hpp" using namespace std; +using namespace cellar; using json = nlohmann::json; int main(int argc, char* argv[]) { - vector bottles = cellar::bottles::list(); - for (string item : bottles) { - cout << item << endl; + map bottles = bottles::get_bottles(); + for (auto& item : bottles) { + cout << item.first << "- "; + bottles::Bottle current = item.second; + + switch (current.type) { + case bottles::bottle_anonymous: + cout << "anonymous wine bottle"; + break; + case bottles::bottle_labelled: + cout << current.config["name"]; + break; + case bottles::bottle_symlink: + cout << "symlink to "; + cout << current.canonical_path; + break; + default: + cout << "broken or unsupported wine bottle"; + } + + cout << endl; } return 0;