File: version.c

package info (click to toggle)
xblast-tnt 2.10.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,444 kB
  • sloc: ansic: 54,105; sh: 4,014; makefile: 129; sed: 16
file content (220 lines) | stat: -rwxr-xr-x 5,538 bytes parent folder | download | duplicates (6)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * file version.c - tools for version data
 *
 * $Id: version.c,v 1.13 2006/02/10 15:07:17 fzago Exp $
 *
 * Program XBLAST
 * (C) by Oliver Vogel (e-mail: m.vogel@ndh.net)
 *
 * 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; either version 2; or (at your option)
 * any later version
 *
 * This program is distributed in the hope that it will be entertaining,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "xblast.h"

/* initial joint version */
static XBVersion jointVersion = { VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH,
};

/* constant versions */
const XBVersion Ver_Local = { VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH };
const XBVersion Ver_None = { 0, 0, 0 };
const XBVersion Ver_2_10_1 = { 2, 10, 1 };
const XBVersion Ver_2_10_2 = { 2, 10, 2 };

/* version table for hosts */
static XBVersion hostVersion[MAX_HOSTS];

/*************************************
 * general tools for version structs *
 *************************************/

/*
 * return temporary string for a version
 */
char *
Version_ToString (const XBVersion * ver)
{
	static char tmp[20];
	sprintf (tmp, "V%i.%i.%i", ver->major, ver->minor, ver->patch);
	return tmp;
}								/* Version_ToString */

/*
 * clear a version struct
 */
void
Version_Clear (XBVersion * ver)
{
	assert (NULL != ver);
	memcpy (ver, &Ver_None, sizeof (XBVersion));
}								/* Version_Clear */

/*
 * check if version is set
 */
XBBool
Version_isDefined (const XBVersion * ver)
{
	assert (NULL != ver);
	return (memcmp (ver, &Ver_None, sizeof (XBVersion)) != 0);
}								/* Version_isDefined */

/*
 * compare two versions, return sign of v1-v2
 */
int
Version_Compare (const XBVersion * v1, const XBVersion * v2)
{
	assert (NULL != v1);
	assert (NULL != v2);
	if (v1->major > v2->major) {
		return (1);
	}
	else if (v1->major < v2->major) {
		return (-1);
	}
	if (v1->minor > v2->minor) {
		return (1);
	}
	else if (v1->minor < v2->minor) {
		return (-1);
	}
	if (v1->patch > v2->patch) {
		return (1);
	}
	else if (v1->patch < v2->patch) {
		return (-1);
	}
	return (0);
}								/* Version_Compare */

/******************************
 * getting local data version *
 ******************************/

/*
 * return local version data as XBVersion
 */
void
Version_Get (XBVerType type, XBVersion * ver)
{
	assert (NULL != ver);
	switch (type) {
	case VERSION_JOINT:
		memcpy (ver, &jointVersion, sizeof (XBVersion));
		return;
	default:
		assert (type < MAX_HOSTS);
		memcpy (ver, hostVersion + type, sizeof (XBVersion));
		return;
	}
}								/* Version_Get */

/*
 * check if joint version is at least given version
 */
XBBool
Version_AtLeast (XBVerType type, const XBVersion * ver)
{
	assert (NULL != ver);
	switch (type) {
	case VERSION_JOINT:
		return (Version_Compare (&jointVersion, ver) >= 0);
	default:
		assert (type < MAX_HOSTS);
		return (Version_Compare (hostVersion + type, ver) >= 0);
	}
}								/* Version_AtLeast */

/***************************
 * modifying host version *
 ***************************/

/*
 * reset host versions
 */
void
Version_Reset (void)
{
	unsigned char id;
	for (id = 0; id < MAX_HOSTS; id++) {
		Version_Clear (hostVersion + id);
	}
	Dbg_Version ("Resetting host version\n");
	jointVersion = Ver_Local;
}								/* Version_Reset */

/*
 * set a host version and modify to joint version, return change
 */
XBBool
Version_Join (unsigned char id, const XBVersion * ver)
{
	assert (NULL != ver);
	assert (id < MAX_HOSTS);
	if (Version_isDefined (ver)) {
		Dbg_Version ("Current joint version %s\n", Version_ToString (&jointVersion));
		Dbg_Version ("Joining version %s for host #%u\n", Version_ToString (ver), id);
		memcpy (hostVersion + id, ver, sizeof (XBVersion));
		if (Version_Compare (ver, &jointVersion) < 0) {
			jointVersion = *ver;
			Dbg_Version ("Updating joint version\n");
			return XBTrue;
		}
	}
	else {
		Dbg_Version ("Ignoring undefined version for host #%u\n", id);
	}
	return XBFalse;
}								/* Version_Join */

/*
 * remove a single host version, return change
 */
XBBool
Version_Remove (unsigned char id)
{
	unsigned char h;
	assert (id < MAX_HOSTS);
	if (Version_isDefined (hostVersion + id)) {
		Dbg_Version ("Current joint version %s\n", Version_ToString (&jointVersion));

		/*  first calculate joint version without the host version to be removed */
		memcpy (&jointVersion, &Ver_Local, sizeof (XBVersion));
		for (h = 0; h < MAX_HOSTS; h++) {
			if (h != id && Version_isDefined (hostVersion + h)
				&& Version_Compare (hostVersion + h, &jointVersion) < 0) {
				memcpy (&jointVersion, hostVersion + h, sizeof (XBVersion));
			}
		}

		/* now compare with version to be removed, then clear */
		if (Version_Compare (&jointVersion, hostVersion + id) > 0) {
			Dbg_Version ("removing version for host #%u, joint version update to %s", id,
						 Version_ToString (&jointVersion));
			Version_Clear (hostVersion + id);
			return XBTrue;
		}
		else {
			Dbg_Version ("removing version for host #%u\n", id);
			Version_Clear (hostVersion + id);
		}
	}
	return XBFalse;
}								/* Version_Remove */

/*
 * end of file com.c
 */