]> cygwin.com Git - cygwin-apps/setup.git/blob - PackageSpecification.cc
56a4d2b93c623f92f59cc9a5abcbb0cef73179a4
[cygwin-apps/setup.git] / PackageSpecification.cc
1 /*
2 * Copyright (c) 2002, Robert Collins.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by Robert Collins <rbtcollins@hotmail.com>
13 *
14 */
15
16 #if 0
17 static const char *cvsid = "\n%%% $Id$\n";
18 #endif
19
20 #include "PackageSpecification.h"
21 #include "package_version.h"
22
23 PackageSpecification::PackageSpecification (String const &packageName)
24 : _packageName (packageName) , _operator (0), _version ()
25 {
26 }
27
28 String const&
29 PackageSpecification::packageName () const
30 {
31 return _packageName;
32 }
33
34 void
35 PackageSpecification::setOperator (_operators const &anOperator)
36 {
37 _operator = &anOperator;
38 }
39
40 void
41 PackageSpecification::setVersion (String const &aVersion)
42 {
43 _version = aVersion;
44 }
45
46 bool
47 PackageSpecification::satisfies (packageversion const &aPackage) const
48 {
49 if (_packageName.casecompare (aPackage.Name()) != 0)
50 return false;
51 if (_operator && _version.size()
52 && !_operator->satisfies (aPackage.Canonical_version (), _version))
53 return false;
54 return true;
55 }
56
57 String
58 PackageSpecification::serialise () const
59 {
60 return _packageName;
61 }
62
63 PackageSpecification &
64 PackageSpecification::operator= (PackageSpecification const &rhs)
65 {
66 _packageName = rhs._packageName;
67 return *this;
68 }
69
70 std::ostream &
71 operator << (std::ostream &os, PackageSpecification const &spec)
72 {
73 os << spec._packageName;
74 if (spec._operator)
75 os << " " << spec._operator->caption() << " " << spec._version;
76 return os;
77 }
78
79 const PackageSpecification::_operators PackageSpecification::Equals(0);
80 const PackageSpecification::_operators PackageSpecification::LessThan(1);
81 const PackageSpecification::_operators PackageSpecification::MoreThan(2);
82 const PackageSpecification::_operators PackageSpecification::LessThanEquals(3);
83 const PackageSpecification::_operators PackageSpecification::MoreThanEquals(4);
84
85 char const *
86 PackageSpecification::_operators::caption () const
87 {
88 switch (_value)
89 {
90 case 0:
91 return "==";
92 case 1:
93 return "<";
94 case 2:
95 return ">";
96 case 3:
97 return "<=";
98 case 4:
99 return ">=";
100 }
101 // Pacify GCC: (all case options are checked above)
102 return "Unknown operator";
103 }
104
105 bool
106 PackageSpecification::_operators::satisfies (String const &lhs, String const &rhs) const
107 {
108 switch (_value)
109 {
110 case 0:
111 return lhs.casecompare (rhs) == 0;
112 case 1:
113 return lhs.casecompare (rhs) < 0;
114 case 2:
115 return lhs.casecompare (rhs) > 0;
116 case 3:
117 return lhs.casecompare (rhs) <= 0;
118 case 4:
119 return lhs.casecompare (rhs) >= 0;
120 }
121 return false;
122 }
This page took 0.041592 seconds and 5 git commands to generate.