Add little installedmsg utility for printing APK-style messages

parent 1b2805c4
.POSIX:
all: packageversion repoquery repository
all: installedmsg packageversion repoquery repository
packageversion: packageversion.cc
installedmsg: installedmsg.cc ../libparcel.so.0
@printf "\tCXX\tinstalledmsg\n"
@$(CXX) $(CXXFLAGS) -o installedmsg installedmsg.cc -L.. -lparcel
packageversion: packageversion.cc ../libparcel.so.0
@printf "\tCXX\tpackageversion\n"
@$(CXX) $(CXXFLAGS) -o packageversion packageversion.cc -L.. -lparcel
repoquery: repoquery.cc
repoquery: repoquery.cc ../libparcel.so.0
@printf "\tCXX\trepoquery\n"
@$(CXX) $(CXXFLAGS) -o repoquery repoquery.cc -L.. -Wl,-rpath .. -lparcel
repository: repository.cc
repository: repository.cc ../libparcel.so.0
@printf "\tCXX\trepository\n"
@$(CXX) $(CXXFLAGS) -o repository repository.cc -L.. -Wl,-rpath .. -lparcel
clean:
@-rm installedmsg 2>/dev/null
@-rm packageversion 2>/dev/null
@-rm repoquery 2>/dev/null
@-rm repository 2>/dev/null
......
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include "../Repository.hh"
using namespace Parcel;
Repository *make_repo_from_file(string filename) {
std::ifstream my_file(filename);
if(!my_file) return nullptr;
return new Repository(my_file);
}
static const char prefixes[7] = {' ', 'K', 'M', 'G', 'T' , 'E', 'Z'};
int main(void) {
uint64_t size = 0;
char prefix = 0;
string prefix_str;
Repository *test_repo = make_repo_from_file("/lib/apk/db/installed");
if(test_repo == nullptr) {
std::cerr << "Ack! Can't load installed packages." << std::endl;
return EXIT_FAILURE;
}
for(auto &pkg : test_repo->packages()) {
size += pkg->size();
}
/* Wow, this is ugly */
while(size > 10240) {
size /= 1024;
prefix++;
}
while(prefix > sizeof(prefixes)) {
size *= 1024;
prefix--;
}
if(prefix == 0) prefix_str = "bytes";
else prefix_str = string("") + prefixes[prefix] + "iB";
std::cout << "OK: " << size << " " << prefix_str << " in " << test_repo->package_count() << " packages" << std::endl;
delete test_repo;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment