2011-06-21 20:26:32 +10:00
|
|
|
/*
|
|
|
|
* This file is part of libgim.
|
|
|
|
*
|
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2012-04-23 13:06:41 +10:00
|
|
|
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
2011-06-21 20:26:32 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#include "version.hpp"
|
|
|
|
|
2011-10-07 21:58:16 +11:00
|
|
|
#include <cstring>
|
2011-05-23 17:18:52 +10:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
using util::version;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-08-05 21:46:11 +10:00
|
|
|
version::version ():
|
2016-01-19 18:31:35 +11:00
|
|
|
size (0),
|
|
|
|
release (PRODUCTION)
|
2013-08-05 21:46:11 +10:00
|
|
|
{ ; }
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
version::version (unsigned int _major,
|
|
|
|
unsigned int _minor):
|
2016-01-19 18:31:35 +11:00
|
|
|
size (0),
|
|
|
|
components { _major, _minor },
|
|
|
|
release (PRODUCTION)
|
2012-07-30 16:32:32 +10:00
|
|
|
{
|
2016-01-19 18:31:35 +11:00
|
|
|
components[MAJOR] = _major;
|
|
|
|
components[MINOR] = _minor;
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-08-05 21:46:11 +10:00
|
|
|
version::version (const std::string& str):
|
2016-01-19 18:31:35 +11:00
|
|
|
version (str.c_str ())
|
|
|
|
{ ; }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-07 21:58:16 +11:00
|
|
|
version::version (const char *str):
|
2016-01-19 18:31:35 +11:00
|
|
|
version ()
|
2012-07-30 16:32:32 +10:00
|
|
|
{
|
2013-08-05 21:46:11 +10:00
|
|
|
*this = version::parse (str);
|
2011-10-07 21:58:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-05-23 17:18:52 +10:00
|
|
|
static void
|
2016-01-19 18:31:35 +11:00
|
|
|
sanity (version::release_t r)
|
|
|
|
{
|
2011-05-23 17:18:52 +10:00
|
|
|
switch (r) {
|
2016-01-19 18:31:35 +11:00
|
|
|
case version::ALPHA:
|
|
|
|
case version::BETA:
|
|
|
|
case version::GAMMA:
|
|
|
|
case version::PRODUCTION:
|
2011-05-23 17:18:52 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
panic ("invalid release_t value");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
void
|
|
|
|
version::sanity (void) const {
|
2013-08-05 21:46:11 +10:00
|
|
|
::sanity (release);
|
|
|
|
CHECK (size > 0);
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
unsigned version::major (void) const { return components[MAJOR]; }
|
|
|
|
unsigned version::minor (void) const { return components[MINOR]; }
|
|
|
|
unsigned version::point (void) const { return components[POINT]; }
|
|
|
|
unsigned version::build (void) const { return components[BUILD]; }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
const unsigned*
|
|
|
|
version::begin (void) const
|
|
|
|
{
|
|
|
|
return components.begin ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const unsigned*
|
|
|
|
version::end (void) const
|
|
|
|
{
|
|
|
|
return components.begin () + size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-05-23 17:18:52 +10:00
|
|
|
%%{
|
|
|
|
machine version;
|
|
|
|
|
|
|
|
action clear
|
2013-08-05 21:46:11 +10:00
|
|
|
{ accum = 0; }
|
2011-05-23 17:18:52 +10:00
|
|
|
action increment
|
2013-08-05 21:46:11 +10:00
|
|
|
{ accum *= 10;
|
|
|
|
accum += (uintptr_t)(fc - (unsigned char)'0'); }
|
2011-05-23 17:18:52 +10:00
|
|
|
action finish
|
2016-01-19 18:31:35 +11:00
|
|
|
{ v.components[v.size++] = accum ; }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
number = (digit+)
|
|
|
|
>clear
|
|
|
|
$increment
|
|
|
|
%finish;
|
|
|
|
|
|
|
|
dots = (number '.')* number;
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
type = ('beta'i | 'b'i) %{ v.release = BETA; }
|
|
|
|
| ('alpha'i | 'a'i) %{ v.release = ALPHA; }
|
|
|
|
| ('gamma'i | 'g'i) %{ v.release = GAMMA; };
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-03-03 14:35:28 +11:00
|
|
|
version := (dots type?) >clear
|
2013-08-05 21:46:11 +10:00
|
|
|
$!{ throw std::invalid_argument (str); };
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2012-07-30 16:32:32 +10:00
|
|
|
write data;
|
2011-05-23 17:18:52 +10:00
|
|
|
}%%
|
|
|
|
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
util::version
|
|
|
|
util::version::parse (const std::string& str) {
|
2015-07-13 16:29:50 +10:00
|
|
|
unsigned int accum = 0;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2012-04-11 15:49:11 +10:00
|
|
|
int cs;
|
2011-05-23 17:18:52 +10:00
|
|
|
const char *p = str.data (),
|
|
|
|
*pe = str.data () + str.size (),
|
|
|
|
*eof = pe;
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
version v;
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
%%write init;
|
|
|
|
%%write exec;
|
2013-08-05 21:46:11 +10:00
|
|
|
|
|
|
|
return v;
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-08-05 21:46:11 +10:00
|
|
|
util::version
|
|
|
|
util::version::parse (const char *str) {
|
|
|
|
return parse (std::string (str));
|
2011-10-07 21:58:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-05-23 17:18:52 +10:00
|
|
|
bool
|
2016-01-19 18:31:35 +11:00
|
|
|
version::operator> (const version &rhs) const
|
|
|
|
{
|
|
|
|
unsigned int count = min (size, rhs.size);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < count; ++i)
|
2016-01-19 18:31:35 +11:00
|
|
|
if (components[i] < rhs.components[i])
|
2011-05-23 17:18:52 +10:00
|
|
|
return false;
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
if (size < rhs.size)
|
2011-05-23 17:18:52 +10:00
|
|
|
return false;
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
if (release <= rhs.release)
|
2011-05-23 17:18:52 +10:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-08-05 21:46:11 +10:00
|
|
|
bool
|
|
|
|
version::operator== (const version &rhs) const {
|
2016-01-19 18:31:35 +11:00
|
|
|
return components == rhs.components &&
|
|
|
|
size == rhs.size &&
|
|
|
|
release == rhs.release;
|
2013-08-05 21:46:11 +10:00
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace util {
|
|
|
|
std::ostream&
|
|
|
|
operator<< (std::ostream& os, const util::version::release_t r)
|
|
|
|
{
|
|
|
|
switch (r) {
|
|
|
|
case version::ALPHA: return os << 'a';
|
|
|
|
case version::BETA: return os << 'b';
|
|
|
|
case version::GAMMA: return os << 'g';
|
|
|
|
case version::PRODUCTION: return os;
|
|
|
|
|
|
|
|
default:
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2013-08-05 21:46:11 +10:00
|
|
|
std::ostream&
|
2016-01-19 18:31:35 +11:00
|
|
|
util::operator<< (std::ostream& os, const util::version& rhs)
|
|
|
|
{
|
2013-08-05 21:46:11 +10:00
|
|
|
size_t elements = rhs.size;
|
2015-11-13 17:10:10 +11:00
|
|
|
CHECK_GT (elements, 0u);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
do {
|
|
|
|
os << rhs.major ();
|
|
|
|
if (!--elements)
|
|
|
|
break;
|
|
|
|
|
|
|
|
os << "." << rhs.minor ();
|
|
|
|
if (!--elements)
|
|
|
|
break;
|
2013-08-05 21:46:11 +10:00
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
os << "." << rhs.point ();
|
|
|
|
if (!--elements)
|
|
|
|
break;
|
2013-08-05 21:46:11 +10:00
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
os << "." << rhs.build ();
|
|
|
|
CHECK_EQ (elements - 1, 0u);
|
|
|
|
} while (0);
|
2013-08-05 21:46:11 +10:00
|
|
|
|
2016-01-19 18:31:35 +11:00
|
|
|
os << rhs.release;
|
2013-08-05 21:46:11 +10:00
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|