diff options
Diffstat (limited to 'src/utilfuns/swversion.cpp')
-rw-r--r-- | src/utilfuns/swversion.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/utilfuns/swversion.cpp b/src/utilfuns/swversion.cpp new file mode 100644 index 0000000..48c85ff --- /dev/null +++ b/src/utilfuns/swversion.cpp @@ -0,0 +1,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; +} |