...
 
Commits (2)
#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <sstream>
#include "Package.hh"
namespace Parcel {
......@@ -51,9 +54,21 @@ Package::Package(std::istream& from_pkginfo)
case 'A':
_arch = raw_value;
break;
#define ATOM_CASE(CURRENT_TYPE) {\
std::istringstream atom_stream(raw_value);\
CURRENT_TYPE = {std::istream_iterator<string>(atom_stream), std::istream_iterator<string>()};\
break;\
}
case 'D': ATOM_CASE(_dependencies)
case 'p': ATOM_CASE(_provides)
case 'r': ATOM_CASE(_replaces)
#undef ATOM_CASE
case 'm':
_maintainer = raw_value;
break;
case 't':
_built_at = std::stoul(raw_value);
break;
case 'c':
_commit_id = raw_value;
break;
......
......@@ -29,11 +29,11 @@ public:
}
}
const string version() {
const string version() const {
return this->_version;
}
unsigned long revision() {
unsigned long revision() const {
return this->_revision;
}
......
......@@ -20,7 +20,7 @@ Repository::Repository(std::istream& from_index) {
// Empty line means this package is completely read.
std::istringstream stream(nextpkg);
_packages.push_back(Package(stream));
_packages.push_back(new Package(stream));
nextpkg = "";
continue;
}
......@@ -32,22 +32,37 @@ Repository::Repository(std::istream& from_index) {
std::cerr << "I/O FAILURE parsing repository index file" << std::endl;
}
for(auto pkg : _packages) _origins.insert(pkg.origin());
for(auto pkg : _packages) _origins.insert(pkg->origin());
}
vector<Package>::iterator find_package_by_name(vector<Package> packages, string name) {
return find_if(packages.begin(), packages.end(),
[name](Package elem) {
return elem.pkgname() == name;
Repository::~Repository() {
for(auto pkg : _packages) { delete pkg; }
}
inline vector<Package*>::const_iterator find_package_by_name(vector<Package*> packages, string name) {
return find_if(packages.cbegin(), packages.cend(),
[name](Package* elem) {
return elem->pkgname() == name;
});
}
bool Repository::has_package(string name) {
return find_package_by_name(this->_packages, name) != this->_packages.end();
return find_package_by_name(this->_packages, name) != this->_packages.cend();
}
Package Repository::package(string name) {
return *find_package_by_name(this->_packages, name);
Package* Repository::package(string name) {
vector<Package*> maybe_pkgs;
std::copy_if(this->_packages.cbegin(), this->_packages.cend(), std::back_inserter(maybe_pkgs),
[name](Package* elem) {
return elem->pkgname() == name;
});
if(maybe_pkgs.size() == 0) return nullptr;
return *std::max_element(maybe_pkgs.cbegin(), maybe_pkgs.cend(), [](Package* elem1, Package* elem2) {
return elem1->pkgver() < elem2->pkgver();
});
}
}
......@@ -12,15 +12,20 @@ class Repository
{
public:
Repository(std::istream& from_index);
~Repository();
std::vector<Package> packages() { return _packages; }
/*! All packages in this repository. */
std::vector<Package*> packages() { return _packages; }
/*! True if at least one Package is present in this repository with
* the specified name. */
bool has_package(string name);
Package package(string name);
/*! Returns the newest version of the package specified by name. */
Package* package(string name);
unsigned long package_count() { return _packages.size(); }
std::vector<string> origins() { return std::vector<string>(_origins.begin(), _origins.end()); }
unsigned long origin_count() { return _origins.size(); }
private:
std::vector<Package> _packages;
std::vector<Package*> _packages;
std::unordered_set<string> _origins;
};
......
......@@ -21,26 +21,30 @@ Repository *make_repo_from_file(string filename) {
int main(int argc, char* argv[])
{
Repository* repo;
Package *pkg;
if(argc < 2) return usage();
if(argc == 2) repo = make_repo_from_file("ActualSystemRepo");
else repo = make_repo_from_file(string(argv[2]));
if(!repo->has_package(string(argv[1]))) {
if((pkg = repo->package(string(argv[1]))) == nullptr) {
std::cout << "No package \"" << argv[1] << "\" found." << std::endl;
delete repo;
return EXIT_FAILURE;
}
Package pkg = repo->package(string(argv[1]));
std::cout << "Details for \"" << pkg->pkgname() << "\" (version " << pkg->pkgver().version() << "):" << std::endl;
std::cout << "Details for \"" << pkg.pkgname() << "\":" << std::endl;
std::cout << "\tDescription: " << pkg->pkgdesc() << std::endl;
std::cout << "\tArchitecture: " << pkg->arch() << std::endl;
std::cout << "\tLicense: " << pkg->license() << std::endl;
std::cout << "\tWeb Site: " << pkg->url() << std::endl;
std::cout << "\tMaintained by: " << pkg->maintainer() << std::endl;
std::cout << "\tSize when installed: " << pkg->size() << " bytes" << std::endl;
std::cout << "\tDepends on:\n" << std::endl;
for(auto dep : pkg->depends()) std::cout << "\t\t" << dep << std::endl;
std::cout << "\tDescription: " << pkg.pkgdesc() << std::endl;
std::cout << "\tArchitecture: " << pkg.arch() << std::endl;
std::cout << "\tLicense: " << pkg.license() << std::endl;
std::cout << "\tWeb Site: " << pkg.url() << std::endl;
std::cout << "\tMaintained by: " << pkg.maintainer() << std::endl;
std::cout << "\tSize when installed: " << pkg.size() << " bytes" << std::endl;
delete repo;
return EXIT_SUCCESS;
}