-
Notifications
You must be signed in to change notification settings - Fork 0
/
NEVersionCompare.m
207 lines (187 loc) · 7.31 KB
/
NEVersionCompare.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// NEVersionCompare.h
// https://github.com/nicolasembleton/NEVersionCompare
//
// Created by Nicolas Embleton on 3/17/12.
// Copyright (c) 2012 Nicolas Embleton. All rights reserved.
//
// Current version: 1.0.0
/*
This code is licensed under a Simplified BSD License. See LICENSE for details.
Follow me at:
Twitter: @nicolasembleton
G+: https://plus.google.com/u/0/103866161283605516596/
*/
#import "NEVersionCompare.h"
@implementation NEVersionCompare
@synthesize iminor, imedium, imajor, ibuild;
#pragma mark -
#pragma mark Initialize the versioning object
/**
*
* @params; (NSString*)fullText: The version string. Has to be of the pattern XXX or XXX.XXX or XXX.XXX.XXX or XXX.XXX.XXX.XXX for build handling. Note that every missing part will be defaulted to 0
* @description; Initialize an NEVersionCompare and parse the version string
* @return; NEVersionCompare self object initialized with parsed value
*/
- (id) initWithFullTextVersion:(NSString*)fullText {
if(__NEVC_VERBOSE__) {
NSLog(@"NEVersion.initWithFullTextVersion; Current version: %@ ", fullText);
}
fullTextVersion = fullText;
/** Obsolete
minor = [fullText substringFromIndex:4];
medium = [fullText substringWithRange:NSMakeRange(2, 1)];
major = [fullText substringToIndex:1];
*/
int index = 0;
int level = 1; // 1 = major, 2 = medium, 3 = minor, 4 = build
NSString *tmpChar = @"", *tmpValue = @"";
// Let's traverse the string and stop at the 3rd level ( no "build" yet )
while ( index < [fullText length] && level <= 4) {
// Get just 1 character
tmpChar = [fullText substringWithRange:NSMakeRange(index, 1)];
if([tmpChar isEqualToString:@"."]){
// Process the swap with actual value
switch (level) {
case 1:
major = tmpValue;
break;
case 2:
medium = tmpValue;
break;
case 3:
minor = tmpValue;
break;
case 4:
build = tmpValue;
break;
default:
// Do nothing... should not happen
break;
}
tmpValue = @"";
level++;
} else {
// Append the char to tmpValue
tmpValue = [tmpValue stringByAppendingString:tmpChar];
}
index++;
}
// Latest swap for either medium, minor or build version ( @"" if build doesn't exist )
if(level == 1) { // major. Means the pattern was XXX
major = tmpValue;
medium = minor = build = @"";
} else if(level == 2) { // medium
medium = tmpValue;
minor = build = @""; // 0 default to every other part
} else if(level == 3) { // minor
minor = tmpValue;
build = @""; // no build info
} else if( level == 4 ) { // build
build = tmpValue;
} else {
// default all to 0
major = medium = minor = build = @"";
}
iminor = [minor intValue];
imedium = [medium intValue];
imajor = [major intValue];
ibuild = [build intValue]; // defaulted to 0 if no build
if(__NEVC_VERBOSE__) {
NSLog(@"Current app Version Major (%@) Medium (%@) Minor (%@) Build (%@): %@ ", major, medium, minor, build, fullText);
NSLog(@"Current app Version Major (%i) Medium (%i) Minor (%i) Build (%i): %@ ", imajor, imedium, iminor, ibuild, fullText);
}
return self;
}
#pragma mark -
#pragma mark Comparison method
/**
*
* @params; compareArg: NEVersionCompare object that will be compared agains the self object. Self being the reference
* @description; Provides a convenient way to compare 2 versions. For Build comparison handling, use the function -(int)compareWith:(NEVersionCompare*)compareArg withBuild:(bool)buildEnabled;
* @return; NEVersionEnum value.
*/
-(int) compareWith:(NEVersionCompare*)compareArg {
if(self.imajor > compareArg.imajor) {
// Major is greater
return NEVersionGreaterThan;
} else if( self.imajor == compareArg.imajor ){
// Majors are equivalent
// Compare the medium
if( self.imedium > compareArg.imedium ){
// Medium is greater
return NEVersionGreaterThan;
} else if ( self.imedium == compareArg.imedium ) {
// Mediums are equivalent
if( self.iminor > compareArg.iminor ) {
// Minor is greater
return NEVersionGreaterThan;
} else if ( self.iminor == compareArg.iminor ) {
// 2 versions are equivalent
return NEVersionEquivalent;
} else {
// Minor is smaller
return NEVersionSmallerThan;
}
} else {
// Medium is smaller
return NEVersionSmallerThan;
}
} else {
// Major is smaller
return NEVersionSmallerThan;
}
}
/**
*
* @params; compareArg: NEVersionCompare object that will be compared agains the self object. Self being the reference
buildEnabled: Pass true if you want to enable the build comparison. If NO is passed, this function has the same behavior as -(int) compareWith:(NEVersionCompare*)compareArg; function
* @description; Provides a convenient way to compare 2 versions with build comparison
* @return; NEVersionEnum value. Note that if buildEnabled is set to YES, the return values will be either one of NEVersionEquivalent, NEVersionEqualButBuildGreaterThan or NEVersionEqualButBuildSmallerThan
*/
-(int)compareWith:(NEVersionCompare*)compareArg withBuild:(BOOL)buildEnabled {
if(self.imajor > compareArg.imajor) {
// Major is greater
return NEVersionGreaterThan;
} else if( self.imajor == compareArg.imajor ){
// Majors are equivalent
// Compare the medium
if( self.imedium > compareArg.imedium ){
// Medium is greater
return NEVersionGreaterThan;
} else if ( self.imedium == compareArg.imedium ) {
// Mediums are equivalent
if( self.iminor > compareArg.iminor ) {
// Minor is greater
return NEVersionGreaterThan;
} else if ( self.iminor == compareArg.iminor ) {
// 2 versions are equivalent
if(buildEnabled) {
// Let's check the builds
if( self.ibuild > compareArg.ibuild ) {
// Return that versions are same but build is greater
return NEVersionEqualButBuildGreaterThan;
} else if ( self.ibuild == compareArg.ibuild ) {
// Only 1 value for equivalent
return NEVersionEquivalent;
} else {
return NEVersionEqualButBuildSmallerThan;
}
} else {
// buildEnable is false, so do not check them
return NEVersionEquivalent;
}
} else {
// Minor is smaller
return NEVersionSmallerThan;
}
} else {
// Medium is smaller
return NEVersionSmallerThan;
}
} else {
// Major is smaller
return NEVersionSmallerThan;
}
}
@end