PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/donations/source/common/pcre.pas

https://github.com/the-Arioch/jcl
Pascal | 345 lines | 190 code | 31 blank | 124 comment | 3 complexity | 05a189ff77f67f0d1c49930b144ed7d5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1{**************************************************************************************************}
  2{                                                                                                  }
  3{ Project JEDI Code Library (JCL)                                                                  }
  4{                                                                                                  }
  5{ The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); }
  6{ you may not use this file except in compliance with the License. You may obtain a copy of the    }
  7{ License at http://www.mozilla.org/MPL/                                                           }
  8{                                                                                                  }
  9{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF   }
 10{ ANY KIND, either express or implied. See the License for the specific language governing rights  }
 11{ and limitations under the License.                                                               }
 12{                                                                                                  }
 13{ The Original Code is JclPRCE.pas.                                                                }
 14{                                                                                                  }
 15{ The Initial Developer of the Original Code is documented in the accompanying                     }
 16{ help file JCL.chm. Portions created by these individuals are Copyright (C) of these individuals. }
 17{ Portions created by University of Cambridge are                                                  }
 18{ Copyright (C) 1997-2001 by University of Cambridge.                                              }
 19{                                                                                                  }
 20{The latest release of PCRE is always available from                                               }
 21{ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-xxx.tar.gz                             }
 22{**************************************************************************************************}
 23{                                                                                                  }
 24{ Header conversion of pcre.h                                                                      }
 25{                                                                                                  }
 26{ Unit owner: Peter Thornqvist                                                                     }
 27{ Last modified: April 30, 2004                                                                    }
 28{                                                                                                  }
 29{**************************************************************************************************}
 30//$Id$
 31
 32unit pcre;
 33
 34interface
 35(*************************************************
 36*       Perl-Compatible Regular Expressions      *
 37*************************************************)
 38
 39{$WEAKPACKAGEUNIT ON}
 40
 41// (p3) this is the switch to change between static and dynamic linking.
 42// It is set to dynamic by default. To disable simply insert a '.' before the '$'
 43//
 44// NOTE: if you enable static linking of DLL, this means that the pcre.dll *must*
 45// be in the users path or an AV will occur at startup
 46
 47{$DEFINE PCRE_LINKONREQUEST}
 48(*$HPPEMIT '#include "pcre.h"'*)
 49
 50const
 51  MAX_PATTERN_LENGTH = $10003;
 52{$EXTERNALSYM MAX_PATTERN_LENGTH}
 53  MAX_QUANTIFY_REPEAT = $10000;
 54{$EXTERNALSYM MAX_QUANTIFY_REPEAT}
 55  MAX_CAPTURE_COUNT = $FFFF;
 56{$EXTERNALSYM MAX_CAPTURE_COUNT}
 57  MAX_NESTING_DEPTH = 200;
 58{$EXTERNALSYM MAX_NESTING_DEPTH}
 59
 60  (* Options *)
 61const
 62  PCRE_CASELESS = $0001;
 63{$EXTERNALSYM PCRE_CASELESS}
 64  PCRE_MULTILINE = $0002;
 65{$EXTERNALSYM PCRE_MULTILINE}
 66  PCRE_DOTALL = $0004;
 67{$EXTERNALSYM PCRE_DOTALL}
 68  PCRE_EXTENDED = $0008;
 69{$EXTERNALSYM PCRE_EXTENDED}
 70  PCRE_ANCHORED = $0010;
 71{$EXTERNALSYM PCRE_ANCHORED}
 72  PCRE_DOLLAR_ENDONLY = $0020;
 73{$EXTERNALSYM PCRE_DOLLAR_ENDONLY}
 74  PCRE_EXTRA = $0040;
 75{$EXTERNALSYM PCRE_EXTRA}
 76  PCRE_NOTBOL = $0080;
 77{$EXTERNALSYM PCRE_NOTBOL}
 78  PCRE_NOTEOL = $0100;
 79{$EXTERNALSYM PCRE_NOTEOL}
 80  PCRE_UNGREEDY = $0200;
 81{$EXTERNALSYM PCRE_UNGREEDY}
 82  PCRE_NOTEMPTY = $0400;
 83{$EXTERNALSYM PCRE_NOTEMPTY}
 84  PCRE_UTF8 = $0800;
 85{$EXTERNALSYM PCRE_UTF8}
 86
 87  (* Exec-time and get-time error codes *)
 88
 89  PCRE_ERROR_NOMATCH = -1;
 90{$EXTERNALSYM PCRE_ERROR_NOMATCH}
 91  PCRE_ERROR_NULL = -2;
 92{$EXTERNALSYM PCRE_ERROR_NULL}
 93  PCRE_ERROR_BADOPTION = -3;
 94{$EXTERNALSYM PCRE_ERROR_BADOPTION}
 95  PCRE_ERROR_BADMAGIC = -4;
 96{$EXTERNALSYM PCRE_ERROR_BADMAGIC}
 97  PCRE_ERROR_UNKNOWN_NODE = -5;
 98{$EXTERNALSYM PCRE_ERROR_UNKNOWN_NODE}
 99  PCRE_ERROR_NOMEMORY = -6;
100{$EXTERNALSYM PCRE_ERROR_NOMEMORY}
101  PCRE_ERROR_NOSUBSTRING = -7;
102{$EXTERNALSYM PCRE_ERROR_NOSUBSTRING}
103
104  (* Request types for pcre_fullinfo() *)
105
106  PCRE_INFO_OPTIONS = 0;
107{$EXTERNALSYM PCRE_INFO_OPTIONS}
108  PCRE_INFO_SIZE = 1;
109{$EXTERNALSYM PCRE_INFO_SIZE}
110  PCRE_INFO_CAPTURECOUNT = 2;
111{$EXTERNALSYM PCRE_INFO_CAPTURECOUNT}
112  PCRE_INFO_BACKREFMAX = 3;
113{$EXTERNALSYM PCRE_INFO_BACKREFMAX}
114  PCRE_INFO_FIRSTCHAR = 4;
115{$EXTERNALSYM PCRE_INFO_FIRSTCHAR}
116  PCRE_INFO_FIRSTTABLE = 5;
117{$EXTERNALSYM PCRE_INFO_FIRSTTABLE}
118  PCRE_INFO_LASTLITERAL = 6;
119{$EXTERNALSYM PCRE_INFO_LASTLITERAL}
120
121  (* Types *)
122type
123  PPChar = ^PChar;
124  PPPChar = ^PPChar;
125  PInteger = ^integer;
126  PPointer = ^Pointer;
127
128  real_pcre = record
129    magic_number: longword;
130    size: integer;
131    tables: PChar;
132    options: longword;
133    top_bracket: word;
134    top_backref: word;
135    first_char: PChar;
136    req_char: PChar;
137    code: array[0..0] of char;
138  end;
139{$EXTERNALSYM real_pcre}
140  TPCRE = real_pcre;
141  PPCRE = ^TPCRE;
142
143  real_pcre_extra = record
144    options: PChar;
145    start_bits: array[0..31] of char;
146  end;
147{$EXTERNALSYM real_pcre_extra}
148  TPCREExtra = real_pcre_extra;
149  PPCREExtra = ^TPCREExtra;
150
151  (* Functions *)
152{$IFNDEF PCRE_LINKONREQUEST}
153function pcre_compile(const pattern: PChar; options: integer;
154  const errptr: PPChar; erroffset: PInteger; const tableptr: PChar): PPCRE; cdecl;
155{$EXTERNALSYM pcre_compile}
156function pcre_copy_substring(const subject: PChar; ovector: PInteger; stringcount, stringnumber: integer;
157  buffer: PChar; buffersize: integer): integer; cdecl;
158{$EXTERNALSYM pcre_copy_substring}
159function pcre_exec(const code: PPCRE; const extra: PPCREExtra; const subject: PChar;
160{$EXTERNALSYM pcre_exec}
161  length, startoffset, options: integer; ovector: PInteger; ovecsize: integer): integer; cdecl;
162function pcre_study(const code: PPCRE; options: integer; const errptr: PPChar): PPCREExtra; cdecl;
163{$EXTERNALSYM pcre_study}
164function pcre_get_substring(const subject: PChar; ovector: PInteger;
165{$EXTERNALSYM pcre_get_substring}
166  stringcount, stringnumber: integer; const stringptr: PPChar): integer; cdecl;
167function pcre_get_substring_list(const subject: PChar; ovector: PInteger;
168  stringcount: integer; listptr: PPPChar): integer; cdecl;
169{$EXTERNALSYM pcre_get_substring_list}
170procedure pcre_free_substring(var stringptr: PChar); cdecl;
171{$EXTERNALSYM pcre_free_substring}
172procedure pcre_free_substring_list(var stringptr: PChar); cdecl;
173{$EXTERNALSYM pcre_free_substring_list}
174function pcre_maketables: PChar; cdecl;
175{$EXTERNALSYM pcre_maketables}
176function pcre_fullinfo(const code: PPCRE; const extra: PPCREExtra;
177  what: integer; where: Pointer): integer; cdecl;
178{$EXTERNALSYM pcre_fullinfo}
179function pcre_info(const code: PPCRE; optptr, firstcharptr: PInteger): integer; cdecl;
180{$EXTERNALSYM pcre_info}
181function pcre_version: PChar; cdecl;
182{$EXTERNALSYM pcre_version}
183
184// Don't use! These does *not* work!!!
185function pcre_malloc(Size: integer): Pointer; cdecl;
186{$EXTERNALSYM pcre_malloc}
187procedure pcre_free(P: Pointer); cdecl;
188{$EXTERNALSYM pcre_free}
189
190{$ELSE}
191  // dynamic linking
192type
193  pcre_compile_func = function(const pattern: PChar; options: integer;
194    const errptr: PPChar; erroffset: PInteger; const tableptr: PChar): PPCRE; cdecl;
195  pcre_copy_substring_func = function(const subject: PChar; ovector: PInteger; stringcount, stringnumber: integer;
196    buffer: PChar; buffersize: integer): integer; cdecl;
197  pcre_exec_func = function(const code: PPCRE; const extra: PPCREExtra; const subject: PChar;
198    length, startoffset, options: integer; ovector: PInteger; ovecsize: integer): integer; cdecl;
199  pcre_study_func = function(const code: PPCRE; options: integer; const errptr: PPChar): PPCREExtra; cdecl;
200  pcre_get_substring_func = function(const subject: PChar; ovector: PInteger;
201    stringcount, stringnumber: integer; const stringptr: PPChar): integer; cdecl;
202  pcre_get_substring_list_func = function(const subject: PChar; ovector: PInteger;
203    stringcount: integer; listptr: PPPChar): integer; cdecl;
204  pcre_free_substring_func = procedure(var stringptr: PChar); cdecl;
205  pcre_free_substring_list_func = procedure(var stringptr: PChar); cdecl;
206  pcre_maketables_func = function: PChar; cdecl;
207  pcre_fullinfo_func = function(const code: PPCRE; const extra: PPCREExtra;
208    what: integer; where: Pointer): integer; cdecl;
209  pcre_info_func = function(const code: PPCRE; optptr, firstcharptr: PInteger): integer; cdecl;
210  pcre_version_func = function: PChar; cdecl;
211
212  pcre_malloc_func = function(Size: integer): Pointer; cdecl;
213  pcre_free_func = procedure(P: Pointer); cdecl;
214var
215  pcre_compile: pcre_compile_func = nil;
216{$EXTERNALSYM pcre_compile}
217  pcre_copy_substring: pcre_copy_substring_func = nil;
218{$EXTERNALSYM pcre_copy_substring}
219  pcre_exec: pcre_exec_func = nil;
220{$EXTERNALSYM pcre_exec}
221  pcre_study: pcre_study_func = nil;
222{$EXTERNALSYM pcre_study}
223  pcre_get_substring: pcre_get_substring_func = nil;
224{$EXTERNALSYM pcre_get_substring}
225  pcre_get_substring_list: pcre_get_substring_list_func = nil;
226{$EXTERNALSYM pcre_get_substring_list}
227  pcre_free_substring: pcre_free_substring_func = nil;
228{$EXTERNALSYM pcre_free_substring}
229  pcre_free_substring_list: pcre_free_substring_list_func = nil;
230{$EXTERNALSYM pcre_free_substring_list}
231  pcre_maketables: pcre_maketables_func = nil;
232{$EXTERNALSYM pcre_maketables}
233  pcre_fullinfo: pcre_fullinfo_func = nil;
234{$EXTERNALSYM pcre_fullinfo}
235  pcre_info: pcre_info_func = nil;
236{$EXTERNALSYM pcre_info}
237  pcre_version: pcre_version_func = nil;
238{$EXTERNALSYM pcre_version}
239
240  // Don't use! These doesn't work!!!
241  pcre_malloc: pcre_malloc_func = nil;
242{$EXTERNALSYM pcre_malloc}
243  pcre_free: pcre_free_func = nil;
244{$EXTERNALSYM pcre_free}
245
246{$ENDIF}
247
248function IsPCRELoaded: Boolean;
249function LoadPCRE: Boolean;
250procedure UnloadPCRE;
251
252implementation
253uses
254  Windows;
255const
256  pcreDLL = 'pcre.dll';
257
258{$IFDEF PCRE_LINKONREQUEST}
259var
260  PCRELib: THandle = INVALID_HANDLE_VALUE;
261{$ENDIF}
262
263function IsPCRELoaded: Boolean;
264begin
265{$IFDEF PCRE_LINKONREQUEST}
266  Result := PCRELib <> INVALID_HANDLE_VALUE;
267{$ELSE}
268  Result := True;
269{$ENDIF}
270end;
271
272function LoadPCRE: Boolean;
273begin
274{$IFDEF PCRE_LINKONREQUEST}
275  if PCRELib = INVALID_HANDLE_VALUE then
276    PCRELib := LoadLibrary(pcreDLL);
277  Result := PCRELib <> INVALID_HANDLE_VALUE;
278  if Result then
279  begin
280    @pcre_compile := GetProcAddress(PCRELib, 'pcre_compile');
281    @pcre_copy_substring := GetProcAddress(PCRELib, 'pcre_copy_substring');
282    @pcre_exec := GetProcAddress(PCRELib, 'pcre_exec');
283    @pcre_study := GetProcAddress(PCRELib, 'pcre_study');
284    @pcre_get_substring := GetProcAddress(PCRELib, 'pcre_get_substring');
285    @pcre_get_substring_list := GetProcAddress(PCRELib, 'pcre_get_substring_list');
286    @pcre_free_substring := GetProcAddress(PCRELib, 'pcre_free_substring');
287    @pcre_free_substring_list := GetProcAddress(PCRELib, 'pcre_free_substring_list');
288    @pcre_maketables := GetProcAddress(PCRELib, 'pcre_maketables');
289    @pcre_fullinfo := GetProcAddress(PCRELib, 'pcre_fullinfo');
290    @pcre_info := GetProcAddress(PCRELib, 'pcre_info');
291    @pcre_version := GetProcAddress(PCRELib, 'pcre_version');
292
293    @pcre_malloc := GetProcAddress(PCRELib, 'pcre_malloc');
294    @pcre_free := GetProcAddress(PCRELib, 'pcre_free');
295  end
296  else
297    UnloadPCRE;
298{$ELSE}
299  Result := True;
300{$ENDIF}
301end;
302
303procedure UnloadPCRE;
304begin
305{$IFDEF PCRE_LINKONREQUEST}
306  if PCRELib <> INVALID_HANDLE_VALUE then
307    FreeLibrary(PCRELib);
308  PCRELib := INVALID_HANDLE_VALUE;
309  @pcre_compile := nil;
310  @pcre_copy_substring := nil;
311  @pcre_exec := nil;
312  @pcre_study := nil;
313  @pcre_get_substring := nil;
314  @pcre_get_substring_list := nil;
315  @pcre_free_substring := nil;
316  @pcre_free_substring_list := nil;
317  @pcre_maketables := nil;
318  @pcre_fullinfo := nil;
319  @pcre_info := nil;
320  @pcre_version := nil;
321
322  @pcre_malloc := nil;
323  @pcre_free := nil;
324{$ENDIF}
325end;
326
327{$IFNDEF PCRE_LINKONREQUEST}
328function pcre_compile; external pcreDLL name 'pcre_compile';
329function pcre_copy_substring; external pcreDLL name 'pcre_copy_substring';
330function pcre_exec; external pcreDLL name 'pcre_exec';
331function pcre_study; external pcreDLL name 'pcre_study';
332function pcre_get_substring; external pcreDLL name 'pcre_get_substring';
333function pcre_get_substring_list; external pcreDLL name 'pcre_get_substring_list';
334procedure pcre_free_substring; external pcreDLL name 'pcre_free_substring';
335procedure pcre_free_substring_list; external pcreDLL name 'pcre_free_substring_list';
336function pcre_maketables; external pcreDLL name 'pcre_maketables';
337function pcre_fullinfo; external pcreDLL name 'pcre_fullinfo';
338function pcre_info; external pcreDLL name 'pcre_info';
339function pcre_version; external pcreDLL name 'pcre_version';
340function pcre_malloc; external pcreDLL name 'pcre_malloc';
341procedure pcre_free; external pcreDLL name 'pcre_free';
342{$ENDIF}
343
344end.
345