OSDN Git Service

・URL をクリックしたときに可能ならギコナビで開くようになった。
[gikonavigoeson/gikonavi.git] / YofUtils.pas
1 unit YofUtils;
2
3 {
4         HttpApp \82Ì\83N\83\8d\81[\83\93\82â\82»\82Ì\91¼\8eG\97p\8aÖ\90\94
5 }
6 interface
7
8 //==================================================
9 uses
10 //==================================================
11
12         Classes, SysUtils;
13
14 procedure ExtractHttpFields(
15         const chrSep : TSysCharSet;
16         const chrWhite : TSysCharSet;
17         const strValue : string;
18         var strResult : TStringList;
19         unknownFlag : boolean = false
20 );
21
22 function HtmlEncode(
23         const strValue : string
24 ) : string;
25
26 function HtmlDecode(
27         const strValue : string
28 ) : string;
29
30 function HttpEncode(
31         const strValue : string
32 ) : string;
33
34 function MatchesMask(
35         const filename, mask : string
36 ) : boolean;
37
38 // \83\81\83^\83L\83\83\83\89\83N\83^\82ð\90³\8bK\95\\8c»\88µ\82¢\82É\82È\82ç\82È\82¢\82æ\82¤\82É\92u\8a·
39 function RegExpEncode(
40         const text : string
41 ) : string;
42
43 //==================================================
44 const
45 //==================================================
46         kYofKanji : TSysCharSet = [#$80..#$A0, #$E0..#$ff];
47
48 //==================================================
49 implementation
50 //==================================================
51
52 // \82Æ\82è\82 \82¦\82¸\82Ì\91ã\97p\95i\82È\82Ì\82Å chrWhite \82ð\8dl\97\82µ\82Ä\82¢\82È\82¢\82±\82Æ\82É\92\8d\88Ó\81I\81I\81I
53 procedure ExtractHttpFields(
54         const chrSep : TSysCharSet;
55         const chrWhite : TSysCharSet;
56         const strValue : string;
57         var strResult : TStringList;
58         unknownFlag : boolean = false
59 );
60 var
61         last, p, strLen : Integer;
62 begin
63
64         strLen := Length( strValue );
65         p := 1;
66         last := 1;
67
68         while p <= strLen do
69         begin
70
71                 if strValue[ p ] in chrSep then
72                 begin
73                         strResult.Add( Copy( strValue, last, p - last ) );
74                         last := p + 1;
75                 end;
76
77                 p := p + 1;
78
79         end;
80
81         if last <> p then
82                 strResult.Add( Copy( strValue, last, strLen - last + 1 ) );
83
84 end;
85
86 function HtmlEncode(
87         const strValue : string
88 ) : string;
89 var
90         i : Integer;
91         strLen : Integer;
92         strResult : string;
93 begin
94
95         strLen := Length( strValue );
96         i := 1;
97
98         while i <= strLen do
99         begin
100
101                 case strValue[ i ] of
102                 '&':
103                         begin
104                                 strResult := strResult + '&amp;';
105                         end;
106                 '<':
107                         begin
108                                 strResult := strResult + '&lt;';
109                         end;
110                 '>':
111                         begin
112                                 strResult := strResult + '&gt;';
113                         end;
114                 '"':
115                         begin
116                                 strResult := strResult + '&quot;';
117                         end;
118                 else
119                         begin
120                                 if strValue[ i ] in kYofKanji then
121                                 begin
122                                         strResult := strResult + strValue[ i ];
123                                         Inc( i );
124                                 end;
125                                 strResult := strResult + strValue[ i ];
126                         end;
127                 end;
128
129                 i := i + 1;
130
131         end;
132
133         Result := strResult;
134
135 end;
136
137 function HtmlDecode(
138         const strValue : string
139 ) : string;
140 var
141         strResult : string;
142 begin
143
144         strResult := StringReplace( strValue, '&lt;', '<', [rfReplaceAll] );
145         strResult := StringReplace( strResult, '&gt;', '>', [rfReplaceAll] );
146         strResult := StringReplace( strResult, '&quot;', '"', [rfReplaceAll] );
147         strResult := StringReplace( strResult, '&amp;', '&', [rfReplaceAll] );
148
149         Result := strResult;
150
151 end;
152
153 function HttpEncode(
154         const strValue : string
155         ) : string;
156 var
157         i : Integer;
158         strLen : Integer;
159         strResult : string;
160         b : Integer;
161 const
162         kHexCode : array [0..15] of char = (
163                                 '0', '1', '2', '3', '4', '5', '6', '7',
164                                 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' );
165 begin
166
167         strLen := Length( strValue );
168         i := 1;
169
170         while i <= strLen do
171         begin
172
173                 case strValue[ i ] of
174                 '0' .. '9', 'a' .. 'z', 'A' .. 'Z', '*', '-', '.', '@', '_':
175                         begin
176                                 strResult := strResult + strValue[ i ];
177                         end;
178                 else
179                         begin
180                                 b := Integer( strValue[ i ] );
181                                 strResult := strResult + '%'
182                                                                 + kHexCode[ b div $10 ]
183                                                                 + kHexCode[ b mod $10 ];
184                         end;
185                 end;
186
187                 i := i + 1;
188
189         end;
190
191         Result := strResult;
192
193 end;
194
195 // \82Æ\82è\82 \82¦\82¸\82Ì\91ã\97p\95i\82È\82Ì\82Å [] \82ð\8eg\82Á\82½\90³\8bK\95\\8c»\82ð\8dl\97\82µ\82Ä\82¢\82È\82¢\82±\82Æ\82É\92\8d\88Ó\81I\81I\81I
196 function MatchesMask(
197         const filename, mask : string
198         ) : boolean;
199 var
200         pName, pMask : Integer;
201   ptrName, ptrMask : PChar;
202         nameLen, maskLen : Integer;
203         chrUpMask : char;
204   delimiterPos : Integer;
205 begin
206
207         nameLen := Length( filename );
208         maskLen := Length( mask );
209   ptrName := PChar( filename );
210   ptrMask := PChar( mask );
211   pName := 0;
212         pMask := 0;
213   delimiterPos := Pos( '\', string( ptrName + pName ) );
214   while delimiterPos > 0 do
215   begin
216         pName := pName + delimiterPos;
217         delimiterPos := Pos( '\', string( ptrName + pName ) );
218   end;
219
220         while (pMask < maskLen) and (pName < nameLen) do
221         begin
222
223                 case ptrMask[ pMask ] of
224                 '?':
225                         begin
226                                 // \82±\82Ì 1 \8e\9a\82Í\89½\82à\82µ\82È\82¢
227                         end;
228                 '*':
229                         begin
230                                 pMask := pMask + 1;
231                                 // mask \82ð\91\96\8d¸\82µ\90Ø\82Á\82½\82ç\8fI\97¹
232                                 if pMask >= maskLen then
233                                 begin
234                                         Result := true;
235                                         exit;
236                                 end;
237
238                                 // * \82Ì\8e\9f\82Ì\95\8e\9a\82ª\97\88\82é\82Ü\82Å\94ò\82Î\82·
239                                 chrUpMask := upcase( ptrMask[ pMask ] );
240                                 while chrUpMask <> UpCase( ptrName[ pName ] ) do
241                                 begin
242                                         pName := pName + 1;
243                                         if pName >= nameLen then
244                                         begin
245                                                 Result := true;
246                                                 exit;
247                                         end;
248                                 end;
249
250                                 // * \82Ì\8e\9f\82Ì\95\8e\9a\82ª\8c©\82Â\82©\82ç\82È\82©\82Á\82½\82ç\8fI\97¹
251                                 if chrUpMask <> UpCase( ptrName[ pName ] ) then
252                                 begin
253                                         Result := false;
254                                         exit;
255                                 end;
256
257                                 pName := pName + 1;
258                                 pMask := pMask + 1;
259                         end;
260                 else
261                         begin
262                                 // \82±\82Ì 1 \95\8e\9a\82ª\88á\82Á\82½\82ç\8fI\97¹
263                                 if UpCase( ptrMask[ pMask ] ) <> UpCase( ptrName[ pName ] ) then
264                                 begin
265                                         Result := false;
266                                         exit;
267                                 end;
268
269                         end;
270                 end;
271
272                 // \8e\9f\82Ì\95\8e\9a\82Ö
273                 pName := pName + 1;
274                 pMask := pMask + 1;
275
276         end;
277
278         if (pMask >= maskLen) and (pName >= nameLen) then
279                 Result := true
280         else
281                 Result := false;
282
283 end;
284
285
286 // \83\81\83^\83L\83\83\83\89\83N\83^\82ð\90³\8bK\95\\8c»\88µ\82¢\82É\82È\82ç\82È\82¢\82æ\82¤\82É\92u\8a·
287 function RegExpEncode(
288         const text : string
289 ) : string;
290 var
291         strResult : string;
292 begin
293
294         strResult := StringReplace( text, '\', '\\', [rfReplaceAll] );
295         strResult := StringReplace( strResult, '[', '\[', [rfReplaceAll] );
296         strResult := StringReplace( strResult, ']', '\]', [rfReplaceAll] );
297         strResult := StringReplace( strResult, '(', '\(', [rfReplaceAll] );
298         strResult := StringReplace( strResult, ')', '\)', [rfReplaceAll] );
299         strResult := StringReplace( strResult, '[', '\[', [rfReplaceAll] );
300         strResult := StringReplace( strResult, ']', '\]', [rfReplaceAll] );
301         strResult := StringReplace( strResult, '*', '\*', [rfReplaceAll] );
302         strResult := StringReplace( strResult, '?', '\?', [rfReplaceAll] );
303         strResult := StringReplace( strResult, '.', '\.', [rfReplaceAll] );
304         strResult := StringReplace( strResult, '+', '\+', [rfReplaceAll] );
305         strResult := StringReplace( strResult, '|', '\|', [rfReplaceAll] );
306         strResult := StringReplace( strResult, '^', '\^', [rfReplaceAll] );
307         strResult := StringReplace( strResult, '$', '\$', [rfReplaceAll] );
308
309         Result := strResult;
310
311 end;
312
313 end.