aboutsummaryrefslogtreecommitdiffstats
path: root/src/utilfuns/swversion.cpp
blob: 48c85ff7a7958f5a89bc4565be395e0cb973fcdc (plain) (blame)
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
#include <swversion.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


SWVersion SWVersion::currentVersion(SWORDVER);

/******************************************************************************
 * SWVersion c-tor - Constructs a new SWVersion
 *
 * ENT:	version	- const version string
 */

SWVersion::SWVersion(const char *version) {
	char *buf = new char[ strlen(version) + 1 ];
	char *tok;
	major = minor = minor2 = minor3 = -1;
		
	strcpy(buf, version);
	tok = strtok(buf, ".");
	if (tok)
		major = atoi(tok);
	tok = strtok(0, ".");
	if (tok)
		minor = atoi(tok);
	tok = strtok(0, ".");
	if (tok)
		minor2 = atoi(tok);
	tok = strtok(0, ".");
	if (tok)
		minor3 = atoi(tok);
	delete [] buf;
}


/******************************************************************************
 * compare - compares this version to another version
 *
 * ENT:	vi	- other version with which to compare
 *
 * RET:	= 0 if equal;
 * 		< 0 if this version is less than other version;
 * 		> 0 if this version is greater than other version
 */

int SWVersion::compare(const SWVersion &vi) const {
	if (major == vi.major)
		if (minor == vi.minor)
			if (minor2 == vi.minor2)
				if (minor3 == vi.minor3)
					return 0;
				else return minor3 - vi.minor3;
			else	return minor2 - vi.minor2;
		else	return minor - vi.minor;
	else	return major - vi.major;
}


const char *SWVersion::getText() const {

	// 255 is safe because there is no way 4 integers (plus 3 '.'s) can have
	// a string representation that will overrun this buffer
	static char buf[255];

	if (minor > -1) {
		if (minor2 > -1) {
			if (minor3 > -1) {
				sprintf(buf, "%d.%d.%d.%d", major, minor, minor2, minor3);
			}
			else	sprintf(buf, "%d.%d.%d", major, minor, minor2);
		}
		else	sprintf(buf, "%d.%d", major, minor);
	}
	else	sprintf(buf, "%d", major);

	return buf;
}