...
 
Commits (3)
......@@ -9,7 +9,7 @@ libparcel.a: Package.o Repository.o
libparcel.so: Package.o Repository.o
$(CXX) $(CXXFLAGS) $(LDFLAGS) -shared -o libparcel.so.0 Package.o Repository.o
ln -s libparcel.so.0 libparcel.so
-ln -s libparcel.so.0 libparcel.so
Package.o: Package.cc Package.hh
$(CXX) $(CXXFLAGS) -c -o Package.o Package.cc
......
#include <cassert>
#include <iostream>
#include "Package.hh"
namespace Parcel {
......@@ -11,6 +12,60 @@ bool operator<(const PackageVersion &left, const PackageVersion &right)
Package::Package(std::istream& from_pkginfo)
{
// We can't blindly trust this input - we could be called from something
// other than the Repository ctor.
char nextline[8192];
while(from_pkginfo.getline(nextline, 8192)) {
string raw_value;
if(nextline[0] == '\0') {
// Skip blank lines
continue;
}
if(nextline[1] == '\0' || nextline[1] != ':') {
std::cerr << "Package: invalid descriptor: " << nextline << std::endl;
}
raw_value = string(nextline + 2);
switch(nextline[0]) {
case 'P':
// Package name
_name = raw_value;
break;
case 'V':
_version = raw_value;
break;
case 'T':
_description = raw_value;
break;
case 'U':
_url = raw_value;
break;
case 'I':
_install_size = std::stoul(raw_value);
break;
case 'L':
_license = raw_value;
break;
case 'A':
_arch = raw_value;
break;
case 'm':
_maintainer = raw_value;
break;
case 'c':
_commit_id = raw_value;
break;
case 'o':
_origin = raw_value;
break;
}
}
if(from_pkginfo.fail() && !from_pkginfo.eof()) {
std::cerr << "I/O FAILURE parsing package information" << std::endl;
}
}
}
#include "Repository.hh"
#include <algorithm>
#include <iostream>
#include <sstream>
namespace Parcel {
using std::vector;
using std::find_if;
Repository::Repository(std::istream& from_index) {
string nextpkg;
// libreoffice-common p: line is current record with 4241 bytes
char nextline[8192];
while(from_index.getline(nextline, 8192)) {
if(nextline[0] == '\0') {
// Spurious empty line at beginning / end of index?
......@@ -30,4 +35,19 @@ Repository::Repository(std::istream& from_index) {
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;
});
}
bool Repository::has_package(string name) {
return find_package_by_name(this->_packages, name) != this->_packages.end();
}
Package Repository::package(string name) {
return *find_package_by_name(this->_packages, name);
}
}
......@@ -14,6 +14,8 @@ public:
Repository(std::istream& from_index);
std::vector<Package> packages() { return _packages; }
bool has_package(string 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(); }
......
.POSIX:
all: packageversion repository
all: packageversion repoquery repository
packageversion: packageversion.cc
$(CXX) $(CXXFLAGS) -o packageversion packageversion.cc -L.. -lparcel
repoquery: repoquery.cc
$(CXX) $(CXXFLAGS) -o repoquery repoquery.cc -L.. -Wl,-rpath .. -lparcel
repository: repository.cc
$(CXX) $(CXXFLAGS) -o repository repository.cc -L.. -Wl,-rpath .. -lparcel
......
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include "../Repository.hh"
using namespace Parcel;
int usage() {
printf("repoquery pkgname [repofile]\n");
return EXIT_FAILURE;
}
Repository *make_repo_from_file(string filename) {
std::ifstream my_file(filename);
if(!my_file) return nullptr;
return new Repository(my_file);
}
int main(int argc, char* argv[])
{
Repository* repo;
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]))) {
std::cout << "No package \"" << argv[1] << "\" found." << std::endl;
return EXIT_FAILURE;
}
Package pkg = repo->package(string(argv[1]));
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;
return EXIT_SUCCESS;
}