File: pkg-list.cpp

package info (click to toggle)
packagekit 1.1.5-2%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 15,464 kB
  • sloc: ansic: 56,926; cpp: 8,658; python: 7,641; xml: 6,388; sh: 4,517; perl: 1,738; makefile: 1,437; ruby: 1,341
file content (91 lines) | stat: -rw-r--r-- 2,933 bytes parent folder | download | duplicates (3)
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
/* pkg-list.cpp
 *
 * Copyright (c) 2012-2016 Daniel Nicoletti <dantti12@gmail.com>
 *
 * 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.
 */

#include "pkg-list.h"

#include <algorithm>

// compare...uses the candidate version of each package.
class compare
{
public:
    compare() {}
    
    bool operator()(const pkgCache::VerIterator &a,
                    const pkgCache::VerIterator &b) {
        int ret = strcmp(a.ParentPkg().Name(), b.ParentPkg().Name());
        if (ret == 0) {
            ret = strcmp(a.VerStr(), b.VerStr());
            if (ret == 0) {
                ret = strcmp(a.Arch(), b.Arch());
                if (ret == 0) {
                    pkgCache::VerFileIterator aVF = a.FileList();
                    pkgCache::VerFileIterator bVF = b.FileList();
                    ret = strcmp(aVF.File().Archive() == NULL ? "" : aVF.File().Archive(),
                                 bVF.File().Archive() == NULL ? "" : bVF.File().Archive());
                }
            }
        }
        return ret < 0;
    }
};

/** \brief operator== for match results. */
class result_equality
{
public:
    result_equality() {}
    
    bool operator() (const pkgCache::VerIterator &a, const pkgCache::VerIterator &b) {
        bool ret;
        ret = strcmp(a.ParentPkg().Name(), b.ParentPkg().Name()) == 0 &&
                strcmp(a.VerStr(), b.VerStr()) == 0 &&
                strcmp(a.Arch(), b.Arch()) == 0;
        if (ret) {
            pkgCache::VerFileIterator aVF = a.FileList();
            pkgCache::VerFileIterator bVF = b.FileList();
            ret = strcmp(aVF.File().Archive() == NULL ? "" : aVF.File().Archive(),
                         bVF.File().Archive() == NULL ? "" : bVF.File().Archive()) == 0;
        }
        return ret;
    }
};

bool PkgList::contains(const pkgCache::PkgIterator &pkg)
{
    for (const pkgCache::VerIterator &ver : *this) {
        if (ver.ParentPkg() == pkg) {
            return true;
        }
    }
    return false;
}

void PkgList::sort()
{
    // Sort so we can remove the duplicated entries
    std::sort(begin(), end(), compare());
}

void PkgList::removeDuplicates()
{
    // Remove the duplicated entries
    erase(unique(begin(), end(), result_equality()), end());
}