File: pgsRegexGen.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (309 lines) | stat: -rw-r--r-- 6,669 bytes parent folder | download
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//////////////////////////////////////////////////////////////////////////
//
// pgScript - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////////////////


#include "pgAdmin3.h"
#include "pgscript/generators/pgsRegexGen.h"

#include <wx/sstream.h>
#include <wx/regex.h>
#include <wx/xml/xml.h>

#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY(pgsVectorStringGen);

pgsRegexGen::pgsRegex::pgsRegex(const pgsVectorChar &characters,
                                const long &first, const long &second) :
	m_characters(characters), m_first(wxMin(first, second)),
	m_second(wxMax(first, second))
{

}

pgsRegexGen::pgsRegex::pgsRegex() :
	m_characters(pgsVectorChar()), m_first(0), m_second(0)
{

}

pgsRegexGen::pgsRegex::~pgsRegex()
{

}

pgsRegexGen::pgsRegex *pgsRegexGen::pgsRegex::clone()
{
	return pnew pgsRegexGen::pgsRegex(*this);
}

void pgsRegexGen::pgsRegex::set_characters(const pgsVectorChar &characters)
{
	m_characters = characters;
}

void pgsRegexGen::pgsRegex::add_character(const wxChar &c)
{
	m_characters.Add(c);
}

void pgsRegexGen::pgsRegex::set_first(const long &first)
{
	m_first = first;
	m_second = first;
}

void pgsRegexGen::pgsRegex::set_second(const long &second)
{
	long first = m_first;
	m_first = wxMin(first, second);
	m_second = wxMax(first, second);
}

const pgsVectorChar &pgsRegexGen::pgsRegex::get_characters() const
{
	return m_characters;
}

const long &pgsRegexGen::pgsRegex::get_first() const
{
	return m_first;
}

const long &pgsRegexGen::pgsRegex::get_second() const
{
	return m_second;
}

pgsRegexGen::pgsRegexGen(const wxString &regex, const long &seed) :
	pgsObjectGen(seed), m_regex(regex), m_valid(true), m_string_gens(pgsVectorStringGen())
{
	wxLogScriptVerbose(wxT("REGEXGEN: %s"), m_regex.c_str());

	// Transform regular expression into XML structure
	bool escape = false, first_regex = true, list = false;
	wxString result = wxT("<regexpressions>\n");
	size_t i = 0;
	while (i < m_regex.Length())
	{
		if (escape)
		{
			if (list == true)
			{
				result.Append(espace_xml_char(m_regex[i]));
			}
			else
			{
				if (!first_regex)
					result.Append(wxT(" </regex>\n"));
				else
					first_regex = false;
				result.Append(wxT(" <regex>\n  <characters>"));
				result.Append(espace_xml_char(m_regex[i]));
				result.Append(wxT("</characters>\n"));
			}
			escape = false;
		}
		else if (list == true && m_regex[i] == wxT('-'))
		{
			if ((i + 1) < m_regex.Length())
			{
				result.Append(char_range(m_regex[i - 1], m_regex[i + 1]));
			}
		}
		else if (m_regex[i] == wxT('['))
		{
			if (!first_regex)
				result.Append(wxT(" </regex>\n"));
			else
				first_regex = false;
			result.Append(wxT(" <regex>\n  <characters>"));
			list = true;
		}
		else if (m_regex[i] == wxT(']'))
		{
			result.Append(wxT("</characters>\n"));
			list = false;
		}
		else if (m_regex[i] == wxT('{'))
		{
			result.Append(wxT("  <range>"));
			list = true;
		}
		else if (m_regex[i] == wxT('}'))
		{
			result.Append(wxT("</range>\n"));
			list = false;
		}
		else if (m_regex[i] == wxT('\\'))
		{
			escape = true;
		}
		else
		{
			if (list == true)
			{
				result.Append(espace_xml_char(m_regex[i]));
			}
			else
			{
				if (!first_regex)
					result.Append(wxT(" </regex>\n"));
				else
					first_regex = false;
				result.Append(wxT(" <regex>\n  <characters>"));
				result.Append(espace_xml_char(m_regex[i]));
				result.Append(wxT("</characters>\n"));
			}
		}

		++i;
	}
	if (result != wxT("<regexpressions>\n"))
		result.Append(wxT(" </regex>\n"));
	result.Append(wxT("</regexpressions>\n"));

	wxLogScriptVerbose(wxT("REGEXGEN: %s"), result.c_str());

	// Load this XML structure with the wxXmlDocument from wxWidgets
	wxStringInputStream input(result);
	wxXmlDocument doc;
	if (!doc.Load(input, wxT("UTF-8"), wxXMLDOC_KEEP_WHITESPACE_NODES))
	{
		m_valid = false;
	}
	else
	{
		// Start processing the XML file
		if (doc.GetRoot()->GetName() != wxT("regexpressions"))
		{
			m_valid = false;
		}
		else
		{
			// Go through XML nodes
			wxXmlNode *xml_regexpressions = doc.GetRoot()->GetChildren();
			while (xml_regexpressions && m_valid)
			{
				if (xml_regexpressions->GetName() == wxT("regex"))
				{
					wxXmlNode *xml_regex = xml_regexpressions->GetChildren();

					pgsRegex regex;
					regex.set_first(1);

					while (xml_regex && m_valid)
					{
						if (xml_regex->GetName() == wxT("characters"))
						{
							wxString content = xml_regex->GetNodeContent();
							for (size_t i = 0; i < content.Length(); i++)
							{
								regex.add_character(content[i]);
							}
						}
						else if (xml_regex->GetName() == wxT("range"))
						{
							wxString content = xml_regex->GetNodeContent();
							wxRegEx ex(wxT("^([0-9]+)(,([0-9]+))?$"));
							wxASSERT(ex.IsValid());
							if (ex.Matches(content))
							{
								long min;
								ex.GetMatch(content, 1).ToLong(&min);
								regex.set_first(min);
								wxString smax = ex.GetMatch(content, 3);
								if (!smax.IsEmpty())
								{
									long max;
									smax.ToLong(&max);
									regex.set_second(max);
								}
							}
							else
							{
								// m_valid = false;
							}
						}

						xml_regex = xml_regex->GetNext();
					}

					m_string_gens.Add(pgsStringGen(regex.get_first(),
					                               regex.get_second(), 1, seed,
					                               regex.get_characters()));
				}

				xml_regexpressions = xml_regexpressions->GetNext();
			}
		}
	}
}

wxString pgsRegexGen::random()
{
	wxString result;
	for (size_t i = 0; i < string_gens_size(); i++)
	{
		result.Append(m_string_gens.Item(i).random());
	}
	return result;
}

const pgsVectorStringGen &pgsRegexGen::string_gens() const
{
	return m_string_gens;
}

size_t pgsRegexGen::string_gens_size() const
{
	return m_string_gens.size();
}

const bool &pgsRegexGen::is_valid() const
{
	return m_valid;
}

wxString pgsRegexGen::espace_xml_char(const wxChar &c)
{
	if (c == wxT('<'))
		return wxT("&lt;");
	else if (c == wxT('&'))
		return wxT("&amp;");
	else if (c == wxT('>'))
		return wxT("&gt;");
	else if (c == wxT('"'))
		return wxT("&quot;");
	else if (c == wxT('\''))
		return wxT("&apos;");
	else return wxString(c);
}

wxString pgsRegexGen::char_range(const wxChar &b, const wxChar &c)
{
	wxChar min = wxMin(b, c);
	++min;
	wxChar max = wxMax(b, c);
	wxString result;
	for (wxChar i = min; i < max; i++)
	{
		result.Append(espace_xml_char(i));
	}
	return result;
}

pgsRegexGen::~pgsRegexGen()
{

}

pgsRegexGen *pgsRegexGen::clone()
{
	return pnew pgsRegexGen(*this);
}