BPicture: Fix archive constructor.
[haiku.git] / src / kits / package / PackageResolvableExpression.cpp
blob086479d1d6d5272fd4cb2fdf624048f2c8a3f7c7
1 /*
2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
3 * Distributed under the terms of the MIT License.
4 */
7 #include <package/PackageResolvableExpression.h>
9 #include <package/hpkg/PackageInfoAttributeValue.h>
10 #include <package/PackageInfo.h>
11 #include <package/PackageResolvable.h>
14 namespace BPackageKit {
17 const char*
18 BPackageResolvableExpression
19 ::kOperatorNames[B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT] = {
20 "<",
21 "<=",
22 "==",
23 "!=",
24 ">=",
25 ">",
29 BPackageResolvableExpression::BPackageResolvableExpression()
31 fOperator(B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT)
36 BPackageResolvableExpression::BPackageResolvableExpression(
37 const BPackageResolvableExpressionData& data)
39 fName(data.name),
40 fOperator(data.op),
41 fVersion(data.version)
43 fName.ToLower();
47 BPackageResolvableExpression::BPackageResolvableExpression(const BString& name,
48 BPackageResolvableOperator _operator, const BPackageVersion& version)
50 fName(name),
51 fOperator(_operator),
52 fVersion(version)
54 fName.ToLower();
58 BPackageResolvableExpression::BPackageResolvableExpression(
59 const BString& expressionString)
61 fName(),
62 fOperator(B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT),
63 fVersion()
65 SetTo(expressionString);
69 status_t
70 BPackageResolvableExpression::InitCheck() const
72 if (fName.Length() == 0)
73 return B_NO_INIT;
75 // either both or none of operator and version must be set
76 if ((fOperator >= 0 && fOperator < B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT)
77 != (fVersion.InitCheck() == B_OK))
78 return B_NO_INIT;
80 return B_OK;
84 const BString&
85 BPackageResolvableExpression::Name() const
87 return fName;
91 BPackageResolvableOperator
92 BPackageResolvableExpression::Operator() const
94 return fOperator;
98 const BPackageVersion&
99 BPackageResolvableExpression::Version() const
101 return fVersion;
105 BString
106 BPackageResolvableExpression::ToString() const
108 BString string = fName;
110 if (fVersion.InitCheck() == B_OK)
111 string << kOperatorNames[fOperator] << fVersion.ToString();
113 return string;
117 status_t
118 BPackageResolvableExpression::SetTo(const BString& expressionString)
120 fName.Truncate(0);
121 fOperator = B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT;
122 fVersion.Clear();
124 return BPackageInfo::ParseResolvableExpressionString(expressionString,
125 *this);
129 void
130 BPackageResolvableExpression::SetTo(const BString& name,
131 BPackageResolvableOperator _operator, const BPackageVersion& version)
133 fName = name;
134 fOperator = _operator;
135 fVersion = version;
137 fName.ToLower();
141 void
142 BPackageResolvableExpression::Clear()
144 fName.Truncate(0);
145 fOperator = B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT;
146 fVersion.Clear();
150 bool
151 BPackageResolvableExpression::Matches(const BPackageVersion& version,
152 const BPackageVersion& compatibleVersion) const
154 // If no particular version is required, we always match.
155 if (fVersion.InitCheck() != B_OK)
156 return true;
158 if (version.InitCheck() != B_OK)
159 return false;
161 int compare = version.Compare(fVersion);
162 bool matches = false;
163 switch (fOperator) {
164 case B_PACKAGE_RESOLVABLE_OP_LESS:
165 matches = compare < 0;
166 break;
167 case B_PACKAGE_RESOLVABLE_OP_LESS_EQUAL:
168 matches = compare <= 0;
169 break;
170 case B_PACKAGE_RESOLVABLE_OP_EQUAL:
171 matches = compare == 0;
172 break;
173 case B_PACKAGE_RESOLVABLE_OP_NOT_EQUAL:
174 matches = compare != 0;
175 break;
176 case B_PACKAGE_RESOLVABLE_OP_GREATER_EQUAL:
177 matches = compare >= 0;
178 break;
179 case B_PACKAGE_RESOLVABLE_OP_GREATER:
180 matches = compare > 0;
181 break;
182 default:
183 break;
185 if (!matches)
186 return false;
188 // Check compatible version. If not specified, the match must be exact.
189 // Otherwise fVersion must be >= compatibleVersion.
190 if (compatibleVersion.InitCheck() != B_OK)
191 return compare == 0;
193 // Since compatibleVersion <= version, we can save the comparison, if
194 // version <= fVersion.
195 return compare <= 0 || compatibleVersion.Compare(fVersion) <= 0;
199 bool
200 BPackageResolvableExpression::Matches(const BPackageResolvable& provides) const
202 if (provides.Name() != fName)
203 return false;
205 return Matches(provides.Version(), provides.CompatibleVersion());
209 } // namespace BPackageKit