2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
3 * Distributed under the terms of the MIT License.
7 #include <package/PackageResolvableExpression.h>
9 #include <package/hpkg/PackageInfoAttributeValue.h>
10 #include <package/PackageInfo.h>
11 #include <package/PackageResolvable.h>
14 namespace BPackageKit
{
18 BPackageResolvableExpression
19 ::kOperatorNames
[B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT
] = {
29 BPackageResolvableExpression::BPackageResolvableExpression()
31 fOperator(B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT
)
36 BPackageResolvableExpression::BPackageResolvableExpression(
37 const BPackageResolvableExpressionData
& data
)
41 fVersion(data
.version
)
47 BPackageResolvableExpression::BPackageResolvableExpression(const BString
& name
,
48 BPackageResolvableOperator _operator
, const BPackageVersion
& version
)
58 BPackageResolvableExpression::BPackageResolvableExpression(
59 const BString
& expressionString
)
62 fOperator(B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT
),
65 SetTo(expressionString
);
70 BPackageResolvableExpression::InitCheck() const
72 if (fName
.Length() == 0)
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
))
85 BPackageResolvableExpression::Name() const
91 BPackageResolvableOperator
92 BPackageResolvableExpression::Operator() const
98 const BPackageVersion
&
99 BPackageResolvableExpression::Version() const
106 BPackageResolvableExpression::ToString() const
108 BString string
= fName
;
110 if (fVersion
.InitCheck() == B_OK
)
111 string
<< kOperatorNames
[fOperator
] << fVersion
.ToString();
118 BPackageResolvableExpression::SetTo(const BString
& expressionString
)
121 fOperator
= B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT
;
124 return BPackageInfo::ParseResolvableExpressionString(expressionString
,
130 BPackageResolvableExpression::SetTo(const BString
& name
,
131 BPackageResolvableOperator _operator
, const BPackageVersion
& version
)
134 fOperator
= _operator
;
142 BPackageResolvableExpression::Clear()
145 fOperator
= B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT
;
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
)
158 if (version
.InitCheck() != B_OK
)
161 int compare
= version
.Compare(fVersion
);
162 bool matches
= false;
164 case B_PACKAGE_RESOLVABLE_OP_LESS
:
165 matches
= compare
< 0;
167 case B_PACKAGE_RESOLVABLE_OP_LESS_EQUAL
:
168 matches
= compare
<= 0;
170 case B_PACKAGE_RESOLVABLE_OP_EQUAL
:
171 matches
= compare
== 0;
173 case B_PACKAGE_RESOLVABLE_OP_NOT_EQUAL
:
174 matches
= compare
!= 0;
176 case B_PACKAGE_RESOLVABLE_OP_GREATER_EQUAL
:
177 matches
= compare
>= 0;
179 case B_PACKAGE_RESOLVABLE_OP_GREATER
:
180 matches
= compare
> 0;
188 // Check compatible version. If not specified, the match must be exact.
189 // Otherwise fVersion must be >= compatibleVersion.
190 if (compatibleVersion
.InitCheck() != B_OK
)
193 // Since compatibleVersion <= version, we can save the comparison, if
194 // version <= fVersion.
195 return compare
<= 0 || compatibleVersion
.Compare(fVersion
) <= 0;
200 BPackageResolvableExpression::Matches(const BPackageResolvable
& provides
) const
202 if (provides
.Name() != fName
)
205 return Matches(provides
.Version(), provides
.CompatibleVersion());
209 } // namespace BPackageKit