aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/gemcutter/gemcutter_uri_key.cc
blob: acb7f6553f0bd1deab7bb1c1b3dd1ea55a1f2217 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2010, 2011 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/repositories/gemcutter/gemcutter_uri_key.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/spec_tree.hh>
#include <paludis/dep_spec.hh>
#include <paludis/pretty_printer.hh>
#include <sstream>
#include <algorithm>

using namespace paludis;
using namespace paludis::gemcutter_repository;

namespace
{
    std::shared_ptr<SimpleURISpecTree> parse(const std::string & s)
    {
        auto result(std::make_shared<SimpleURISpecTree>(std::make_shared<AllDepSpec>()));
        result->top()->append(std::make_shared<SimpleURIDepSpec>(s));
        return result;
    }

    struct ValuePrinter
    {
        std::stringstream s;
        const PrettyPrinter & printer;
        const PrettyPrintOptions options;

        const unsigned indent;
        const bool flat;
        bool need_space;

        ValuePrinter(
                const PrettyPrinter & p,
                const PrettyPrintOptions & o) :
            printer(p),
            options(o),
            indent(0),
            flat(! options[ppo_multiline_allowed]),
            need_space(false)
        {
        }

        void visit(const SimpleURISpecTree::NodeType<SimpleURIDepSpec>::Type & node)
        {
            if (! flat)
                s << printer.indentify(indent + 1);
            else if (need_space)
                s << " ";
            else
                need_space = true;

            s << printer.prettify(*node.spec());

            if (! flat)
                s << printer.newline();
        }

        void visit(const SimpleURISpecTree::NodeType<AllDepSpec>::Type & node)
        {
            std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));
        }

        void visit(const SimpleURISpecTree::NodeType<ConditionalDepSpec>::Type & node)
        {
            std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));
        }
    };
}

namespace paludis
{
    template <>
    struct Imp<GemcutterURIKey>
    {
        const std::string * const raw_name;
        const std::string * const human_name;
        const MetadataKeyType type;
        const std::string value_string;
        const std::shared_ptr<SimpleURISpecTree> value;

        Imp(
                const std::string * const r,
                const std::string * const h,
                const MetadataKeyType t,
                const std::string & v) :
            raw_name(r),
            human_name(h),
            type(t),
            value_string(v),
            value(parse(v))
        {
        }
    };
}

GemcutterURIKey::GemcutterURIKey(const std::string * const r, const std::string * const h,
        const MetadataKeyType t, const std::string & v) :
    _imp(r, h, t, v)
{
}

GemcutterURIKey::~GemcutterURIKey() = default;

const std::shared_ptr<const SimpleURISpecTree>
GemcutterURIKey::parse_value() const
{
    return _imp->value;
}

const std::string
GemcutterURIKey::raw_name() const
{
    return *_imp->raw_name;
}

const std::string
GemcutterURIKey::human_name() const
{
    return *_imp->human_name;
}

MetadataKeyType
GemcutterURIKey::type() const
{
    return _imp->type;
}

const std::string
GemcutterURIKey::pretty_print_value(
        const PrettyPrinter & printer, const PrettyPrintOptions & options) const
{
    ValuePrinter p{printer, options};
    _imp->value->top()->accept(p);
    return p.s.str();
}

namespace paludis
{
    template class Pimp<GemcutterURIKey>;
}