-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientversion.cpp
More file actions
180 lines (152 loc) · 3.88 KB
/
clientversion.cpp
File metadata and controls
180 lines (152 loc) · 3.88 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
** clientversion.cpp
**
** Copyright © Quazaa Development Team, 2014.
** This file is part of QUAZAA (quazaa.sourceforge.net)
**
** Quazaa is free software; this file may be used under the terms of the GNU
** General Public License version 3.0 or later as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Quazaa 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.
**
** Please review the following information to ensure the GNU General Public
** License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** You should have received a copy of the GNU General Public License version
** 3.0 along with Quazaa; if not, write to the Free Software Foundation,
** Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <QDebug>
#include <QRegularExpression>
#include "clientversion.h"
ClientVersion::ClientVersion() :
m_eStyle( Style::Unknown ),
m_nVersion( 0 ),
m_sVersion( "" )
{
}
ClientVersion::ClientVersion( const QString& sVersion , Style eStyle ) :
m_eStyle( eStyle )
{
qDebug() << "Parsing ClientVersion String: " << sVersion.toLocal8Bit().data();
quint8 bytes[4];
bytes[0] = 0;
bytes[1] = 0;
bytes[2] = 0;
bytes[3] = 0;
QString sTmp = sVersion.trimmed();
int nVersionLength = 0;
switch ( eStyle )
{
case Style::QuazaaDefault:
{
int n, nPos;
for ( quint8 i = 3; i > 0; --i )
{
nPos = sTmp.indexOf( '.' );
bytes[i] = sTmp.left( nPos ).toUInt();
sTmp = sTmp.mid( nPos + 1 );
}
if ( sTmp.indexOf( QRegularExpression( "(1[0-9][0-9]|2[0-4][0-9]|25[0-5])" ) ) == 0 )
{
n = 3;
}
else if ( sTmp.indexOf( QRegularExpression( "[1-9][0-9]" ) ) == 0 )
{
n = 2;
}
else
{
#ifdef _DEBUG
Q_ASSERT( sTmp.indexOf( QRegularExpression( "[0-9]" ) ) == 0 );
#endif // _DEBUG
n = 1;
}
bytes[0] = sTmp.left( n ).toUInt();
nVersionLength = sVersion.size() - sTmp.size() + n;
break;
}
case Style::eMule:
case Style::Simple:
{
int nPos = sTmp.indexOf( '.' );
bytes[3] = sTmp.left( nPos ).toUInt();
sTmp = sTmp.mid( nPos + 1 );
int n;
if ( sTmp.indexOf( QRegularExpression( "(1[0-9][0-9]|2[0-4][0-9]|25[0-5])" ) ) == 0 )
{
n = 3;
}
else if ( sTmp.indexOf( QRegularExpression( "[0-9][0-9]" ) ) == 0 )
{
n = 2;
}
else
{
#ifdef _DEBUG
Q_ASSERT( sTmp.indexOf( QRegularExpression( "[0-9]" ) ) == 0 );
#endif // _DEBUG
n = 1;
}
bytes[2] = sTmp.left( n ).toUInt();
nVersionLength = sVersion.size() - sTmp.size() + n;
if ( eStyle == Style::eMule )
{
Q_ASSERT( n == 2 );
QChar cVersion = sTmp[2];
Q_ASSERT( cVersion.isLower() );
ushort nVal = cVersion.unicode() - QChar( 'a' ).unicode();
Q_ASSERT( nVal >= 0 && nVal <= 26 );
bytes[1] = ( quint8 )nVal;
++nVersionLength;
}
break;
}
default:
// do nothing
break;
}
m_nVersion = *( quint32* )( &bytes );
m_sVersion = sVersion.left( nVersionLength );
qDebug() << "Extracted version number: " << m_nVersion;
}
ClientVersion& ClientVersion::operator=( const ClientVersion& other )
{
m_eStyle = other.m_eStyle;
m_nVersion = other.m_nVersion;
m_sVersion = other.m_sVersion;
return *this;
}
bool ClientVersion::operator<( const ClientVersion& other )
{
return m_nVersion < other.m_nVersion;
}
bool ClientVersion::operator>( const ClientVersion& other )
{
return m_nVersion > other.m_nVersion;
}
bool ClientVersion::operator<=( const ClientVersion& other )
{
return m_nVersion <= other.m_nVersion;
}
bool ClientVersion::operator>=( const ClientVersion& other )
{
return m_nVersion >= other.m_nVersion;
}
ClientVersion::Style ClientVersion::style() const
{
return m_eStyle;
}
quint32 ClientVersion::version() const
{
return m_nVersion;
}
QString ClientVersion::versionString() const
{
return m_sVersion;
}