OSDN Git Service

ヘルプメニューのクライアントヘルプページが403表示なので、ジャンプ先を
[winbottle/winbottle.git] / bottleclient / BRegExp.pas
1 unit BRegExp;
2
3 //=====================================================================
4 // BRegExp.pas : Borland Delphi \97p BREGEXP.DLL \97\98\97p\83\86\83j\83b\83g
5 //               1998/10/11\94Å      osamu@big.or.jp
6 //
7 // BREGEXP.DLL \82Í\81Ahttp://www.hi-ho.or.jp/~babaq/ \82É\82Ä\8cö\8aJ\82³\82ê\82Ä\82¢\82é
8 // Perl5\8cÝ\8a·\82Ì\90³\8bK\95\\8c»\83G\83\93\83W\83\93 BREGEXP.DLL \82ð Borland Delphi \82©\82ç\97\98\97p
9 // \82·\82é\82½\82ß\82Ì\83\86\83j\83b\83g\83t\83@\83C\83\8b\82Å\82·\81BDelphi 3 \82Å\8dì\90¬\82µ\82Ü\82µ\82½\82ª\81A32bit
10 // \94Å\82Ì Delphi \82¨\82æ\82Ñ C++ Builder \82Å\93®\8dì\89Â\94\\82Æ\8ev\82¢\82Ü\82·\81B
11 //
12 // BREGEXP.DLL \82Ì\97\98\97p\8fð\8c\8f\82È\82Ç\82Í\81A\93¯\83z\81[\83\80\83y\81[\83W\82ð\82²\8eQ\8fÆ\89º\82³\82¢\81B\97L\97p\82È
13 // \83\89\83C\83u\83\89\83\8a\82ð\96³\8f\9e\82Å\92ñ\8b\9f\89º\82³\82Á\82Ä\82¢\82é babaq \82³\82ñ\82É\8a´\8eÓ\82·\82é\82Æ\82Æ\82à\82É\81A
14 // \8d¡\8cã\82Ì\82²\8a\88\96ô\82ð\8aú\91Ò\82µ\82Ä\82¢\82Ü\82·\81B
15 //
16 // \96{\83\86\83j\83b\83g\82Ì\92\98\8dì\8c \82É\82Â\82¢\82Ä\82Í\81A\82Æ\82â\82©\82­\8c¾\82¤\82Â\82à\82è\82Í\82 \82è\82Ü\82¹\82ñ\81B\8dD\82«\82È
17 // \82æ\82¤\82É\82¨\8eg\82¢\89º\82³\82¢\81B\82½\82¾\82µ\81A\97\98\97p\82É\93\96\82½\82Á\82Ä\82Í\82²\8e©\95ª\82Ì\90Ó\94C\82Ì\89º\82É\82¨\8aè
18 // \82¢\82µ\82Ü\82·\81B\96{\83\86\83j\83b\83g\82É\8aÖ\82µ\82Ä osamu@big.or.jp \82Í\89½\82ç\90Ó\94C\82ð\95\89\82¤\82±\82Æ\82Í
19 // \96³\82¢\82à\82Ì\82Æ\82µ\82Ü\82·\81B
20 //
21 // \96{\83\86\83j\83b\83g\82Í\81A DLL \82Æ\82Æ\82à\82É\94z\95z\82³\82ê\82Ä\82¢\82é\83w\83b\83_\83t\83@\83C\83\8b\8by\82Ñ\81A\8fã\8bL\83z\81[\83\80
22 // \83y\81[\83W\82Å\8ds\82í\82ê\82½\83\86\81[\83U\83T\83|\81[\83g\82Ì\83\8d\83O\83t\83@\83C\83\8b\82ð\82à\82Æ\82É\8dì\90¬\82³\82ê\82Ü\82µ\82½\81B
23 // \82¨\8bC\82Ã\82«\82Ì\93_\82È\82Ç\82 \82è\82Ü\82µ\82½\82ç\81Aosamu@big.or.jp \82Ü\82Å\93d\8eq\83\81\81[\83\8b\82É\82Ä
24 // \82¨\92m\82ç\82¹\89º\82³\82ê\82Î\81A\8bC\95ª\8e\9f\91æ\82Å\82Í\82È\82ñ\82ç\82©\82Ì\91Î\8f\88\82ð\82·\82é\89Â\94\\90«\82ª\82 \82è\82Ü\82·\81B(^_^;
25 //
26 // \8eg\97p\95û\96@\82É\82Â\82¢\82Ä\82Í\95t\91®\82Ì\83w\83\8b\83v\83t\83@\83C\83\8b\82ð\82²\97\97\89º\82³\82¢\81B
27 //
28 // [\97\9a\97ð]
29 //  98.10.3     \8f\89\8aú\83o\81[\83W\83\87\83\93
30 //  98.10.11    \83o\83O\83t\83B\83b\83N\83X (TBRegExpRec.splitp \82ð ^PChar -> PPChar)
31 //
32 //=====================================================================
33
34 interface
35
36 uses SysUtils;
37
38 //=====================================================================
39 // \96{\89Æ BREGEXP.H \82Æ\81A\83T\83|\81[\83g\83z\81[\83\80\83y\81[\83W\82Ì\83h\83L\83\85\83\81\83\93\83g\82æ\82è
40 // BREGEXP.DLL \82Æ\92¼\8c\8b\82µ\82½\90é\8c¾
41 //=====================================================================
42
43 type
44 PPChar=^PChar;
45 TBRegExpRec=record
46     outp: PChar;        // \92u\8a·\82¦\8c\8b\89Ê\90æ\93ª\83|\83C\83\93\83^
47     outendp: PChar;     // \92u\8a·\82¦\8c\8b\89Ê\96\96\94ö\83|\83C\83\93\83^
48     splitctr: Integer;  // split \8c\8b\89Ê\83J\83E\83\93\83^
49     splitp: PPChar;     // split \8c\8b\89Ê\83|\83C\83\93\83^\83|\83C\83\93\83^
50     rsv1: Integer;      // \97\\96ñ\8dÏ\82Ý
51     parap: PChar;       // \83R\83}\83\93\83h\95\8e\9a\97ñ\90æ\93ª\83|\83C\83\93\83^ ('s/xxxxx/yy/gi')
52     paraendp: PChar;    // \83R\83}\83\93\83h\95\8e\9a\97ñ\96\96\94ö\83|\83C\83\93\83^
53     transtblp: PChar;   // tr \83e\81[\83u\83\8b\82Ö\82Ì\83|\83C\83\93\83^
54     startp: PPChar;     // \83}\83b\83`\82µ\82½\95\8e\9a\97ñ\82Ö\82Ì\90æ\93ª\83|\83C\83\93\83^
55     endp: PPChar;       // \83}\83b\83`\82µ\82½\95\8e\9a\97ñ\82Ö\82Ì\96\96\94ö\83|\83C\83\93\83^
56 end;
57 pTBRegExpRec=^TBRegExpRec;
58
59 function BMatch(str, target, targetendp: PChar;
60                 var rxp: pTBRegExpRec; msg: PChar): Boolean; cdecl;
61     external 'bregexp.dll';
62 function BSubst(str, target, targetendp: PChar;
63                 var rxp: pTBRegExpRec; msg: PChar): Boolean; cdecl;
64     external 'bregexp.dll';
65 function BTrans(str, target, targetendp: PChar;
66                 var rxp: pTBRegExpRec; msg: PChar): Boolean; cdecl;
67     external 'bregexp.dll';
68 function BSplit(str, target, targetendp: PChar; limit: Integer;
69                 var rxp: pTBRegExpRec; msg: PChar): Boolean; cdecl;
70     external 'bregexp.dll';
71 procedure BRegFree(rx: pTBRegExpRec); cdecl;
72     external 'bregexp.dll' name 'BRegfree';
73 function BRegExpVersion: PChar; cdecl;
74     external 'bregexp.dll' name 'BRegexpVersion';
75
76 //=====================================================================
77 // TBRegExp : BREGEXP.DLL \82Ì\8b@\94\\82ð\83J\83v\83Z\83\8b\89»\82·\82é\83I\83u\83W\83F\83N\83g
78 //=====================================================================
79
80 type
81 EBRegExpError=class(Exception) end;
82 TBRegExpMode=(brxNone, brxMatch, brxSplit);
83 TBRegExp=class(TObject)
84   private
85     Mode: TBRegExpMode;
86     pBRegExp: PTBRegExpRec;
87     function GetSplitCount: Integer;
88     function GetSplitStrings(index: Integer): string;
89     function GetMatchStrings(index:Integer): string;
90     function GetMatchCount: Integer;
91     function GetCount: Integer;
92     function GetStrings(index: Integer): string;
93   public
94     destructor Destroy; override;
95   public
96     function Match(const Command, TargetString: string): Boolean;
97     function Subst(const Command: string;var TargetString: string): Boolean;
98     function Split(const Command, TargetString: string; Limit: Integer): Boolean;
99     function Trans(const Command: string;var TargetString: string): Boolean;
100     property Count: Integer read GetCount;
101     property Strings[index: Integer]: string read GetStrings; default;
102 end;
103
104 //=====================================================================
105
106 implementation
107
108 //=====================================================================
109
110 destructor TBRegExp.Destroy;
111 begin
112     if pBRegExp<>nil then
113         BRegFree(pBRegExp);
114     inherited Destroy;
115 end;
116
117 //=====================================================================
118
119 function TBRegExp.Match(const Command, TargetString: string): Boolean;
120 var ErrorString: string;
121 begin
122     SetLength(ErrorString,256);
123     Mode:=brxNone;
124     Result:=BMatch(
125         PChar(Command),
126         PChar(TargetString),
127         PChar(TargetString)+Length(TargetString),
128         pBRegExp,
129         PChar(ErrorString));
130     SetLength(ErrorString,StrLen(PChar(ErrorString)));
131     if ErrorString<>'' then
132         raise EBRegExpError.Create(ErrorString);
133     Mode:=brxMatch;
134 end;
135
136 //=====================================================================
137
138 function TBRegExp.Subst(const Command: string;
139                         var TargetString: string): Boolean;
140 var ErrorString: string;
141 begin
142     SetLength(ErrorString,256);
143     Mode:=brxNone;
144     Result:=BSubst(
145         PChar(Command),
146         PChar(TargetString),
147         PChar(TargetString)+Length(TargetString),
148         pBRegExp,
149         PChar(ErrorString));
150     SetLength(ErrorString,StrLen(PChar(ErrorString)));
151     if ErrorString<>'' then
152         raise EBRegExpError.Create(ErrorString);
153     if Result then TargetString:=pBRegExp^.outp;
154 end;
155
156 //=====================================================================
157
158 function TBRegExp.Trans(const Command: string;
159                         var TargetString: string): Boolean;
160 var ErrorString: string;
161 begin
162     SetLength(ErrorString,256);
163     Mode:=brxNone;
164     Result:=BTrans(
165         PChar(Command),
166         PChar(TargetString),
167         PChar(TargetString)+Length(TargetString),
168         pBRegExp,
169         PChar(ErrorString));
170     SetLength(ErrorString,StrLen(PChar(ErrorString)));
171     if ErrorString<>'' then
172         raise EBRegExpError.Create(ErrorString);
173     if Result then TargetString:=pBRegExp^.outp;
174 end;
175
176 //=====================================================================
177
178 function TBRegExp.Split(const Command, TargetString: string;
179                         Limit: Integer): Boolean;
180 var ErrorString: string;
181 begin
182     SetLength(ErrorString,256);
183     Mode:=brxNone;
184     Result:=BSplit(
185         PChar(Command),
186         PChar(TargetString),
187         PChar(TargetString)+Length(TargetString),
188         Limit,
189         pBRegExp,
190         PChar(ErrorString));
191     SetLength(ErrorString,StrLen(PChar(ErrorString)));
192     if ErrorString<>'' then
193         raise EBRegExpError.Create(ErrorString);
194     Mode:=brxSplit;
195 end;
196
197 //=====================================================================
198
199 function TBRegExp.GetCount: Integer;
200 begin
201     Result:=0;
202     case Mode of
203     brxNone:
204         raise EBRegExpError.Create('no count now');
205     brxMatch:
206         Result:=GetMatchCount;
207     brxSplit:
208         Result:=GetSplitCount;
209     end;
210 end;
211
212 function TBRegExp.GetMatchCount: Integer;
213 var sp,ep: PPChar;
214 begin
215     Result:=0;
216     sp:=pBRegExp^.startp;
217     ep:=pBRegExp^.endp;
218     while Integer(sp^)<Integer(ep^) do begin
219         Inc(sp);
220         Inc(ep);
221         Inc(Result);
222     end;
223 end;
224
225 function TBRegExp.GetSplitCount: Integer;
226 begin
227     Result:=pBRegExp^.splitctr;
228 end;
229
230 //=====================================================================
231
232 function TBRegExp.GetStrings(index: Integer): string;
233 begin
234     Result:='';
235     case Mode of
236     brxNone:
237         raise EBRegExpError.Create('no strings now');
238     brxMatch:
239         Result:=GetMatchStrings(index);
240     brxSplit:
241         Result:=GetSplitStrings(index);
242     end;
243 end;
244
245 function TBRegExp.GetMatchStrings(index:Integer):string;
246 var sp,ep: PPChar;
247 begin
248     Result:='';
249     sp:=pBRegExp^.startp;
250     ep:=pBRegExp^.endp;
251     while index>0 do begin
252         if Integer(sp^)>=Integer(ep^) then
253             raise EBRegExpError.Create('index out of range');
254         Inc(sp);
255         Inc(ep);
256         Dec(index);
257     end;
258     SetLength(Result,Integer(ep^)-Integer(sp^));
259     Move(sp^^,PChar(Result)^,Integer(ep^)-Integer(sp^));
260 end;
261
262 function TBRegExp.GetSplitStrings(index:Integer): string;
263 var p: PPChar;
264     sp,ep: PChar;
265 begin
266     if (index<0) or (index>=GetSplitCount) then
267         raise EBRegExpError.Create('index out of range');
268     p:=pBRegExp^.splitp;
269     Inc(p,index*2);
270     sp:=p^;
271     Inc(p);
272     ep:=p^;
273     SetLength(Result,Integer(ep)-Integer(sp));
274     Move(sp^,PChar(Result)^,Integer(ep)-Integer(sp));
275 end;
276
277 //=====================================================================
278
279 end.
280