moved to separate file

This commit is contained in:
Nicholas O'Connor 2017-03-13 19:03:07 -07:00
parent 252b2fdfe6
commit f69095d7ab
4 changed files with 80 additions and 33 deletions

View File

@ -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

57
src/bottles.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <cstdlib>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include "json.hpp"
#include "bottles.hpp"
#include "fs.hpp"
using namespace std;
using json = nlohmann::json;
vector<string> cellar::bottles::list() {
stringstream sstr_output;
vector<string> result;
string homepath = getenv("HOME");
vector<string> 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;
}

16
src/bottles.hpp Normal file
View File

@ -0,0 +1,16 @@
#ifndef __BOTTLES_HPP
#define __BOTTLES_HPP
#pragma once
#include <vector>
#include <string>
using namespace std;
namespace cellar {
namespace bottles {
vector<string> list();
}
}
#endif // __BOTTLES_HPP

View File

@ -1,45 +1,19 @@
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#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<string> 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<string> bottles = cellar::bottles::list();
for (string item : bottles) {
cout << item << endl;
}
cout << endl;
return 0;
return 0;
}