2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
3 * Distributed under the terms of the MIT License.
7 #include <package/PackageVersion.h>
9 #include <NaturalCompare.h>
11 #include <package/PackageInfo.h>
12 #include <package/hpkg/PackageInfoAttributeValue.h>
15 using BPrivate::NaturalCompare
;
18 namespace BPackageKit
{
21 BPackageVersion::BPackageVersion()
28 BPackageVersion::BPackageVersion(const BPackageVersionData
& data
)
30 SetTo(data
.major
, data
.minor
, data
.micro
, data
.preRelease
, data
.revision
);
34 BPackageVersion::BPackageVersion(const BString
& versionString
,
35 bool revisionIsOptional
)
37 SetTo(versionString
, revisionIsOptional
);
41 BPackageVersion::BPackageVersion(const BString
& major
, const BString
& minor
,
42 const BString
& micro
, const BString
& preRelease
, uint32 revision
)
44 SetTo(major
, minor
, micro
, preRelease
, revision
);
49 BPackageVersion::InitCheck() const
51 return fMajor
.Length() > 0 ? B_OK
: B_NO_INIT
;
56 BPackageVersion::Major() const
63 BPackageVersion::Minor() const
70 BPackageVersion::Micro() const
77 BPackageVersion::PreRelease() const
84 BPackageVersion::Revision() const
91 BPackageVersion::Compare(const BPackageVersion
& other
) const
93 int diff
= NaturalCompare(fMajor
.String(), other
.fMajor
.String());
97 diff
= NaturalCompare(fMinor
.String(), other
.fMinor
.String());
101 diff
= NaturalCompare(fMicro
.String(), other
.fMicro
.String());
105 // The pre-version works differently: The empty string is greater than any
106 // non-empty string (e.g. "R1" is newer than "R1-rc2"). So we catch the
107 // empty string cases first.
108 if (fPreRelease
.IsEmpty()) {
109 if (!other
.fPreRelease
.IsEmpty())
111 } else if (other
.fPreRelease
.IsEmpty()) {
114 // both are non-null -- compare normally
115 diff
= NaturalCompare(fPreRelease
.String(), other
.fPreRelease
.String());
120 return (int)fRevision
- (int)other
.fRevision
;
125 BPackageVersion::ToString() const
127 if (InitCheck() != B_OK
)
130 BString string
= fMajor
;
132 if (fMinor
.Length() > 0) {
133 string
<< '.' << fMinor
;
134 if (fMicro
.Length() > 0)
135 string
<< '.' << fMicro
;
138 if (!fPreRelease
.IsEmpty())
139 string
<< '~' << fPreRelease
;
142 string
<< '-' << fRevision
;
149 BPackageVersion::SetTo(const BString
& major
, const BString
& minor
,
150 const BString
& micro
, const BString
& preRelease
, uint32 revision
)
155 fPreRelease
= preRelease
;
156 fRevision
= revision
;
161 fPreRelease
.ToLower();
166 BPackageVersion::SetTo(const BString
& versionString
, bool revisionIsOptional
)
169 return BPackageInfo::ParseVersionString(versionString
, revisionIsOptional
,
175 BPackageVersion::Clear()
180 fPreRelease
.Truncate(0);
185 } // namespace BPackageKit