ip2cparser.h
1 //------------------------------------------------------------------------------
2 // ip2cparser.h
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 #ifndef __IP2CPARSER_H__
24 #define __IP2CPARSER_H__
25 
26 #include "ip2c.h"
27 #include <QMutex>
28 #include <QThread>
29 
55 class IP2CParser : public QObject
56 {
57  Q_OBJECT
58 
59 public:
60  IP2CParser();
61 
66  bool isParsing() const
67  {
68  return bIsParsing;
69  }
70 
71  const QList<IP2C::IP2CData> parsed() const
72  {
73  return parsedValues;
74  }
75 
76  bool readDatabase(const QString &filePath);
77  void readDatabaseThreaded(const QString &filePath);
78 
79 signals:
86  void parsingFinished(bool bSuccess);
87 
88 private:
94  class ConstructorDestructorParserStateSetter
95  {
96  public:
97  ConstructorDestructorParserStateSetter(IP2CParser *pParser);
98  ~ConstructorDestructorParserStateSetter();
99 
100  private:
101  IP2CParser *pParser;
102  };
103 
104  class ParsingThread : public QThread
105  {
106  public:
107  bool bSuccessState;
108  QString filePath;
109  IP2CParser *pParser;
110 
111 
112  ParsingThread(IP2CParser *pParser, const QString &filePath)
113  {
114  bSuccessState = false;
115  this->filePath = filePath;
116  this->pParser = pParser;
117  }
118 
119  void run() override;
120  };
121 
125  typedef QHash<QString, QList<IP2C::IP2CData> > Countries;
126  typedef QHash<QString, QList<IP2C::IP2CData> >::iterator CountriesIt;
127  typedef QHash<QString, QList<IP2C::IP2CData> >::const_iterator CountriesConstIt;
128 
129  bool bIsParsing;
130  ParsingThread *currentParsingThread;
131  QList<IP2C::IP2CData> parsedValues;
132 
133  QMutex thisLock;
134 
135  bool doReadDatabase(const QString &filePath);
136  bool readDatabaseVersion2(const QByteArray &dataArray);
137 
138 private slots:
139  void parsingThreadFinished();
140 };
141 
142 #endif