aboutsummaryrefslogtreecommitdiff
path: root/doc/api/cplusplus/examples/example_dep_spec.cc
blob: 9be91eb5d9878da8a6e4809ffc1ccc1344dcce98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/** \file
 *
 * Example \ref example_dep_spec.cc "example_dep_spec.cc" .
 *
 * \ingroup g_dep_spec
 */

/** \example example_dep_spec.cc
 *
 * This example demonstrates how to handle dependency specs.
 *
 * See \ref example_dep_label.cc "example_dep_label.cc" for labels.
 * See \ref example_dep_tree.cc "example_dep_tree.cc" for trees.
 */

#include <paludis/paludis.hh>
#include "example_command_line.hh"
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <map>
#include <sstream>

using namespace paludis;
using namespace examples;

using std::cout;
using std::endl;
using std::setw;
using std::left;

namespace
{
    /* Used to print out more information about a UseRequirement. */
    struct UseRequirementPrinter :
        ConstVisitor<UseRequirementVisitorTypes>
    {
        std::ostringstream s;

        void visit(const EnabledUseRequirement & r)
        {
            s << "[" << r.flag() << "]";
        }

        void visit(const DisabledUseRequirement & r)
        {
            s << "[!" << r.flag() << "]";
        }

        void visit(const EqualUseRequirement & r)
        {
            s << "[" << r.flag() << "?] (using '" << *r.package_id() << "')";
        }

        void visit(const NotEqualUseRequirement & r)
        {
            s << "[!" << r.flag() << "?] (using '" << *r.package_id() << "')";
        }
    };
}

int main(int argc, char * argv[])
{
    try
    {
        CommandLine::get_instance()->run(argc, argv,
                "example_dep_spec", "EXAMPLE_DEP_SPEC_OPTIONS", "EXAMPLE_DEP_SPEC_CMDLINE");

        /* We start with an Environment, respecting the user's '--environment' choice. */
        tr1::shared_ptr<Environment> env(EnvironmentMaker::get_instance()->make_from_spec(
                    CommandLine::get_instance()->a_environment.argument()));

        /* For each command line parameter... */
        for (CommandLine::ParametersConstIterator q(CommandLine::get_instance()->begin_parameters()),
                q_end(CommandLine::get_instance()->end_parameters()) ; q != q_end ; ++q)
        {
            /* Create a PackageDepSpec from the parameter. For user-inputted
             * data, parse_user_package_dep_spec() should be used. If wildcards
             * are to be permitted, the updso_allow_wildcards option should be
             * included. If data about the spec is known at compile time,
             * make_package_dep_spec() should be used instead. */
            PackageDepSpec spec(parse_user_package_dep_spec(
                        *q, UserPackageDepSpecOptions() + updso_allow_wildcards));

            /* Display information about the PackageDepSpec. */
            cout << "Information about '" << spec << "':" << endl;

            if (spec.package_ptr())
                cout << "    " << left << setw(24) << "Package:" << " " << *spec.package_ptr() << endl;

            if (spec.category_name_part_ptr())
                cout << "    " << left << setw(24) << "Category part:" << " " << *spec.category_name_part_ptr() << endl;

            if (spec.package_name_part_ptr())
                cout << "    " << left << setw(24) << "Package part:" << " " << *spec.package_name_part_ptr() << endl;

            if (spec.version_requirements_ptr() && ! spec.version_requirements_ptr()->empty())
            {
                cout << "    " << left << setw(24) << "Version requirements:" << " ";
                bool need_join(false);
                for (VersionRequirements::ConstIterator r(spec.version_requirements_ptr()->begin()),
                        r_end(spec.version_requirements_ptr()->end()) ; r != r_end ; ++r)
                {
                    if (need_join)
                    {
                        switch (spec.version_requirements_mode())
                        {
                            case vr_and:
                                cout << " and ";
                                break;

                            case vr_or:
                                cout << " or ";
                                break;

                            case last_vr:
                                throw InternalError(PALUDIS_HERE, "Bad version_requirements_mode");
                        }
                    }

                    cout << r->version_operator << r->version_spec;
                    need_join = true;
                }
                cout << endl;
            }

            if (spec.slot_ptr())
                cout << "    " << left << setw(24) << "Slot:" << " " << *spec.slot_ptr() << endl;

            if (spec.repository_ptr())
                cout << "    " << left << setw(24) << "Repository:" << " " << *spec.repository_ptr() << endl;

            if (spec.use_requirements_ptr() && ! spec.use_requirements_ptr()->empty())
            {
                cout << "    " << left << setw(24) << "Use requirements:" << " ";
                bool need_join(false);
                for (UseRequirements::ConstIterator u(spec.use_requirements_ptr()->begin()),
                        u_end(spec.use_requirements_ptr()->end()) ; u != u_end ; ++u)
                {
                    if (need_join)
                        cout << " and ";

                    /* A UseRequirement could be one of various subclasses. We
                     * use a visitor to do the right thing. */
                    UseRequirementPrinter p;
                    (*u)->accept(p);
                    cout << p.s.str();
                    need_join = true;
                }
                cout << endl;
            }

            /* And display packages matching that spec */
            cout << "    " << left << setw(24) << "Matches:" << " ";
            tr1::shared_ptr<const PackageIDSequence> ids(
                    env->package_database()->query(query::Matches(spec), qo_order_by_version));
            bool need_indent(false);
            for (PackageIDSequence::ConstIterator i(ids->begin()), i_end(ids->end()) ;
                    i != i_end ; ++i)
            {
                if (need_indent)
                    cout << "    " << left << setw(24) << "" << " ";
                cout << **i << endl;
                need_indent = true;
            }

            cout << endl;
        }
    }
    catch (const Exception & e)
    {
        /* Paludis exceptions can provide a handy human-readable backtrace and
         * an explanation message. Where possible, these should be displayed. */
        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_SUCCESS;
}