aboutsummaryrefslogtreecommitdiffstats
path: root/tool/PGListUtil/src/common/CErrorList.cpp
blob: 4dce97bc08fce963d3c4e58132a35c3005666dda (plain)
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
#include <stdio.h>
#include <boost/xpressive/xpressive.hpp>
#include "CErrorList.h"

namespace pglu {
namespace error {

CErrorList::CErrorList() :
	m_pool(sizeof(CError)),
	m_errFoot(&m_errHead),
	m_errNext(&m_errHead),
	m_count(0)
{
	m_errHead.line = 0;
	m_errHead.kind = SYNTAX;
	m_errHead.next = NULL;
}

CErrorList::~CErrorList() {
	Clear();
}

void CErrorList::Clear() {
	m_pool.purge_memory();
	m_errHead.next = NULL;
	m_errFoot = &m_errHead;
	m_errNext = &m_errHead;
	m_count = 0;
}

bool CErrorList::LoadListFile(const char *path) {
	char buf[PGLU_LENGTH_FILELINE];
	uint ip_begin1, ip_begin2, ip_begin3, ip_begin4;
	uint ip_end1, ip_end2, ip_end3, ip_end4;
	FILE *fp;
	CError *err = m_errFoot;

	using namespace boost::xpressive;

	static cmatch match;
	static mark_tag tagIp1(1), tagIp2(2), tagIp3(3), tagIp4(4), tagIp5(5), tagIp6(6), tagIp7(7), tagIp8(8);
	static mark_tag tagSep(9), tagMask(10);

	static cregex reSyntax =
		as_xpr(':') >> *_s >>
		(tagIp1 = repeat<1, 3>(_d)) >> as_xpr('.') >>
		(tagIp2 = repeat<1, 3>(_d)) >> as_xpr('.') >>
		(tagIp3 = repeat<1, 3>(_d)) >> as_xpr('.') >>
		(tagIp4 = repeat<1, 3>(_d)) >>
		*_s >> as_xpr('-') >> *_s >>
		(tagIp5 = repeat<1, 3>(_d)) >> as_xpr('.') >>
		(tagIp6 = repeat<1, 3>(_d)) >> as_xpr('.') >>
		(tagIp7 = repeat<1, 3>(_d)) >> as_xpr('.') >>
		(tagIp8 = repeat<1, 3>(_d)) >>
		*_s >> (_ln | eos);

	static cregex reSyntaxRestorable =
		(tagIp1 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
		(tagIp2 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
		(tagIp3 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
		(tagIp4 = repeat<1, 3>(_d)) >> *_s >> (
			(tagSep = +as_xpr('-')) >> *_s >>
			(tagIp5 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
			(tagIp6 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
			(tagIp7 = repeat<1, 3>(_d)) >> (set = '.', ',') >>
			(tagIp8 = repeat<1, 3>(_d))
		|
			(tagSep = +(set = '/', '\\')) >> *_s >>
			(tagMask = repeat<1, 2>(_d))
		) >> *_s >> (_ln | eos);

#define reIpError repeat<3>(+_d >> *_s >> (set = '.', ',') >> *_s) >> +_d

	static cregex reSyntaxError =
		reIpError >> *_s >> (
			*as_xpr('-') >> *_s >> reIpError |
			+(set = '/', '\\') >> *_s >> +_d
		);

	fp = fopen(path, "r");
	if(fp == NULL)
		return false;

	for(int line = 1; fgets(buf, PGLU_LENGTH_FILELINE, fp); ++line) {

		if(regex_search(buf, match, reSyntax)) {
			if(!(
				(ip_begin1 = ParseDigit3(match[1].first, match[1].second)) < 256 &&
				(ip_begin2 = ParseDigit3(match[2].first, match[2].second)) < 256 &&
				(ip_begin3 = ParseDigit3(match[3].first, match[3].second)) < 256 &&
				(ip_begin4 = ParseDigit3(match[4].first, match[4].second)) < 256 &&
				(ip_end1 = ParseDigit3(match[5].first, match[5].second)) < 256 &&
				(ip_end2 = ParseDigit3(match[6].first, match[6].second)) < 256 &&
				(ip_end3 = ParseDigit3(match[7].first, match[7].second)) < 256 &&
				(ip_end4 = ParseDigit3(match[8].first, match[8].second)) < 256 &&
				(uint)((ip_begin1 << 24) | (ip_begin2 << 16) | (ip_begin3 << 8) | ip_begin4) <=
				(uint)((ip_end1 << 24) | (ip_end2 << 16) | (ip_end3 << 8) | ip_end4)
			)) {
				++m_count;
				err->next = (CError*)m_pool.malloc();
				err = err->next;
				err->line = line;
				err->kind = IP;
			}

		} else if(regex_search(buf, match, reSyntaxRestorable)) {
			++m_count;
			err->next = (CError*)m_pool.malloc();
			err = err->next;
			err->line = line;
			if(*(match[9].first) == '-') {
				if(
					(ip_begin1 = ParseDigit3(match[1].first, match[1].second)) < 256 &&
					(ip_begin2 = ParseDigit3(match[2].first, match[2].second)) < 256 &&
					(ip_begin3 = ParseDigit3(match[3].first, match[3].second)) < 256 &&
					(ip_begin4 = ParseDigit3(match[4].first, match[4].second)) < 256 &&
					(ip_end1 = ParseDigit3(match[5].first, match[5].second)) < 256 &&
					(ip_end2 = ParseDigit3(match[6].first, match[6].second)) < 256 &&
					(ip_end3 = ParseDigit3(match[7].first, match[7].second)) < 256 &&
					(ip_end4 = ParseDigit3(match[8].first, match[8].second)) < 256 &&
					(uint)((ip_begin1 << 24) | (ip_begin2 << 16) | (ip_begin3 << 8) | ip_begin4) <=
					(uint)((ip_end1 << 24) | (ip_end2 << 16) | (ip_end3 << 8) | ip_end4)
				) {
					err->kind = SYNTAX_RESTORABLE;
				} else {
					err->kind = SYNTAX;
				}
			} else {
				uint mask = ParseDigit3(match[10].first, match[10].second);
				if(
					ParseDigit3(match[1].first, match[1].second) < 256 &&
					ParseDigit3(match[2].first, match[2].second) < 256 &&
					ParseDigit3(match[3].first, match[3].second) < 256 &&
					ParseDigit3(match[4].first, match[4].second) < 256 &&
					mask < 33
				) {
					err->kind = SYNTAX_RESTORABLE;
				} else {
					err->kind = SYNTAX;
				}
			}
		} else if(regex_search(buf, reSyntaxError)) {
			++m_count;
			err->next = (CError*)m_pool.malloc();
			err = err->next;
			err->line = line;
			err->kind = SYNTAX;
		}
#ifdef __MINGW32__
		ZeroString(buf);
#endif
	}

	fclose(fp);
	err->next = NULL;
	m_errFoot = err;
	return true;
}

int CErrorList::Count() {
	return m_count;
}

CError * CErrorList::GetNext() {
	if(m_errNext)
		m_errNext = m_errNext->next;
	return m_errNext;
}

} // namespace error
} // namespace pglu