list becomes get_bottles, which returns a map<string, Bottle>

This commit is contained in:
Nicholas O'Connor 2017-03-22 19:02:14 -07:00
parent 05eeca49c9
commit 9cf038ebc0
3 changed files with 70 additions and 28 deletions

View File

@ -1,5 +1,6 @@
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
#include <map>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
@ -12,31 +13,38 @@
#include "fs.hpp" #include "fs.hpp"
using namespace std; using namespace std;
using namespace cellar::bottles;
using json = nlohmann::json; using json = nlohmann::json;
vector<string> cellar::bottles::list() { Bottle::Bottle() {
stringstream sstr_output; // define a null bottle
vector<string> result; // strings handle themselves
config = json({});
type = bottle_anonymous;
}
map<string, Bottle> cellar::bottles::get_bottles() {
map<string, Bottle> result;
string homepath = getenv("HOME"); string homepath = getenv("HOME");
vector<string> homedir = cellar::fs::listdir(homepath); vector<string> homedir = cellar::fs::listdir(homepath);
for (string item : homedir) { for (string item : homedir) {
if (item.substr(0,5) == ".wine") { if (item.substr(0,5) == ".wine") {
sstr_output << item; Bottle output;
sstr_output << " ";
string fullitem = homepath + "/" + item; string fullitem = homepath + "/" + item;
output.path = fullitem;
boost::filesystem::file_status fullitem_status = boost::filesystem::symlink_status(fullitem); boost::filesystem::file_status fullitem_status = boost::filesystem::symlink_status(fullitem);
bool symlink = boost::filesystem::is_symlink(fullitem_status); bool symlink = boost::filesystem::is_symlink(fullitem_status);
if (symlink) { if (symlink) {
sstr_output << "- symlink to ";
boost::filesystem::path realpath = boost::filesystem::canonical(fullitem); boost::filesystem::path realpath = boost::filesystem::canonical(fullitem);
sstr_output << realpath.string(); output.canonical_path = realpath.string();
result.push_back(sstr_output.str()); output.type = bottle_symlink;
sstr_output.str("");
} else { } else {
output.canonical_path = fullitem;
string jsonpath = fullitem + "/cellar.json"; string jsonpath = fullitem + "/cellar.json";
if (boost::filesystem::exists(jsonpath)) { if (boost::filesystem::exists(jsonpath)) {
try { try {
@ -46,22 +54,18 @@ vector<string> cellar::bottles::list() {
sstr_config << configstream.rdbuf(); sstr_config << configstream.rdbuf();
config = json::parse(sstr_config.str()); config = json::parse(sstr_config.str());
sstr_output << "- " << config["name"]; output.config = config;
result.push_back(sstr_output.str()); output.type = bottle_labelled;
sstr_output.str(""); // clear it for the next item
} }
catch (const exception &exc) { catch (const exception &exc) {
sstr_output << "- bogus cellar.json file"; output.type = bottle_error;
result.push_back(sstr_output.str());
sstr_output.str("");
} }
} }
else { else {
sstr_output << "- anonymous wine bottle"; output.type = bottle_anonymous;
result.push_back(sstr_output.str());
sstr_output.str("");
} }
} }
result[item] = output;
} }
} }

View File

@ -2,14 +2,32 @@
#define __BOTTLES_HPP #define __BOTTLES_HPP
#pragma once #pragma once
#include <vector> #include <map>
#include <string> #include <string>
#include "json.hpp"
using namespace std; using namespace std;
using json = nlohmann::json;
namespace cellar { namespace cellar {
namespace bottles { namespace bottles {
vector<string> 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<string, Bottle> get_bottles();
} }
} }

View File

@ -1,4 +1,5 @@
#include <iostream> #include <iostream>
#include <map>
#include <string> #include <string>
#include <vector> #include <vector>
@ -7,12 +8,31 @@
#include "bottles.hpp" #include "bottles.hpp"
using namespace std; using namespace std;
using namespace cellar;
using json = nlohmann::json; using json = nlohmann::json;
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
vector<string> bottles = cellar::bottles::list(); map<string, bottles::Bottle> bottles = bottles::get_bottles();
for (string item : bottles) { for (auto& item : bottles) {
cout << item << endl; 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; return 0;