]> git.8kb.co.uk Git - dataflex/dfregex/blob - src/df32/dataflex.pkg
Initial Commit
[dataflex/dfregex] / src / df32 / dataflex.pkg
1 //-------------------------------------------------------------------------\r
2 // posix regex extensions\r
3 //\r
4 // Copyright (c) 2015, glyn@8kb.co.uk\r
5 // Author: Glyn Astill <glyn@8kb.co.uk>\r
6 //\r
7 //-------------------------------------------------------------------------\r
8 //\r
9 \r
10 use dll\r
11 \r
12 Define max_dfregex_buffer                                               for 16384\r
13 Define errors_to_stderr                                                 for 0\r
14 \r
15 external_function RegexpMatch "RegexpMatch" dfregex.dll pointer str pointer pattern pointer flags integer errors returns integer\r
16 external_function RegexpMatches "RegexpMatches" dfregex.dll pointer str pointer pattern pointer flags pointer out pointer out_len integer errors  returns integer\r
17 external_function RegexpReplace "RegexpReplace" dfregex.dll pointer str pointer pattern pointer replacement pointer flags pointer out pointer out_len integer errors returns integer\r
18 \r
19 //Purely check if a regex expression produces match in the input string\r
20 // Returns 1 on match, 0 on no match\r
21 //        E.g\r
22 //    move (regexp_match('the quick brown fox jumps over the lazy dog.', 'fox', 'g'))\r
23 function regexp_match global string str string pattern string flags returns integer\r
24         local integer l_iReturn\r
25         local pointer l_pStr l_pPattern l_pFlags\r
26         \r
27         getaddress of str to l_pStr\r
28         getaddress of pattern to l_pPattern\r
29         getaddress of flags to l_pFlags\r
30         \r
31         move (RegexpMatch(l_pStr, l_pPattern, l_pFlags, errors_to_stderr)) to l_iReturn\r
32         \r
33         function_return l_iReturn\r
34 end_function   \r
35 \r
36 //Return a string containing all regex matches in the input string\r
37 //    E.g\r
38 //    move (regexp_matches('the quick brown fox jumps over the la\{zy d"og.', 'fox|(the)|brown|(la\\\{zy)|(d"og)', 'g')) to myString\r
39 function regexp_matches global string str string pattern string flags returns string\r
40         local integer l_iReturn\r
41         local pointer l_pStr l_pPattern l_pFlags l_pOut\r
42         local string l_sOut l_sReturn\r
43         \r
44         move "" to l_sReturn\r
45         getaddress of str to l_pStr\r
46         getaddress of pattern to l_pPattern\r
47         getaddress of flags to l_pFlags\r
48         zerostring max_dfregex_buffer to l_sOut\r
49         getaddress of l_sOut to l_pOut\r
50         \r
51         move (RegexpMatches(l_pStr, l_pPattern, l_pFlags, l_pOut, max_dfregex_buffer, errors_to_stderr)) to l_iReturn\r
52         \r
53         if (l_iReturn = 0);\r
54                 move (cstring(l_sOut)) To l_sReturn\r
55         else begin\r
56                 if (l_iReturn = -1);\r
57                         error 999997 "Regex output buffer too small"\r
58                 if (l_iReturn = -2);\r
59             error 999998 "Regex compilation failure"\r
60                 move "" to l_sReturn\r
61         end\r
62         \r
63         function_return l_sReturn\r
64 end_function \r
65 \r
66 //Perform a replacement on the input string all matches with the given pattern\r
67 //    E.g.\r
68 //    move (regexp_replace('22 quick brown foxes jump over the 44 lazy dogs.', '([0-9]*).* (foxes) .* ([0-9]*) .* (dogs).*', 'SELECT build_data(\1,\2), build_data(\3,\4);', 'g')) to myString\r
69 function regexp_replace global string str string pattern string replacement string flags returns string\r
70         local integer l_iReturn\r
71         local pointer l_pStr l_pPattern l_pFlags l_pReplacement l_pOut\r
72         local string l_sOut l_sReturn\r
73         \r
74         move "" to l_sReturn\r
75         getaddress of str to l_pStr\r
76         getaddress of pattern to l_pPattern\r
77         getaddress of flags to l_pFlags\r
78         getaddress of replacement to l_pReplacement\r
79         zerostring max_dfregex_buffer to l_sOut\r
80         getaddress of l_sOut to l_pOut\r
81         \r
82         move (RegexpReplace(l_pStr, l_pPattern, l_pReplacement, l_pFlags, l_pOut, max_dfregex_buffer, errors_to_stderr)) to l_iReturn\r
83         \r
84         if (l_iReturn = 0);\r
85                 move (cstring(l_sOut)) To l_sReturn\r
86         else begin\r
87                 if (l_iReturn = -1);\r
88                         error 999997 "Regex output buffer too small"    \r
89                 if (l_iReturn = -2);\r
90                         error 999998 "Regex compilation failure"                        \r
91                 move "" to l_sReturn\r
92         end\r
93                         \r
94         function_return l_sReturn\r
95 end_function