The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swversion.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * swversion.cpp - SWVersion: version number utility class
4  *
5  * $Id: swversion.cpp 2980 2013-09-14 21:51:47Z scribe $
6  *
7  * Copyright 2001-2013 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 #include <swversion.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 
30 
31 
33 
34 
35 /******************************************************************************
36  * SWVersion c-tor - Constructs a new SWVersion
37  *
38  * ENT: version - const version string
39  */
40 
41 SWVersion::SWVersion(const char *version) {
42  char *buf = new char[ strlen(version) + 1 ];
43  char *tok;
44  major = minor = minor2 = minor3 = -1;
45 
46  strcpy(buf, version);
47  tok = strtok(buf, ".");
48  if (tok)
49  major = atoi(tok);
50  tok = strtok(0, ".");
51  if (tok)
52  minor = atoi(tok);
53  tok = strtok(0, ".");
54  if (tok)
55  minor2 = atoi(tok);
56  tok = strtok(0, ".");
57  if (tok)
58  minor3 = atoi(tok);
59  delete [] buf;
60 }
61 
62 
63 /******************************************************************************
64  * compare - compares this version to another version
65  *
66  * ENT: vi - other version with which to compare
67  *
68  * RET: = 0 if equal;
69  * < 0 if this version is less than other version;
70  * > 0 if this version is greater than other version
71  */
72 
73 int SWVersion::compare(const SWVersion &vi) const {
74  if (major == vi.major)
75  if (minor == vi.minor)
76  if (minor2 == vi.minor2)
77  if (minor3 == vi.minor3)
78  return 0;
79  else return minor3 - vi.minor3;
80  else return minor2 - vi.minor2;
81  else return minor - vi.minor;
82  else return major - vi.major;
83 }
84 
85 
86 const char *SWVersion::getText() const {
87 
88  // 255 is safe because there is no way 4 integers (plus 3 '.'s) can have
89  // a string representation that will overrun this buffer
90  static char buf[255];
91 
92  if (minor > -1) {
93  if (minor2 > -1) {
94  if (minor3 > -1) {
95  sprintf(buf, "%d.%d.%d.%d", major, minor, minor2, minor3);
96  }
97  else sprintf(buf, "%d.%d.%d", major, minor, minor2);
98  }
99  else sprintf(buf, "%d.%d", major, minor);
100  }
101  else sprintf(buf, "%d", major);
102 
103  return buf;
104 }
105 
106 
108 
int minor3
Definition: swversion.h:43
#define SWORD_NAMESPACE_START
Definition: defs.h:39
int minor
Definition: swversion.h:43
int major
Definition: swversion.h:43
const char * getText() const
Definition: swversion.cpp:86
int compare(const SWVersion &vi) const
Definition: swversion.cpp:73
int minor2
Definition: swversion.h:43
static SWVersion currentVersion
Definition: swversion.h:69
#define SWORD_NAMESPACE_END
Definition: defs.h:40
#define SWORD_VERSION_STR
Definition: swversion.h:28
SWVersion(const char *version="0.0")
Definition: swversion.cpp:41