]> git.8kb.co.uk Git - dataflex/df32func/blobdiff - src/df32/regex.inc
Add missing new files
[dataflex/df32func] / src / df32 / regex.inc
diff --git a/src/df32/regex.inc b/src/df32/regex.inc
new file mode 100644 (file)
index 0000000..a0f9b5c
--- /dev/null
@@ -0,0 +1,165 @@
+//-------------------------------------------------------------------------\r
+// regex.inc\r
+//      This file contains DataFlex functions to provide basic regex \r
+//      functionality based on the GNU POSIX regex library, and accessed\r
+//      via Win32 API calls to df32func.dll.  \r
+//      See df32func.h for external function definitions.\r
+//\r
+// This file is to be included when using Win32 capabilities in df32func.mk\r
+//\r
+// Copyright (c) 2006-2015, glyn@8kb.co.uk\r
+// \r
+// df32func/regex.inc\r
+//-------------------------------------------------------------------------\r
+\r
+#IFDEF __df32func_h__\r
+#ELSE\r
+    #INCLUDE df32func.h\r
+#ENDIF\r
+\r
+//-------------------------------------------------------------------------\r
+// Functions\r
+//-------------------------------------------------------------------------\r
+\r
+// All the regex function accept a set of flags, this can be one or more of:\r
+//             g = Perform match against each substring rather than just the first (greedy)\r
+//             n = Perform newline-sensitive matching\r
+//             i = Perform cases insensitive matching\r
+\r
+//Purely check if a regex expression produces match in the input string\r
+// Returns 1 on match, 0 on no match\r
+//       E.g\r
+//    move (regexp_match('the quick brown fox jumps over the lazy dog.', 'fox', 'g'))\r
+function regexp_match global string str string pattern string flags returns integer\r
+       local integer l_iReturn\r
+       local pointer l_pStr l_pPattern l_pFlags\r
+       \r
+       getaddress of str to l_pStr\r
+       getaddress of pattern to l_pPattern\r
+       getaddress of flags to l_pFlags\r
+       \r
+       move (RegexpMatch(l_pStr, l_pPattern, l_pFlags, ERRORS_TO_STDERR)) to l_iReturn\r
+       \r
+       function_return l_iReturn\r
+end_function   \r
+\r
+//Return a string containing all regex matches in the input string\r
+//    E.g\r
+//    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
+function regexp_matches global string str string pattern string flags returns string\r
+       local integer l_iReturn\r
+       local pointer l_pStr l_pPattern l_pFlags l_pOut\r
+       local string l_sOut l_sReturn\r
+       \r
+       move "" to l_sReturn\r
+       getaddress of str to l_pStr\r
+       getaddress of pattern to l_pPattern\r
+       getaddress of flags to l_pFlags\r
+       zerostring MAX_DFREGEX_BUFFER to l_sOut\r
+       getaddress of l_sOut to l_pOut\r
+       \r
+       move (RegexpMatches(l_pStr, l_pPattern, l_pFlags, l_pOut, MAX_DFREGEX_BUFFER, ERRORS_TO_STDERR)) to l_iReturn\r
+       \r
+       if (l_iReturn = 0);\r
+               move (cstring(l_sOut)) To l_sReturn\r
+       else begin\r
+               if (l_iReturn = -1);\r
+                       custom_error ERROR_CODE_REGEX_BUFFER_OVERFLOW$ ERROR_MSG_REGEX_BUFFER_OVERFLOW MAX_DFREGEX_BUFFER                       \r
+               if (l_iReturn = -2);\r
+                        custom_error ERROR_CODE_REGEX_COMPILE_FAILURE$ ERROR_MSG_REGEX_COMPILE_FAILURE\r
+               move "" to l_sReturn\r
+       end\r
+       \r
+       function_return l_sReturn\r
+end_function \r
+\r
+//Perform a replacement on the input string all matches with the given pattern\r
+//    E.g.\r
+//    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
+function regexp_replace global string str string pattern string replacement string flags returns string\r
+       local integer l_iReturn\r
+       local pointer l_pStr l_pPattern l_pFlags l_pReplacement l_pOut\r
+       local string l_sOut l_sReturn\r
+       \r
+       move "" to l_sReturn\r
+       getaddress of str to l_pStr\r
+       getaddress of pattern to l_pPattern\r
+       getaddress of flags to l_pFlags\r
+       getaddress of replacement to l_pReplacement\r
+       zerostring MAX_DFREGEX_BUFFER to l_sOut\r
+       getaddress of l_sOut to l_pOut\r
+       \r
+       move (RegexpReplace(l_pStr, l_pPattern, l_pReplacement, l_pFlags, l_pOut, MAX_DFREGEX_BUFFER, ERRORS_TO_STDERR)) to l_iReturn\r
+       \r
+       if (l_iReturn = 0);\r
+               move (cstring(l_sOut)) To l_sReturn\r
+       else begin\r
+               if (l_iReturn = -1);\r
+                       custom_error ERROR_CODE_REGEX_BUFFER_OVERFLOW$ ERROR_MSG_REGEX_BUFFER_OVERFLOW MAX_DFREGEX_BUFFER                       \r
+               if (l_iReturn = -2);\r
+                        custom_error ERROR_CODE_REGEX_COMPILE_FAILURE$ ERROR_MSG_REGEX_COMPILE_FAILURE\r
+               move "" to l_sReturn\r
+       end\r
+                       \r
+       function_return l_sReturn\r
+end_function \r
+\r
+// Parse an output string from regexp_matches to get the result count\r
+//    E.g\r
+//    move (regexp_matches_count(myRegexMatchesOutput)) to myInt\r
+function regexp_matches_count global string argv returns integer\r
+       local integer l_iCount l_i\r
+       local string l_sChar l_sLast\r
+       \r
+       move "" to l_sChar\r
+       move "" to l_sLast\r
+       for l_i from 0 to (length(argv))\r
+               move (mid(argv,1,l_i)) to l_sChar\r
+               if ((l_sChar = '{') and (l_sLast <> '\')) increment l_iCount\r
+               move l_sChar to l_sLast\r
+       loop\r
+       \r
+       function_return l_iCount\r
+end_function\r
+\r
+// Parse an output string from regexp_matches to get the result at an index\r
+//    E.g\r
+//    move (regexp_matches_item(myRegexMatchesOutput,muInt)) to myString\r
+function regexp_matches_item global string argv integer argv2 returns string\r
+       local integer l_iCount l_i l_iOpen l_iQuot\r
+       local string l_sChar l_sLast l_sNext l_sBuf\r
+       \r
+       move 0 to l_iCount\r
+       move 0 to l_iOpen\r
+       move 0 to l_iQuot\r
+       move "" to l_sLast\r
+       for l_i from 0 to (length(argv))\r
+               move (mid(argv,1,l_i)) to l_sChar\r
+               move (mid(argv,1,l_i-1)) to l_sLast\r
+               \r
+               if ((l_sChar = '{') and (l_sLast <> '\')) increment l_iCount            \r
+               if (l_iCount <> argv2) break begin\r
+\r
+               move (mid(argv,1,l_i+1)) to l_sNext\r
+               \r
+               if ((l_sChar = '{') and not (l_iQuot)) begin\r
+                       move 1 to l_iOpen\r
+                       move "" to l_sBuf       \r
+               end             \r
+               else if ((l_sChar = '}') and not (l_iQuot)) begin\r
+                       move 0 to l_iOpen\r
+               end     \r
+               else if ((l_sChar = '"') and (l_sLast <> '\')) begin\r
+                       if (l_iQuot) move 0 to l_iQuot\r
+                       else move 1 to l_iQuot\r
+               end\r
+               if ((l_sChar = ',') and not (l_iOpen)) break begin\r
+               if (((l_sChar = '{') or (l_sChar = '}')) and not (l_iQuot)) break begin\r
+               if ((l_sChar = '"') and (l_sLast <> '\')) break begin\r
+               if ((l_iQuot) and (l_sChar = '\') and ((l_sNext = '"') or (l_sNext = '\'))) break begin\r
+               \r
+               append l_sBuf l_sChar           \r
+       loop\r
+       \r
+       function_return l_sBuf\r
+end_function
\ No newline at end of file