23 #include "ip2cparser.h" 25 #include "datastreamoperatorwrapper.h" 31 #include <QMutexLocker> 34 IP2CParser::IP2CParser(
IP2C *pTargetDatabase)
36 currentParsingThread =
nullptr;
41 bool IP2CParser::doReadDatabase(
const QString &filePath)
43 QMutexLocker mutexLocker(&thisLock);
46 ConstructorDestructorParserStateSetter stateSetter(
this);
48 QFile dataBase(filePath);
49 gLog << tr(
"Parsing IP2C database: %1").arg(dataBase.fileName());
51 if (!dataBase.exists()
52 || !dataBase.open(QIODevice::ReadOnly)
53 || !dataBase.isReadable())
55 gLog << tr(
"Unable to open IP2C file.");
62 QByteArray dataArray = dataBase.readAll();
66 if (pos >= dataArray.size())
69 const char *data = dataArray.constData();
70 unsigned short version = READINT16(&data[pos]);
72 bool wasReadSuccessful =
false;
76 wasReadSuccessful = readDatabaseVersion2(dataArray);
80 wasReadSuccessful =
false;
84 if (!wasReadSuccessful)
87 gLog << tr(
"IP2C database read in %1 ms. Entries read: %2").arg(time.elapsed()).arg(
pTargetDatabase->numKnownEntries());
91 void IP2CParser::parsingThreadFinished()
93 bool bSuccessState = currentParsingThread->bSuccessState;
94 gLog << tr(
"IP2C parsing thread has finished.");
96 delete currentParsingThread;
97 currentParsingThread =
nullptr;
102 bool IP2CParser::readDatabase(
const QString &filePath)
104 bool bSuccess = doReadDatabase(filePath);
110 void IP2CParser::readDatabaseThreaded(
const QString &filePath)
112 if (currentParsingThread !=
nullptr)
115 auto pParsingThread =
new ParsingThread(
this, filePath);
116 connect(pParsingThread, SIGNAL(finished()),
this, SLOT(parsingThreadFinished()));
118 currentParsingThread = pParsingThread;
120 pParsingThread->start();
123 bool IP2CParser::readDatabaseVersion2(
const QByteArray &dataArray)
126 buffer.setData(dataArray);
127 buffer.open(QIODevice::ReadOnly);
128 QDataStream dstream(&buffer);
129 dstream.setByteOrder(QDataStream::LittleEndian);
131 stream.skipRawData(6);
136 QMap<unsigned, IP2C::IP2CData> hashTable;
138 while (stream.hasRemaining())
143 baseEntry.countryFullName = QString::fromUtf8(stream.readRawUntilByte(
'\0'));
144 baseEntry.country = QString::fromUtf8(stream.readRawUntilByte(
'\0'));
145 if (!stream.hasRemaining())
148 quint32 numOfIpBlocks = stream.readQUInt32();
150 for (quint32 x = 0; x < numOfIpBlocks; ++x)
155 entry.ipStart = stream.readQUInt32();
156 if (!stream.hasRemaining())
158 entry.ipEnd = stream.readQUInt32();
160 hashTable[entry.ipStart] = entry;
171 IP2CParser::ConstructorDestructorParserStateSetter::ConstructorDestructorParserStateSetter(
IP2CParser *pParser)
173 this->pParser = pParser;
174 pParser->bIsParsing =
true;
177 IP2CParser::ConstructorDestructorParserStateSetter::~ConstructorDestructorParserStateSetter()
179 pParser->bIsParsing =
false;
184 void IP2CParser::ParsingThread::run()
186 bSuccessState = pParser->doReadDatabase(filePath);
void setDatabase(const QList< IP2CData > &sortedCountryData)
Sets database contents to the list specified.
Wraps around QDataStream stream operators to provide cleaner reading interface.
IP2C * pTargetDatabase
Database to which the IP2C parser will save the data it retrieves from IP2C file. ...
IP to Country database handler.
void parsingFinished(bool bSuccess)
A signal emitted when parser finishes its job.