example_match_package.cc
This example demonstrates how to use
paludis::match_package and
paludis::match_package_in_set.
#include <paludis/paludis.hh>
#include "example_command_line.hh"
#include <iostream>
#include <iomanip>
using namespace paludis;
using namespace examples;
using std::cout;
using std::endl;
using std::left;
using std::setw;
int main(int argc, char * argv[])
{
int exit_status(0);
try
{
CommandLine::get_instance()->run(argc, argv,
"example_match_package", "EXAMPLE_MATCH_PACKAGE_OPTIONS", "EXAMPLE_MATCH_PACKAGE_CMDLINE");
std::tr1::shared_ptr<Environment> env(EnvironmentMaker::get_instance()->make_from_spec(
CommandLine::get_instance()->a_environment.argument()));
std::tr1::shared_ptr<const PackageIDSequence> ids((*env)[selection::AllVersionsSorted(
generator::All() | filter::SupportsAction<InstalledAction>())]);
std::tr1::shared_ptr<const SetSpecTree::ConstItem> system(env->set(SetName("system"))),
world(env->set(SetName("world")));
for (PackageIDSet::ConstIterator i(ids->begin()), i_end(ids->end()) ;
i != i_end ; ++i)
{
if (match_package(*env, make_package_dep_spec().package(QualifiedPackageName("sys-apps/paludis")), **i))
cout << left << setw(50) << (stringify(**i) + ":") << " " << "paludis" << endl;
else if (match_package_in_set(*env, *system, **i))
cout << left << setw(50) << (stringify(**i) + ":") << " " << "system" << endl;
else if (match_package_in_set(*env, *world, **i))
cout << left << setw(50) << (stringify(**i) + ":") << " " << "world" << endl;
else
cout << left << setw(50) << (stringify(**i) + ":") << " " << "nothing" << endl;
}
}
catch (const Exception & e)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * " << e.backtrace("\n * ")
<< e.message() << " (" << e.what() << ")" << endl;
return EXIT_FAILURE;
}
catch (const std::exception & e)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * " << e.what() << endl;
return EXIT_FAILURE;
}
catch (...)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * Unknown exception type. Ouch..." << endl;
return EXIT_FAILURE;
}
return exit_status;
}