summaryrefslogtreecommitdiffstats
path: root/src/common/common/version_data.cpp
blob: 926f290cbc362b19e4bd9f7c83846d5883f26a78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/***************************************************************************
 *   Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org>             *
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "version_data.h"

#include <tqregexp.h>

#include "number.h"

VersionData VersionData::fromString(const TQString &s)
{
  VersionData vd;
  TQRegExp re("([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)");
  if ( !re.exactMatch(s) ) return vd;
  vd._valid = true;
  vd._majorNum = re.cap(1).toUInt();
  vd._minorNum = re.cap(2).toUInt();
  vd._dotNum = re.cap(3).toUInt();
  vd._sub = re.cap(4);
  return vd;
}

VersionData VersionData::fromHexString(const TQString &s)
{
  VersionData vd;
  if ( s.length()!=6 ) return vd;
  vd._valid = true;
  vd._majorNum = ::fromString(NumberBase::Hex, s.mid(0, 2), 0);
  vd._minorNum = ::fromString(NumberBase::Hex, s.mid(2, 2), 0);
  vd._dotNum = ::fromString(NumberBase::Hex, s.mid(4, 2), 0);
  return vd;
}

TQString VersionData::pretty() const
{
  if ( !isValid() ) return "---";
  return TQString::number(_majorNum) + '.' + TQString::number(_minorNum) + '.' + TQString::number(_dotNum) + _sub;
}

TQString VersionData::prettyWithoutDot() const
{
  if ( !isValid() ) return "---";
  return TQString::number(_majorNum) + '.' + TQString::number(_minorNum);
}

uint VersionData::toUInt() const
{
  Q_ASSERT(_valid);
  return (_majorNum << 16) | (_minorNum << 8) | _dotNum;
}