versionlib.h

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2001-2006, William Joseph.
00003  All Rights Reserved.
00004 
00005  This file is part of GtkRadiant.
00006 
00007  GtkRadiant is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  GtkRadiant is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with GtkRadiant; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #if !defined(INCLUDED_VERSIONLIB_H)
00023 #define INCLUDED_VERSIONLIB_H
00024 
00025 #include <cstddef>
00026 #include <string.h>
00027 #include <algorithm>
00028 
00029 class Version
00030 {
00031     public:
00032         int major;
00033         int minor;
00034 };
00035 
00036 inline bool operator< (const Version& version, const Version& other)
00037 {
00038     return version.major < other.major || (!(other.major < version.major) && version.minor < other.minor);
00039 }
00040 
00042 inline bool version_compatible (const Version& version, const Version& other)
00043 {
00044     return version.major == other.major // different major-versions are always incompatible
00045             && !(version.minor < other.minor); // data minor-version is incompatible if greater than code minor-version
00046 }
00047 
00048 inline int string_range_parse_unsigned_decimal_integer (const char* first, const char* last)
00049 {
00050     int result = 0;
00051     for (; first != last; ++first) {
00052         result *= 10;
00053         result += *first - '0';
00054     }
00055     return result;
00056 }
00057 
00058 inline Version version_parse (const std::string& versionString)
00059 {
00060     Version version;
00061     const char* endVersion = versionString.c_str() + versionString.length();
00062 
00063     const char* endMajor = strchr(versionString.c_str(), '.');
00064     if (endMajor == 0) {
00065         endMajor = endVersion;
00066 
00067         version.minor = 0;
00068     } else {
00069         const char* endMinor = strchr(endMajor + 1, '.');
00070         if (endMinor == 0) {
00071             endMinor = endVersion;
00072         }
00073         version.minor = string_range_parse_unsigned_decimal_integer(endMajor + 1, endMinor);
00074     }
00075     version.major = string_range_parse_unsigned_decimal_integer(versionString.c_str(), endMajor);
00076 
00077     return version;
00078 }
00079 
00080 #endif

Generated by  doxygen 1.6.2