xref: /haiku/src/kits/package/PackageVersion.cpp (revision 7a74a5df454197933bc6e80a542102362ee98703)
1 /*
2  * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <package/PackageVersion.h>
8 
9 #include <NaturalCompare.h>
10 
11 #include <package/hpkg/PackageInfoAttributeValue.h>
12 
13 
14 using BPrivate::NaturalCompare;
15 
16 
17 namespace BPackageKit {
18 
19 
20 BPackageVersion::BPackageVersion()
21 	:
22 	fRelease(0)
23 {
24 }
25 
26 
27 BPackageVersion::BPackageVersion(const BPackageVersionData& data)
28 {
29 	SetTo(data.major, data.minor, data.micro, data.preRelease, data.release);
30 }
31 
32 
33 BPackageVersion::BPackageVersion(const BString& major, const BString& minor,
34 	const BString& micro, const BString& preRelease, uint8 release)
35 {
36 	SetTo(major, minor, micro, preRelease, release);
37 }
38 
39 
40 status_t
41 BPackageVersion::InitCheck() const
42 {
43 	return fMajor.Length() > 0 ? B_OK : B_NO_INIT;
44 }
45 
46 
47 const BString&
48 BPackageVersion::Major() const
49 {
50 	return fMajor;
51 }
52 
53 
54 const BString&
55 BPackageVersion::Minor() const
56 {
57 	return fMinor;
58 }
59 
60 
61 const BString&
62 BPackageVersion::Micro() const
63 {
64 	return fMicro;
65 }
66 
67 
68 const BString&
69 BPackageVersion::PreRelease() const
70 {
71 	return fPreRelease;
72 }
73 
74 
75 uint8
76 BPackageVersion::Release() const
77 {
78 	return fRelease;
79 }
80 
81 
82 int
83 BPackageVersion::Compare(const BPackageVersion& other) const
84 {
85 	int diff = NaturalCompare(fMajor.String(), other.fMajor.String());
86 	if (diff != 0)
87 		return diff;
88 
89 	diff = NaturalCompare(fMinor.String(), other.fMinor.String());
90 	if (diff != 0)
91 		return diff;
92 
93 	diff = NaturalCompare(fMicro.String(), other.fMicro.String());
94 	if (diff != 0)
95 		return diff;
96 
97 	// The pre-version works differently: The empty string is greater than any
98 	// non-empty string (e.g. "R1" is newer than "R1-rc2"). So we catch the
99 	// empty string cases first.
100 	if (fPreRelease.IsEmpty()) {
101 		if (!other.fPreRelease.IsEmpty())
102 			return 1;
103 	} else if (other.fPreRelease.IsEmpty()) {
104 		return -1;
105 	} else {
106 		// both are non-null -- compare normally
107 		diff = NaturalCompare(fPreRelease.String(), other.fPreRelease.String());
108 		if (diff != 0)
109 			return diff;
110 	}
111 
112 	return (int)fRelease - (int)other.fRelease;
113 }
114 
115 
116 BString
117 BPackageVersion::ToString() const
118 {
119 	BString string = fMajor;
120 
121 	if (fMinor.Length() > 0) {
122 		string << '.' << fMinor;
123 		if (fMicro.Length() > 0)
124 			string << '.' << fMicro;
125 	}
126 
127 	if (!fPreRelease.IsEmpty())
128 		string << '-' << fPreRelease;
129 
130 	if (fRelease > 0)
131 		string << '-' << fRelease;
132 
133 	return string;
134 }
135 
136 
137 void
138 BPackageVersion::SetTo(const BString& major, const BString& minor,
139 	const BString& micro, const BString& preRelease, uint8 release)
140 {
141 	fMajor = major;
142 	fMinor = minor;
143 	fMicro = micro;
144 	fPreRelease = preRelease;
145 	fRelease = release;
146 
147 	fMajor.ToLower();
148 	fMinor.ToLower();
149 	fMicro.ToLower();
150 	fPreRelease.ToLower();
151 }
152 
153 
154 void
155 BPackageVersion::Clear()
156 {
157 	fMajor.Truncate(0);
158 	fMinor.Truncate(0);
159 	fMicro.Truncate(0);
160 	fPreRelease.Truncate(0);
161 	fRelease = 0;
162 }
163 
164 
165 }	// namespace BPackageKit
166