aboutsummaryrefslogtreecommitdiff
path: root/src/clients/cave/format_user_config.hh
blob: ec954761bf9cc0657d9cf270399fa0eb7068af86 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* 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
 */

#ifndef PALUDIS_GUARD_SRC_CLIENTS_CAVE_FORMAT_USER_CONFIG_HH
#define PALUDIS_GUARD_SRC_CLIENTS_CAVE_FORMAT_USER_CONFIG_HH 1

#include <paludis/util/map.hh>
#include <paludis/util/pimp.hh>
#include <paludis/util/singleton.hh>
#include <string>
#include <memory>
#include <utility>
#include "format_string.hh"
#include "colours.hh"

namespace paludis
{
    namespace cave
    {
        void set_want_colours(const bool v);

        class FormatUserConfigFile :
            public Singleton<FormatUserConfigFile>
        {
            friend class Singleton<FormatUserConfigFile>;

            private:
                Pimp<FormatUserConfigFile> _imp;

                FormatUserConfigFile();
                ~FormatUserConfigFile();

            public:
                std::string fetch(const std::string & v, int vi, const std::string & d) const;
        };

        template <char... cs_>
        struct CharList
        {
        };

        template <char... cs_>
        struct MinElement;

        template <char c_>
        struct MinElement<c_>
        {
            enum { value = c_ };
        };

        template <char c_, char... cs_>
        struct MinElement<c_, cs_...>
        {
            enum {
                cs_value = MinElement<cs_...>::value,
                c_value = c_,
                value = (cs_value < c_value ? cs_value : c_value)
            };
        };

        template <template <char...> class, char, typename>
        struct MakeFromCharAndCharList;

        template <template <char...> class Result_, char c_, char... cs_>
        struct MakeFromCharAndCharList<Result_, c_, CharList<cs_...> >
        {
            typedef Result_<c_, cs_...> Type;
        };

        template <template <char...> class, char... cs_>
        struct Sort;

        template <template <char...> class Result_>
        struct Sort<Result_>
        {
            typedef Result_<> Type;
        };

        template <template <char...> class Result_, char c_>
        struct Sort<Result_, c_>
        {
            typedef Result_<c_> Type;
        };

        template <char c_, typename Already_, typename Remainder_>
        struct Remove;

        template <char c_, char... ca_>
        struct Remove<c_, CharList<ca_...>, CharList<> >
        {
            typedef CharList<ca_...> Type;
        };

        template <char c_, char... ca_, char ch_, char... cb_>
        struct Remove<c_, CharList<ca_...>, CharList<ch_, cb_...> >
        {
            typedef typename std::conditional<(c_ == ch_),
                    typename Remove<c_, CharList<ca_...>, CharList<cb_...> >::Type,
                    typename Remove<c_, CharList<ca_..., ch_>, CharList<cb_...> >::Type
                >::type Type;
        };

        template <template <char...> class, typename>
        struct UnwrapSort;

        template <template <char...> class Result_, char... cs_>
        struct UnwrapSort<Result_, CharList<cs_...> >
        {
            typedef typename Sort<Result_, cs_...>::Type Type;
        };

        template <template <char...> class Result_, char c_, char... cs_>
        struct Sort<Result_, c_, cs_...>
        {
            typedef typename MakeFromCharAndCharList<
                Result_,
                MinElement<c_, cs_...>::value,
                typename UnwrapSort<CharList, typename Remove<MinElement<c_, cs_...>::value, CharList<>, CharList<c_, cs_...> >::Type>::Type
                >::Type Type;
        };

        template <char c_>
        struct FormatValue
        {
            std::string value;
        };

        template <char c_>
        FormatValue<c_> fv(const std::string & s)
        {
            return FormatValue<c_>{s};
        }

        template <char... cs_>
        struct FormatString
        {
            std::string format_string;
        };

        template <char c_>
        struct FormatValuesBase
        {
            std::string value;

            FormatValuesBase(const std::string & v) :
                value(v)
            {
            }
        };

        template <char... cs_>
        struct FormatValues :
            FormatValuesBase<cs_>...
        {
            template <char... c_>
            FormatValues(const FormatValue<c_> & ... c) :
                FormatValuesBase<c_>(c.value)...
            {
            }
        };

        struct OrderedFormatValuesBaseMap
        {
            std::shared_ptr<Map<char, std::string> > map;

            OrderedFormatValuesBaseMap() :
                map(std::make_shared<Map<char, std::string> >())
            {
            }
        };

        template <char c_>
        struct OrderedFormatValuesBase
        {
            OrderedFormatValuesBase(
                    const std::shared_ptr<Map<char, std::string> > & m,
                    const FormatValuesBase<c_> & v)
            {
                m->insert(c_, v.value);
            }
        };

        template <char... cs_>
        struct OrderedFormatValues :
            OrderedFormatValuesBaseMap,
            OrderedFormatValuesBase<cs_>...
        {
            template <char... c_>
            OrderedFormatValues(const FormatValues<c_...> & c) :
                OrderedFormatValuesBase<cs_>(this->map, c)...
            {
            }

            /* just to shut gcc up */
            OrderedFormatValues(const FormatValues<> &)
            {
            }
        };

        template <char... cs_>
        struct MakeOrderedFormatValues
        {
            typedef typename Sort<OrderedFormatValues, cs_...>::Type Type;
        };

        template <char... cs_>
        typename MakeOrderedFormatValues<cs_...>::Type
        order(const FormatValues<cs_...> & f)
        {
            return typename MakeOrderedFormatValues<cs_...>::Type(f);
        }

        template <typename T_>
        struct ExtractFormatValue;

        template <char c_>
        struct ExtractFormatValue<FormatValue<c_> >
        {
            static const char value = c_;
        };

        template <char... cs_>
        std::string fuc_parameters(const FormatString<cs_...> & s, OrderedFormatValues<cs_...> && p)
        {
            return format_string(s.format_string, p.map);
        }

        template <char... cs_, typename... T_>
        std::string fuc(const FormatString<cs_...> & f, const T_ & ... t)
        {
            return fuc_parameters(f, order(FormatValues<ExtractFormatValue<T_>::value...>{t...}));
        }

        template <char... cs_>
        struct MakeFormatStringFetcher
        {
            std::string user_key;
            int user_key_version;

            std::string text;

            FormatString<cs_...> operator() () const
            {
                return FormatString<cs_...>{FormatUserConfigFile::get_instance()->fetch(user_key, user_key_version, text)};
            }
        };

        inline MakeFormatStringFetcher<>
        make_format_string_fetcher(const std::string & u, const int v)
        {
            return MakeFormatStringFetcher<>{u, v, ""};
        }

        template <char... cs_>
        struct MakeMakeFormatStringFetcher
        {
            typedef typename Sort<MakeFormatStringFetcher, cs_...>::Type Type;
        };

        template <char c_, char... cs_>
        struct CharAlreadyInChars;

        template <char c_>
        struct CharAlreadyInChars<c_>
        {
            enum { value = false };
        };

        template <char c_, char h_, char... cs_>
        struct CharAlreadyInChars<c_, h_, cs_...>
        {
            enum { value = c_ == h_ || CharAlreadyInChars<h_, cs_...>::value };
        };

        template <bool b_, char c_, char... cs_>
        struct MakeDeduplicatedMakeFormatStringFetcherResult
        {
            typedef MakeFormatStringFetcher<cs_...> Type;
        };

        template <char c_, char... cs_>
        struct MakeDeduplicatedMakeFormatStringFetcherResult<false, c_, cs_...>
        {
            typedef typename MakeMakeFormatStringFetcher<c_, cs_...>::Type Type;
        };

        template <char c_, char... cs_>
        struct MakeDeduplicatedMakeFormatStringFetcher
        {
            typedef typename MakeDeduplicatedMakeFormatStringFetcherResult<
                CharAlreadyInChars<c_, cs_...>::value,
                c_, cs_...>::Type Type;
        };

        template <char c_>
        struct FormatParam
        {
        };

        template <char c_>
        FormatParam<c_> param()
        {
            return FormatParam<c_>();
        }

        template <char c_>
        struct FormatParamIf
        {
            std::string text;
        };

        template <char c_>
        FormatParamIf<c_> param_if()
        {
            return FormatParamIf<c_>{"%{if " + std::string(1, c_) + "}"};
        }

        template <char c_>
        FormatParamIf<c_> param_else()
        {
            return FormatParamIf<c_>{"%{else}"};
        }

        template <char c_>
        FormatParamIf<c_> param_endif()
        {
            return FormatParamIf<c_>{"%{endif}"};
        }

        template <char... cs_>
        MakeFormatStringFetcher<cs_...> operator<< (MakeFormatStringFetcher<cs_...> && f, const std::string & s)
        {
            MakeFormatStringFetcher<cs_...> result{std::move(f.user_key), f.user_key_version, std::move(f.text)};
            result.text.append(s);
            return result;
        }

        template <char... cs_>
        MakeFormatStringFetcher<cs_...> operator<< (MakeFormatStringFetcher<cs_...> && f, const Colour & c)
        {
            MakeFormatStringFetcher<cs_...> result{std::move(f.user_key), f.user_key_version, std::move(f.text)};
            result.text.append("%{colour " + c.name + "}");
            return result;
        }

        template <char c_, char... cs_>
        typename MakeDeduplicatedMakeFormatStringFetcher<c_, cs_...>::Type
        operator<< (MakeFormatStringFetcher<cs_...> && f, const FormatParam<c_> &)
        {
            typename MakeDeduplicatedMakeFormatStringFetcher<c_, cs_...>::Type result{
                std::move(f.user_key), f.user_key_version, std::move(f.text)};
            result.text.append("%");
            result.text.append(1, c_);
            return result;
        }

        template <char c_, char... cs_>
        typename MakeDeduplicatedMakeFormatStringFetcher<c_, cs_...>::Type
        operator<< (MakeFormatStringFetcher<cs_...> && f, const FormatParamIf<c_> & c)
        {
            typename MakeDeduplicatedMakeFormatStringFetcher<c_, cs_...>::Type result{
                std::move(f.user_key), f.user_key_version, std::move(f.text)};
            result.text.append(c.text);
            return result;
        }
    }

    extern template class Singleton<cave::FormatUserConfigFile>;
}

#endif