-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathparser.h
295 lines (264 loc) · 7.6 KB
/
parser.h
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Copyright 2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
* or its licensors, as applicable.
* Copyright 2008-2015 Markus Goldstein
*
* You may not use this file except under the terms of the accompanying license.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project: ip-countryside
* File: parser.h
* Purpose: Header file for building the ip2country database
* Responsible: Markus Goldstein
* Primary Repository: https://github.com/Markus-Go/ip-countryside/
* Web Sites: madm.dfki.de, www.goldiges.de/ip-countryside
*
*/
#ifndef PARSER_H_
#define PARSER_H_
#include <ctype.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <list>
#include <vector>
#include <deque>
#include <set>
#include <cstring>
#include <cstdlib>
#include <climits>
using namespace std;
#define LINE_LENGTH 4096
void dbParser( string dbIn , string delName, string dbOut );
void delegParser( string , string );
// contains a range with country [124.10.187.65-124.30.121.12-JP]
class DBEntries {
public:
unsigned int ipFrom;
unsigned int ipTo;
char country[2];
DBEntries() {};
DBEntries( const DBEntries& objToCopy ) :
ipFrom( objToCopy.ipFrom ),
ipTo( objToCopy.ipTo )
{
memcpy( country, objToCopy.country, 2 );
};
};
// contains a range with country and registry [124.10.187.65-124.30.121.12-JP-APNIC]
class DBRangeEntries {
public:
unsigned int ipFrom;
unsigned int ipTo;
char country[2];
string registry;
DBRangeEntries() {};
DBRangeEntries( const DBRangeEntries& objToCopy ) :
ipFrom( objToCopy.ipFrom ),
ipTo( objToCopy.ipTo ),
registry( objToCopy.registry )
{
memcpy( country, objToCopy.country, 2 );
};
};
// conatins single ip-country [124.12.10.122-AU]
struct net {
char country[2];
unsigned int ip;
unsigned int size;
bool bSetFlag;
};
// conatins a delegation file range [124/8] = [124.0.0.0-124.255.255.255]
struct range {
unsigned int ipFrom;
unsigned int ipTo;
};
/*
* the comp structure define the sorting strategy.
* is used in combination with Set(Associative Container) to sort the elements
* in the set based on the range size of the elements.
*/
struct comp {
bool operator () ( const DBEntries e, const DBEntries e1 ) const {
if ( (e.ipTo - e.ipFrom) == (e1.ipTo - e1.ipFrom) )
return (e.ipFrom <= e1.ipFrom);
else
return ((e.ipTo - e.ipFrom) < (e1.ipTo - e1.ipFrom) );
}
};
/*
* the compare structure define the sorting strategy.
* is used in combination with Set to sort the elements
* in the Set based on 1. ipFrom value if ipFrom is the same 2.based
* on ipTo.
*/
struct compare {
bool operator () ( const DBRangeEntries e, const DBRangeEntries e1 ) const {
if ( e.ipFrom == e1.ipFrom ) {
if ( e.ipTo == e1.ipTo ) {
if ( e.country[0] == e1.country[0] )
return e.country[1] < e1.country[1];
else
return e.country[0] < e1.country[0];
}
else
return (e.ipTo <= e1.ipTo);
}
else
return ( e.ipFrom < e1.ipFrom );
}
};
struct compare_entries {
bool operator () ( const DBEntries e, const DBEntries e1 ) const {
if ( e.ipFrom == e1.ipFrom ) {
if ( e.ipTo == e1.ipTo ) {
if ( e.country[0] == e1.country[0] )
return e.country[1] < e1.country[1];
else
return e.country[0] < e1.country[0];
}
else
return (e.ipTo <= e1.ipTo);
}
else
return ( e.ipFrom < e1.ipFrom );
}
};
struct compare_structure {
bool operator () ( const struct range e, const struct range e1 ) const {
if ( e.ipFrom == e1.ipFrom ) {
if ( e.ipTo == e1.ipTo ) {
return false;
}
else
return (e.ipTo <= e1.ipTo);
}
else
return ( e.ipFrom < e1.ipFrom );
}
};
// Tests if this string starts with the specified prefix
int startWith( string sString ,string sValue ) {
string::size_type loc;
loc = sString.find(sValue,0);
if ( loc == 0 )
return 1;
else
return 0;
}
// Tests if this string contain the specified string
int contain( string sString ,string sValue ) {
string::size_type loc;
loc = sString.find(sValue,0);
if ( loc != string::npos )
return 1;
else
return 0;
}
// Splits this string around matches of the given character
vector <string> split ( string sStr, char cValue ) {
string sString ;
vector < string > vStr;
if (strchr(sStr.c_str(), cValue) == NULL) {
return vStr;
}
istringstream iss(sStr);
size_t pos;
while ( getline(iss, sString, cValue) ) {
while( (pos = sString.find('\t')) != string::npos) {
sString = sString.substr(0,pos) + sString.substr(pos+1);
}
vStr.push_back(sString);
}
return vStr;
}
// Returns a copy of the string, with no whitespace.
string trim( string str ) {
size_t pos;
while( (pos = str.find(' ')) != string::npos) {
str = str.substr(0,pos) + str.substr(pos+1);
}
return str;
}
// Convert an IP of dot format to an integer value
unsigned int convertToInt ( vector < string > octet) {
unsigned int value = 0;
value = (atoi ( octet[0].c_str() ) * 16777216) +
(atoi ( octet[1].c_str() ) * 65536) +
(atoi ( octet[2].c_str() ) * 256) +
atoi ( octet[3].c_str() );
return value;
}
// Convert an integer value to a dotted IP format
string convertIP( unsigned int ip ) {
stringstream str;
unsigned int oct1 = 0, oct2 = 0, oct3 = 0, oct4 = 0;
oct1 = (ip / 16777216);
ip %= 16777216;
oct2 = (ip / 65536);
ip %= 65536;
oct3 = (ip / 256);
ip %= 256;
oct4 = ip;
str << oct1 << "."<<oct2<<"."<<oct3<<"."<<oct4;
return str.str();
}
// Converts all of the characters in this String to upper case.
string stringToUpper ( string str ) {
for ( unsigned int i = 0 ; i < str.size() ; i++ ) {
str[i] = toupper( str[i] );
}
return str;
}
// this vector contains the files path
vector <struct keyValue> vConfigFile;
// Holds the key-value pairs of the path
struct keyValue {
string key;
string value;
};
// read the configFile
vector <struct keyValue> readConfig ( string sFileName ) {
// cout << "start reading config file \n";
char cBuf [256];
string str;
vector <string> vStr;
vector <struct keyValue> vValues;
ifstream in (sFileName.c_str());
if (!in ) {
cout << sFileName << " File can NOT be opened ! \n";
exit(1);
}
while ( !(in.getline(cBuf,LINE_LENGTH).eof()) ) {
str = cBuf;
struct keyValue * entry = new struct keyValue;
vStr = split( str,'=' );
entry->key = vStr[0];
entry->value = vStr[1];
vValues.push_back( *entry );
}
return vValues;
}
// look for a key in the configFile
string findValue( string key ) {
string value = " ";
for ( unsigned int i = 0; i < vConfigFile.size(); i++ ) {
if ( key == vConfigFile[i].key ) {
return value = vConfigFile[i].value;
}
}
cout << key << " This is invalid key \n";
exit(1);
}
class Parser {
public:
Parser();
virtual ~Parser();
};
#endif /*PARSER_H_*/