summaryrefslogtreecommitdiff
path: root/src/generic/apt/tags.h
blob: d4d5171c6ba2207c453a1b2df9b844fd336bc15f (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
// tags.h                                            -*-c++-*-
//
//   Copyright (C) 2005, 2007, 2010 Daniel Burrows
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of
//   the License, or (at your option) any later version.
//
//   This program 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; see the file COPYING.  If not, write to
//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//   Boston, MA 02111-1307, USA.
//

#ifndef TAGS_H
#define TAGS_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// If ept is unavailable, we use our own (broken!) code to build an
// in-memory database of package tags.  Otherwise, this code just
// handles initializing it, destroying it, and extracting information
// from it.  Note that this means that all callers have to be
// conditionalized on HAVE_EPT: the "tag" class this used to return is
// broken wrt hierarchies and just using ept is simpler.

#ifndef HAVE_EPT

#include <set>
#include <string>

#include <apt-pkg/pkgcache.h>

/** \brief A parser for tags.
 * 
 *  \file tags.h
 */

class OpProgress;

class tag
{
  std::string s;

  int cmp(const tag &other) const;
public:
  class const_iterator
  {
    std::string::const_iterator start, finish, limit;

    friend class tag;
  public:
    const_iterator(const std::string::const_iterator &_start,
		   const std::string::const_iterator &_finish,
		   const std::string::const_iterator &_limit)
      :start(_start), finish(_finish), limit(_limit)
    {
    }

    const_iterator &operator++();

    const_iterator &operator=(const const_iterator &other)
    {
      start = other.start;
      finish = other.finish;
      limit = other.limit;

      return *this;
    }

    bool operator==(const const_iterator &other) const
    {
      return start == other.start && finish == other.finish && limit == other.limit;
    }

    bool operator!=(const const_iterator &other) const
    {
      return start != other.start || finish != other.finish || limit != other.limit;
    }

    std::string operator*() const
    {
      return std::string(start, finish);
    }
  };

  tag(std::string::const_iterator _start,
      std::string::const_iterator _finish);

  tag &operator=(const tag &other)
  {
    s = other.s;

    return *this;
  }

  bool operator<(const tag &other) const
  {
    return cmp(other) < 0;
  }

  bool operator<=(const tag &other) const
  {
    return cmp(other) <= 0;
  }

  bool operator==(const tag &other) const
  {
    return cmp(other) == 0;
  }

  bool operator!=(const tag &other) const
  {
    return cmp(other) != 0;
  }

  bool operator>(const tag &other) const
  {
    return cmp(other) > 0;
  }

  bool operator>=(const tag &other) const
  {
    return cmp(other) >= 0;
  }

  const_iterator begin() const;
  const_iterator end() const
  {
    return const_iterator(s.end(), s.end(), s.end());
  }

  std::string str() const
  {
    return s;
  }
};

class tag_list
{
  // The string to parse.
  std::string s;
public:
  class const_iterator
  {
    std::string::const_iterator start, finish, limit;
  public:
    const_iterator(const std::string::const_iterator &_start,
		   const std::string::const_iterator &_finish,
		   const std::string::const_iterator &_limit)
      :start(_start), finish(_finish), limit(_limit)
    {
    }

    const_iterator operator=(const const_iterator &other)
    {
      start = other.start;
      finish = other.finish;
      limit = other.limit;

      return *this;
    }

    bool operator==(const const_iterator &other)
    {
      return other.start == start && other.finish == finish && other.limit == limit;
    }

    bool operator!=(const const_iterator &other)
    {
      return other.start != start || other.finish != finish || other.limit != limit;
    }

    const_iterator &operator++();

    tag operator*()
    {
      return tag(start, finish);
    }
  };

  tag_list(const char *start, const char *finish)
    :s(start, finish)
  {
  }

  tag_list &operator=(const tag_list &other)
  {
    s=other.s;

    return *this;
  }

  const_iterator begin() const;
  const_iterator end() const
  {
    return const_iterator(s.end(), s.end(), s.end());
  }
};

// Grab the tags for the given package:
const std::set<tag> *get_tags(const pkgCache::PkgIterator &pkg);

// Load tags for all packages (call before get_tags)
void load_tags(OpProgress &progress);



// Interface to the tag vocabulary file; tag vocabularies are assumed
// to not change over time.
std::string facet_description(const std::string &facet);

// Here "Tag" is a fully qualified tag name.
std::string tag_description(const std::string &tag);

#else // HAVE_EPT

#include <apt-pkg/pkgcache.h>

#include <ept/debtags/debtags.h>

#include <set>

namespace aptitude
{
  namespace apt
  {
#ifdef HAVE_EPT_DEBTAGS_TAG
    typedef ept::debtags::Tag tag;
    inline std::string get_fullname(const tag &t)
    {
      return t.fullname();
    }
#else
#ifdef EPT_DEBTAGS_GETTAGSOFITEM_RETURNS_STRINGS
    typedef std::string tag;
    inline std::string get_fullname(const std::string &t)
    {
      return t;
    }
#else
    // Probably means a new version of libept does something the
    // configure checks can't recognize.
#error "Don't know how to represent a debtags tag."
#endif
#endif

    const std::set<tag> get_tags(const pkgCache::PkgIterator &pkg);

    /** \brief Initialize the cache of debtags information. */
    void load_tags();

    /** \brief Get the name of the facet corresponding to a tag. */
    std::string get_facet_name(const tag &t);

    /** \brief Get the name of a tag (the full name minus the facet). */
    std::string get_tag_name(const tag &t);

    /** \brief Get the short description of a tag. */
    std::string get_tag_short_description(const tag &t);

    /** \brief Get the long description of a tag. */
    std::string get_tag_long_description(const tag &t);

    // \note This interface could be more efficient if it just used
    // facet names like libept does.  Using tags is a concession to
    // backwards compatibility (it's hard to implement one interface
    // that covers both cases without a lot of cruft).  In any event,
    // this shouldn't be called enough to matter.

    /** \brief Get the short description of the facet corresponding to a tag. */
    std::string get_facet_short_description(const tag &t);

    /** \brief Get the long description of the facet corresponding to a tag. */
    std::string get_facet_long_description(const tag &t);
  }
}

#endif // HAVE_EPT

#endif