]> icculus.org git repositories - divverent/netradiant.git/blob - libs/versionlib.h
better progress display
[divverent/netradiant.git] / libs / versionlib.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #if !defined(INCLUDED_VERSIONLIB_H)
23 #define INCLUDED_VERSIONLIB_H
24
25 #include <cstddef>
26 #include <string.h>
27 #include <algorithm>
28
29 class Version
30 {
31 public:
32   int major;
33   int minor;
34 };
35
36 inline bool operator<(const Version& version, const Version& other)
37 {
38   return version.major < other.major || (!(other.major < version.major) && version.minor < other.minor);
39 }
40
41 template<typename TextOutputStreamType>
42 TextOutputStreamType& ostream_write(TextOutputStreamType& outputStream, const Version& version)
43 {
44   return outputStream << version.major << '.' << version.minor;
45 }
46
47 /// \brief Returns true if \p version (code) is compatible with \p other (data).
48 inline bool version_compatible(const Version& version, const Version& other)
49 {
50   return version.major == other.major  // different major-versions are always incompatible
51     && !(version.minor < other.minor); // data minor-version is incompatible if greater than code minor-version
52 }
53
54 inline int string_range_parse_unsigned_decimal_integer(const char* first, const char* last)
55 {
56   int result = 0;
57   for(; first != last; ++first)
58   {
59     result *= 10;
60     result += *first - '0';
61   }
62   return result;
63 }
64
65 inline Version version_parse(const char* versionString)
66 {
67   Version version;
68   const char* endVersion = versionString + strlen(versionString);
69
70   const char* endMajor = strchr(versionString, '.');
71   if(endMajor == 0)
72   {
73     endMajor = endVersion;
74
75     version.minor = 0;
76   }
77   else
78   {
79     const char* endMinor = strchr(endMajor + 1, '.');
80     if(endMinor == 0)
81     {
82       endMinor = endVersion;
83     }
84     version.minor = string_range_parse_unsigned_decimal_integer(endMajor + 1, endMinor);
85   }
86   version.major = string_range_parse_unsigned_decimal_integer(versionString, endMajor);
87
88   return version;
89 }
90
91 #endif