summaryrefslogtreecommitdiff
path: root/src/utilfuns/swversion.cpp
blob: fe112db5a9b8874937edfd290f97f39b7825816a (plain)
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
/*
 * Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
 *	CrossWire Bible Society
 *	P. O. Box 2528
 *	Tempe, AZ  85280-2528
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 */

#include <config.h>

#include <swversion.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

SWORD_NAMESPACE_START

SWVersion SWVersion::currentVersion( VERSION );

/******************************************************************************
 * 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;
}

SWORD_NAMESPACE_END