ip2cparser.cpp
1 //------------------------------------------------------------------------------
2 // ip2cparser.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #include "ip2cparser.h"
24 
25 #include "datastreamoperatorwrapper.h"
26 #include "ip2c/ip2cparser_v2.h"
27 #include "ip2c/ip2cparser_v3.h"
28 
29 #include "global.h"
30 
31 #include <cstdint>
32 
33 bool IP2CParser::parse(QIODevice &dataBase)
34 {
35  QDataStream dstream(&dataBase);
36  dstream.setByteOrder(QDataStream::LittleEndian);
37  DataStreamOperatorWrapper stream(&dstream);
38 
39  // Verify the magic number.
40  const uint32_t magic = stream.readQUInt32();
41  if (magic != MAKEID('I', 'P', '2', 'C'))
42  return false;
43 
44  // Read the version.
45  if (!stream.hasRemaining())
46  return false;
47  const uint16_t version = stream.readQUInt16();
48  switch (version)
49  {
50  case 2:
51  return IP2CParserV2().parse(*this, dataBase);
52  case 3:
53  return IP2CParserV3().parse(*this, dataBase);
54  default:
55  return false;
56  }
57 }