1 /******************************************************************************
2 *
3 * Copyright (C) 2009, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * @Author: Alexander Antypenko ( santy ) v. 1.00
11 *
12 ******************************************************************************/
13 define
14 {
15 /* Options */
16 PCRE_CASELESS =0x0001
17 PCRE_MULTILINE =0x0002
18 PCRE_DOTALL =0x0004
19 PCRE_EXTENDED =0x0008
20 PCRE_ANCHORED =0x0010
21 PCRE_DOLLAR_ENDONLY =0x0020
22 PCRE_EXTRA =0x0040
23 PCRE_NOTBOL =0x0080
24 PCRE_NOTEOL =0x0100
25 PCRE_UNGREEDY =0x0200
26 PCRE_NOTEMPTY =0x0400
27 PCRE_UTF8 =0x0800
28 PCRE_NO_AUTO_CAPTURE =0x1000
29 PCRE_NO_UTF8_CHECK =0x2000
30
31 /* Exec-time and get/set-time error codes */
32
33 PCRE_ERROR_NOMATCH = -1
34 PCRE_ERROR_NULL = -2
35 PCRE_ERROR_BADOPTION = -3
36 PCRE_ERROR_BADMAGIC = -4
37 PCRE_ERROR_UNKNOWN_NODE = -5
38 PCRE_ERROR_NOMEMORY = -6
39 PCRE_ERROR_NOSUBSTRING = -7
40 PCRE_ERROR_MATCHLIMIT = -8
41 PCRE_ERROR_CALLOUT = -9 /* Never used by PCRE itself */
42 PCRE_ERROR_BADUTF8 =-10
43
44 /* Request types for pcre_fullinfo() */
45
46 PCRE_INFO_OPTIONS =0
47 PCRE_INFO_SIZE =1
48 PCRE_INFO_CAPTURECOUNT =2
49 PCRE_INFO_BACKREFMAX =3
50 PCRE_INFO_FIRSTBYTE =4
51 PCRE_INFO_FIRSTCHAR =4 /* For backwards compatibility */
52 PCRE_INFO_FIRSTTABLE =5
53 PCRE_INFO_LASTLITERAL =6
54 PCRE_INFO_NAMEENTRYSIZE =7
55 PCRE_INFO_NAMECOUNT =8
56 PCRE_INFO_NAMETABLE =9
57 PCRE_INFO_STUDYSIZE =10
58
59 /* Request types for pcre_config() */
60
61 PCRE_CONFIG_UTF8 =0
62 PCRE_CONFIG_NEWLINE =1
63 PCRE_CONFIG_LINK_SIZE =2
64 PCRE_CONFIG_POSIX_MALLOC_THRESHOLD =3
65 PCRE_CONFIG_MATCH_LIMIT =4
66
67 /* Bit flags for the pcre_extra structure */
68
69 PCRE_EXTRA_STUDY_DATA =0x0001
70 PCRE_EXTRA_MATCH_LIMIT =0x0002
71 PCRE_EXTRA_CALLOUT_DATA =0x0004
72 }
73
74 define
75 {
76 OSIZE = 30
77 NULL = 0
78 }
79
80 import "pcre.dll"
81 {
82 uint pcre_malloc(uint);
83 pcre_free(uint);
84 regfree(uint);
85 uint pcre_callout(uint);
86
87
88 uint pcre_compile(uint,uint,uint,uint,uint)
89 int pcre_config(uint, uint)
90 int pcre_copy_named_substring(uint,uint,uint , uint,uint,uint, uint)
91 int pcre_copy_substring(uint, uint , uint, uint,uint , uint);
92 int pcre_exec(uint,uint,uint, uint, uint, uint, uint , uint);
93 pcre_free_substring(uint);
94 pcre_free_substring_list(uint);
95 int pcre_fullinfo(uint,uint, uint,uint);
96 int pcre_get_named_substring(uint,uint,uint , uint,uint,uint);
97 int pcre_get_stringnumber(uint,uint);
98 int pcre_get_substring(uint,uint,uint,uint,uint);
99 int pcre_get_substring_list(uint, uint , uint,uint);
100 int pcre_info(uint, uint , uint );
101 uint pcre_maketables();
102 uint pcre_study(uint, uint, uint);
103 uint pcre_version();
104
105 }
106
107 func str_print(str sPrn_str)
108 {
109 print(sPrn_str);
110 }
111
112 type pcre_extra
113 {
114 ulong flags; /* Bits for which fields are set */
115 uint study_data; /* Opaque data from pcre_study() */
116 ulong match_limit; /* Maximum number of calls to match() */
117 uint callout_data; /* Data passed back in callouts */
118 }
119
120
121 type pcre
122 {
123 str error_message
124 str src_text
125 }
126
127 /*-----------------------------------------------------------------------------
128 * @syntax [ pcre1.re_version () ]
129 *
130 * @return Get version of pcre library.
131 *
132 * @example
133 * [ str str_version = pcre1.re_version() ]
134 -----------------------------------------------------------------------------*/
135 method str pcre.re_version <result>()
136 {
137 str sVersion;
138 result = sVersion.copy(pcre_version());
139 }
140
141 /*-----------------------------------------------------------------------------
142 * @syntax [ pcre1.srcText = sText ]
143 *
144 * Property srcText sets source text data for parsing (set method)
145 -----------------------------------------------------------------------------*/
146 property pcre.srcText(str sText) : this.src_text = sText
147
148 /*-----------------------------------------------------------------------------
149 * @syntax [ str pcre1.srcText = sText ]
150 *
151 * Property srcText Get source text data for parsing (get method)
152 -----------------------------------------------------------------------------*/
153 property str pcre.srcText() : return this.src_text
154
155 /*-----------------------------------------------------------------------------
156 * @syntax [ pcre1.errorMessage = sText ]
157 *
158 * Property errorMessage sets error message text data (set method)
159 -----------------------------------------------------------------------------*/
160 property pcre.errorMessage(str sText) : this.error_message = sText
161 /*-----------------------------------------------------------------------------
162 * @syntax [ str pcre1.errorMessage = sText ]
163 *
164 * Property errorMessage Get error message text data (get method)
165 -----------------------------------------------------------------------------*/
166 property str pcre.errorMessage() : return this.error_message
167
168
169 method int pcre.errorAnalize(int nMatch)
170 {
171 switch nMatch
172 {
173 case $PCRE_ERROR_NOMATCH : return 0
174 case $PCRE_ERROR_NULL : this.errorMessage = "Pcre ERROR NULL" ; return -1
175 case $PCRE_ERROR_BADOPTION : this.errorMessage = "Pcre ERROR BAD OPTION" ; return -1
176 case $PCRE_ERROR_BADMAGIC : this.errorMessage = "Pcre ERROR BAD MAGIC" ; return -1
177 case $PCRE_ERROR_UNKNOWN_NODE : this.errorMessage = "Pcre ERROR UNKNOWN NODE"; return -1
178 case $PCRE_ERROR_NOMEMORY : this.errorMessage = "Pcre ERROR NO MEMORY" ; return -1
179 case $PCRE_ERROR_MATCHLIMIT : this.errorMessage = "Pcre ERROR MATCH LIMIT" ; return -1
180 }
181 return nMatch
182 }
183
184 /*-----------------------------------------------------------------------------
185 * @syntax [ pcre1.matchGetAll(str sPattern,int iOption, arrstr s ) ]
186 *
187 * @param sPattern zero-terminated string containing the regular expression to be compiled
188 * @param iOption zero or more option bits
189 * @param saReturn strings array where to put resuld data
190 *
191 * @return Count matched strings.
192 *
193 * @example
194 * [ arrstr allData
195 * sPattern = "^\\s*([^:])"
196 * int iCount = reData.matchGetAll (sPattern, $PCRE_NOTEMPTY,allData)
197 * ]
198 -----------------------------------------------------------------------------*/
199 method int pcre.matchGetAll(str sPattern,int iOption, arrstr saReturn )
200 {
201 arr ovector[$OSIZE] of uint;
202 uint erroffset,ptrRegex,iAddrStr;
203 str errorptr,sConv;
204 int numMatch,iReslist
205
206 ptrRegex = pcre_compile (sPattern.ptr(), $NULL, errorptr.ptr(), &erroffset, $NULL)
207 if ( ptrRegex == $NULL )
208 {
209 sConv.out4("%d",erroffset)
210 this.errorMessage = "Error compiling pattern" + sPattern + " at offset" + sConv + " - " //+ errorptr.setlenptr()//copy(errorptr.ptr(),mlen(errorptr.ptr()+1));
211 return - 1
212 }
213 numMatch = pcre_exec(ptrRegex, 0, this.srcText.ptr(),*this.srcText, 0, iOption, ovector.ptr(), *ovector )
214 if (numMatch == $NULL)
215 {
216 this.errorMessage = "OSIZE value is too small!"
217 regfree (ptrRegex)
218 return - 1
219 }
220 int iCode = this.errorAnalize(numMatch)
221 if (iCode <= 0) : return iCode
222 uint i;
223 str sTekStr,sdemo;
224 fornum i=1, numMatch
225 {
226 sTekStr.clear()
227 //print(" From \(ovector[2*i] ) len \(ovector[2*i+1] - ovector[2*i]) \n ")
228 sTekStr.substr(this.srcText,ovector[2*i],ovector[2*i+1] - ovector[2*i])
229 saReturn += sTekStr;
230 }
231 regfree (ptrRegex)
232 return numMatch;
233 }
234
235
236 /*-----------------------------------------------------------------------------
237 * @syntax [ pcre1.match(str sPattern,int iOption) ]
238 *
239 * @param sPattern zero-terminated string containing the regular expression to be compiled
240 * @param iOption zero or more option bits
241 *
242 * @return Count matched strings.
243 *
244 * @example
245 * [
246 * sPattern = "^\\s*([^:])"
247 * int iCount = reData.match(sPattern, $PCRE_NOTEMPTY)
248 * ]
249 -----------------------------------------------------------------------------*/
250 method int pcre.match(str sPattern,int iOption)
251 {
252 arr ovector[$OSIZE] of uint;
253 uint erroffset,ptrRegex;
254 str errorptr, sConv;
255 int numMatch;
256
257 ptrRegex = pcre_compile(sPattern.ptr(), $NULL, errorptr.ptr(), &erroffset, $NULL)
258 if (ptrRegex == $NULL)
259 {
260 sConv.out4("%d",erroffset)
261 this.errorMessage = "Error compiling pattern "+sPattern+" at offset"+ sConv + " - "+ errorptr.setlenptr()
262 return - 1;
263
264 }
265 numMatch = pcre_exec (ptrRegex, 0, this.srcText.ptr(),*this.srcText, 0, iOption, ovector.ptr(), *ovector )
266 if (numMatch == $NULL)
267 {
268 this.errorMessage = "OSIZE value is too small!";
269 regfree (ptrRegex);
270 return - 1;
271 }
272
273 int iCode = this.errorAnalize(numMatch)
274 if (iCode <= 0) : return iCode
275
276 regfree (ptrRegex)
277 return numMatch;
278 }
279
280