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"
|
|
|
|
|
|
|
|
|
2011-10-07 21:58:16 +11:00
|
|
|
using namespace util;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
version::version ():
|
|
|
|
size (0)
|
|
|
|
{ ; }
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
version::version (unsigned int _major,
|
|
|
|
unsigned int _minor):
|
2013-08-05 21:46:11 +10:00
|
|
|
size (2),
|
|
|
|
release (RELEASE_PRODUCTION)
|
2012-07-30 16:32:32 +10:00
|
|
|
{
|
2013-08-05 21:46:11 +10:00
|
|
|
values[OFFSET_MAJOR] = _major;
|
|
|
|
values[OFFSET_MINOR] = _minor;
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
version::version (const std::string& str):
|
|
|
|
size (0)
|
2012-07-30 16:32:32 +10:00
|
|
|
{
|
2013-08-05 21:46:11 +10:00
|
|
|
*this = version::parse (str);
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-07 21:58:16 +11:00
|
|
|
version::version (const char *str):
|
2013-08-05 21:46:11 +10:00
|
|
|
size (0)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
static void
|
2013-08-05 21:46:11 +10:00
|
|
|
sanity (version::release_t r) {
|
2011-05-23 17:18:52 +10:00
|
|
|
switch (r) {
|
|
|
|
case version::RELEASE_ALPHA:
|
|
|
|
case version::RELEASE_BETA:
|
|
|
|
case version::RELEASE_GAMMA:
|
|
|
|
case version::RELEASE_PRODUCTION:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
panic ("invalid release_t value");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
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
|
2013-08-05 21:46:11 +10:00
|
|
|
{ v.values[v.size++] = accum ; }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
number = (digit+)
|
|
|
|
>clear
|
|
|
|
$increment
|
|
|
|
%finish;
|
|
|
|
|
|
|
|
dots = (number '.')* number;
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
type = ('beta'i | 'b'i) %{ v.release = RELEASE_BETA; }
|
|
|
|
| ('alpha'i | 'a'i) %{ v.release = RELEASE_ALPHA; }
|
|
|
|
| ('gamma'i | 'g'i) %{ v.release = 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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
static std::string
|
|
|
|
release_to_string (version::release_t r) {
|
|
|
|
sanity (r);
|
|
|
|
|
|
|
|
static const char* RELEASE_STRINGS[] = {
|
|
|
|
"a", // RELEASE_ALPHA
|
|
|
|
"b", // RELEASE_BETA
|
|
|
|
"g", // RELEASE_GAMMA
|
|
|
|
"" // RELEASE_PRODUCTION
|
|
|
|
};
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
return RELEASE_STRINGS[r];
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2012-07-30 16:32:32 +10:00
|
|
|
version::operator> (const version &rhs) const {
|
2013-08-05 21:46:11 +10:00
|
|
|
unsigned int count = min (values.size (), rhs.values.size ());
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < count; ++i)
|
2013-08-05 21:46:11 +10:00
|
|
|
if (values[i] < rhs.values[i])
|
2011-05-23 17:18:52 +10:00
|
|
|
return false;
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
if (values.size () < rhs.values.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
bool
|
|
|
|
version::operator== (const version &rhs) const {
|
|
|
|
return values == rhs.values &&
|
|
|
|
size == rhs.size &&
|
|
|
|
release == rhs.release;
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
std::ostream&
|
|
|
|
operator <<(std::ostream& os, const util::version& rhs) {
|
|
|
|
size_t elements = rhs.size;
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK_GT (elements, 0);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2013-08-05 21:46:11 +10:00
|
|
|
os << rhs.major ();
|
|
|
|
if (!--elements)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
os << "." << rhs.minor ();
|
|
|
|
if (!--elements)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
os << "." << rhs.point ();
|
|
|
|
if (!--elements)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
os << "." << rhs.build ();
|
|
|
|
CHECK_EQ (--elements, 0);
|
|
|
|
|
|
|
|
done:
|
|
|
|
os << release_to_string (rhs.release);
|
|
|
|
return os;
|
|
|
|
}
|