diff --git a/src/Makefile.am b/src/Makefile.am index 84f7aaf..860f0f0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ bin_PROGRAMS = cellar cellar_CPPFLAGS = $(BOOST_CPPFLAGS) cellar_LDFLAGS = $(BOOST_LDFLAGS) $(BOOST_SYSTEM_LIB) $(BOOST_FILESYSTEM_LIB) -cellar_SOURCES = cellar.cpp fs.cpp +cellar_SOURCES = cellar.cpp fs.cpp bottles.cpp diff --git a/src/bottles.cpp b/src/bottles.cpp new file mode 100644 index 0000000..50544aa --- /dev/null +++ b/src/bottles.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +#include +#include +#include "json.hpp" + +#include "bottles.hpp" +#include "fs.hpp" + +using namespace std; + +using json = nlohmann::json; + +vector cellar::bottles::list() { + stringstream sstr_output; + vector 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 << " "; + + string jsonpath = homepath + "/" + item + "/cellar.json"; + if (boost::filesystem::exists(jsonpath)) { + try { + json config; + 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 + } + catch (const exception &exc) { + sstr_output << "- bogus cellar.json file"; + result.push_back(sstr_output.str()); + sstr_output.str(""); + } + } + else { + sstr_output << "- anonymous wine bottle"; + result.push_back(sstr_output.str()); + sstr_output.str(""); + } + } + } + + return result; +} diff --git a/src/bottles.hpp b/src/bottles.hpp new file mode 100644 index 0000000..afe2c31 --- /dev/null +++ b/src/bottles.hpp @@ -0,0 +1,16 @@ +#ifndef __BOTTLES_HPP +#define __BOTTLES_HPP +#pragma once + +#include +#include + +using namespace std; + +namespace cellar { + namespace bottles { + vector list(); + } +} + +#endif // __BOTTLES_HPP diff --git a/src/cellar.cpp b/src/cellar.cpp index 6f9965e..0fd08ed 100644 --- a/src/cellar.cpp +++ b/src/cellar.cpp @@ -1,45 +1,19 @@ -#include -#include #include #include -#include #include -#include -#include #include "json.hpp" -#include "fs.hpp" +#include "bottles.hpp" using namespace std; using json = nlohmann::json; int main(int argc, char* argv[]) { - string homepath = getenv("HOME"); - vector homedir = cellar::fs::listdir(homepath); - for (string item : homedir) { - if (item.substr(0,5) == ".wine") { - cout << item; - cout << " "; - - string jsonpath = homepath + "/" + item + "/cellar.json"; - if (boost::filesystem::exists(jsonpath)) { - try { - json config; - ifstream configstream(jsonpath); - stringstream sstr; - sstr << configstream.rdbuf(); - config = json::parse(sstr.str()); - - cout << "- " << config["name"]; - } - catch (const exception &exc) { - cout << "- bogus cellar.json file" << endl; - } - } - else { cout << "- anonymous wine bottle" << endl; } - } + vector bottles = cellar::bottles::list(); + for (string item : bottles) { + cout << item << endl; } - cout << endl; - return 0; + + return 0; }