ip2cparser.h
1 //------------------------------------------------------------------------------
2 // ip2cparser.h
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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 "Blzut3" <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #ifndef __IP2CPARSER_H__
24 #define __IP2CPARSER_H__
25 
26 #include "ip2c.h"
27 #include <QMutex>
28 #include <QThread>
29 
81 class IP2CParser : public QObject
82 {
83  Q_OBJECT
84 
85  public:
87 
91  IP2C* ip2c() const { return pTargetDatabase; }
92 
97  bool isParsing() const { return bIsParsing; }
98 
99  bool readDatabase(const QString& filePath);
100  void readDatabaseThreaded(const QString& filePath);
101 
102  signals:
109  void parsingFinished(bool bSuccess);
110 
111  protected:
118  {
119  public:
122 
123  private:
124  IP2CParser* pParser;
125  };
126 
127  class ParsingThread : public QThread
128  {
129  public:
130  bool bSuccessState;
131  QString filePath;
132  IP2CParser* pParser;
133 
134 
135  ParsingThread(IP2CParser* pParser, const QString& filePath)
136  {
137  bSuccessState = false;
138  this->filePath = filePath;
139  this->pParser = pParser;
140  }
141 
142  void run();
143  };
144 
148  typedef QHash<QString, QList<IP2C::IP2CData> > Countries;
149  typedef QHash<QString, QList<IP2C::IP2CData> >::iterator CountriesIt;
150  typedef QHash<QString, QList<IP2C::IP2CData> >::const_iterator CountriesConstIt;
151 
152  bool bIsParsing;
153  ParsingThread* currentParsingThread;
154 
163 
164  QMutex thisLock;
165 
166 
171  bool convertAndSaveDatabase(QByteArray& downloadedData, const QString& outFilePath);
172 
177  void convertCountriesIntoBinaryData(const Countries& countries, QByteArray& output);
178 
179  bool doReadDatabase(const QString& filePath);
180 
181  bool readDatabaseVersion1(const QByteArray& dataArray);
182  bool readDatabaseVersion2(const QByteArray& dataArray);
183 
190  void readTextDatabase(QByteArray& textDatabase, Countries& countries);
191 
192  protected slots:
193  void parsingThreadFinished();
194 };
195 
196 #endif
Sets states for IP2C when being constructed and destructed.
Definition: ip2cparser.h:117
IP2C * pTargetDatabase
Database to which the IP2C parser will save the data it retrieves from IP2C file. ...
Definition: ip2cparser.h:162
void convertCountriesIntoBinaryData(const Countries &countries, QByteArray &output)
Definition: ip2cparser.cpp:64
bool isParsing() const
For multi-threading purposes. If this is true it is not recommended to delete this object nor the und...
Definition: ip2cparser.h:97
IP to Country database handler.
Definition: ip2c.h:48
QHash< QString, QList< IP2C::IP2CData > > Countries
Definition: ip2cparser.h:148
void parsingFinished(bool bSuccess)
A signal emitted when parser finishes its job.
void readTextDatabase(QByteArray &textDatabase, Countries &countries)
Definition: ip2cparser.cpp:300
IP2C * ip2c() const
Retrieves the IP2C database this parser operates on.
Definition: ip2cparser.h:91
bool convertAndSaveDatabase(QByteArray &downloadedData, const QString &outFilePath)
Definition: ip2cparser.cpp:40