-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDwmDebPkgDepend.cc
155 lines (139 loc) · 5.22 KB
/
DwmDebPkgDepend.cc
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
//===========================================================================
// @(#) $DwmPath$
//===========================================================================
// Copyright (c) Daniel W. McRobb 2020
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. The names of the authors and copyright holders may not be used to
// endorse or promote products derived from this software without
// specific prior written permission.
//
// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
// TRADEMARK OR OTHER RIGHTS.
//===========================================================================
//---------------------------------------------------------------------------
//! \file DwmDebPkgDepend.cc
//! \author Daniel W. McRobb
//! \brief Dwm::Deb::PkgDepend class implementation
//---------------------------------------------------------------------------
#include <cstdio>
#include <regex>
#include "DwmDebPkgDepend.hh"
namespace Dwm {
namespace Deb {
using namespace std;
//------------------------------------------------------------------------
//!
//------------------------------------------------------------------------
const string & PkgDepend::Package(const string & package)
{
_pkg = package;
return _pkg;
}
//------------------------------------------------------------------------
//!
//------------------------------------------------------------------------
const string & PkgDepend::Operator(const string & op)
{
_operator = op;
return _operator;
}
//------------------------------------------------------------------------
//!
//------------------------------------------------------------------------
const PkgVersion & PkgDepend::Version(const PkgVersion & dpv)
{
_version = dpv;
return _version;
}
//------------------------------------------------------------------------
//!
//------------------------------------------------------------------------
PkgVersion PkgDepend::InstalledVersion(const string & pkg)
{
PkgVersion rc;
string cmd("dpkg -s ");
cmd += pkg;
FILE *cmdpipe = popen(cmd.c_str(), "r");
if (cmdpipe) {
regex rgx("^Version[:][ \\t]*([^ \\t\\n]+)",
regex::ECMAScript|regex::optimize);
smatch sm;
char line[4096];
while (fgets(line, 4096, cmdpipe) != NULL) {
string s(line);
if (regex_search(s, sm, rgx)) {
if (sm.size() == 2) {
rc.FromString(sm[1].str());
break;
}
}
}
pclose(cmdpipe);
}
return rc;
}
//------------------------------------------------------------------------
//!
//------------------------------------------------------------------------
bool PkgDepend::operator < (const PkgDepend & dpd) const
{
bool rc = false;
if (_pkg < dpd._pkg) {
rc = true;
}
else if (_pkg == dpd._pkg) {
if (_operator < dpd._operator) {
rc = true;
}
else if (_operator == dpd._operator) {
if (_version < dpd._version) {
rc = true;
}
}
}
return rc;
}
//------------------------------------------------------------------------
//!
//------------------------------------------------------------------------
bool PkgDepend::operator == (const PkgDepend & dpd) const
{
return ((_pkg == dpd._pkg)
&& (_operator == dpd._operator)
&& (_version == dpd._version));
}
//------------------------------------------------------------------------
//!
//------------------------------------------------------------------------
ostream & operator << (ostream & os, const PkgDepend & dep)
{
os << dep._pkg;
if (! dep._operator.empty()) {
os << " (" << dep._operator << ' ' << dep._version << ')';
}
return os;
}
} // namespace Deb
} // namespace Dwm