]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/versionlib.h
Callback: work at any arity
[xonotic/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         return version.major < other.major || ( !( other.major < version.major ) && version.minor < other.minor );
38 }
39
40 template<typename TextOutputStreamType>
41 TextOutputStreamType& ostream_write( TextOutputStreamType& outputStream, const Version& version ){
42         return outputStream << version.major << '.' << version.minor;
43 }
44
45 /// \brief Returns true if \p version (code) is compatible with \p other (data).
46 inline bool version_compatible( const Version& version, const Version& other ){
47         return version.major == other.major // different major-versions are always incompatible
48                    && !( version.minor < other.minor ); // data minor-version is incompatible if greater than code minor-version
49 }
50
51 inline int string_range_parse_unsigned_decimal_integer( const char* first, const char* last ){
52         int result = 0;
53         for (; first != last; ++first )
54         {
55                 result *= 10;
56                 result += *first - '0';
57         }
58         return result;
59 }
60
61 inline Version version_parse( const char* versionString ){
62         Version version;
63         const char* endVersion = versionString + strlen( versionString );
64
65         const char* endMajor = strchr( versionString, '.' );
66         if ( endMajor == 0 ) {
67                 endMajor = endVersion;
68
69                 version.minor = 0;
70         }
71         else
72         {
73                 const char* endMinor = strchr( endMajor + 1, '.' );
74                 if ( endMinor == 0 ) {
75                         endMinor = endVersion;
76                 }
77                 version.minor = string_range_parse_unsigned_decimal_integer( endMajor + 1, endMinor );
78         }
79         version.major = string_range_parse_unsigned_decimal_integer( versionString, endMajor );
80
81         return version;
82 }
83
84 #endif