!**********************************************************************
!*       LIKE Pattern Matching (Based on Oracle LIKE Operator)        *
!**********************************************************************
!*                                                                    *
!*        INPUT: $I_string  - Input String                            *
!*               $I_pattern - Search Pattern                          *
!*               $I_escape  - Escape Character                        *
!*       OUTPUT: $O_match   - Match Indictor (Y/N)                    *
!*                                                                    *
!*               To de-activate this function:                        *
!*               #define LIKE_Func_Remove                             *
!*                                                                    *
!**********************************************************************
!*                                                                    *
!*      EXAMPLE: do LIKE-Func('ABCDE',  'A%',     '',  $O_match1)     *
!*               do LIKE-Func('ABCDE',  'A%E',    '',  $O_match2)     *
!*               do LIKE-Func('ABCDE',  '__C%',   '',  $O_match3)     *
!*               do LIKE-Func('ABCDE',  '_C%',    '',  $O_match4)     *
!*               do LIKE-Func('50% OFF','%^% OFF','^', $O_match5)     *
!*               do LIKE-Func('50 OFF', '%^% OFF','^', $O_match6)     *
!*                                                                    *
!*      RESULTS: $O_match1 = 'Y'  - Starts with 'A' rest wildcard     *
!*               $O_match2 = 'Y'  - Wildcard between 'A' and 'E'      *
!*               $O_match3 = 'Y'  - 2 fixed (any), 'C' rest wildcard. *
!*               $O_match4 = 'N'  - 'C' not in second position.       *
!*               $O_match5 = 'Y'  - Wildcard then '% OFF' (escape)    *
!*               $O_match6 = 'N'  - '%' not in string                 *
!*                                                                    *
!**********************************************************************
!*                                                                    *
!*         NOTE: This function mimics the Oracle LIKE Operator.       *
!*               Example 5 ($O_match5) would be performed in SQL*Plus *
!*               as follows:                                          *
!*                                                                    *
!*               WHERE '50% OFF' LIKE '%^% OFF' ESCAPE '^'            *
!*                                                                    *
!*               The Escape Character tells Oracle the associated     *
!*               instance of '%' is to be treated as a character      *
!*               and not a wildcard.                                  *
!*                                                                    *
!*               The LIKE operator may vary by database. For example  *
!*               SQLBase processes nulls differently than Oracle.     *
!*               Oracle always returns false if either the string or  *
!*               pattern are null. SQLBase may possibly return true   *
!*               if a null is present in either or both.              *
!*                                                                    *
!*               SQLBASE: where ''  like ''   <=== true               *
!*                ORACLE: where ''  like ''   <=== false              *
!*                                                                    *
!*               SQLBASE: where ''  like '%'  <=== true               *
!*                ORACLE: where ''  like '%'  <=== false              *
!*                                                                    *
!*               SQLBASE: where 'X' like ''   <=== false              *
!*                ORACLE: where 'X' like ''   <=== false              *
!*                                                                    *
!*               Modify to suit your particular requirements.         *
!*                                                                    *
!**********************************************************************
#ifndef LIKE_Func_Remove
begin-procedure LIKE-Func($I_string, $I_pattern, $I_escape, :$O_match)
let $O_match = 'Y'
!   Oracle Null Handling (Modify as needed)
if  isnull($I_pattern)
or  isnull($I_string)
    let $O_match = 'N'
end-if
let $special = '%_' || $I_escape
let $last    = '_'
let $dubb    = ''
let #lenS    = length($I_string)
let #lenP    = length($I_pattern)
let #posS    = 1
let #posP    = 1
let $srch    = ''
!   PHASE 1 - Matching driven within Pattern Loop
while #posP <= #lenP
   let $char = substr($I_pattern, #posP, 1)
   if  instr($special, $char, 1) > 0
       if $srch <> ''
          let #srch  = instr($I_string, $srch, #posS)
          if  #srch  = 0
              let $O_match = 'N'
          else
              if  $last  = '_'
              and #srch <> #posS
              and $dubb <> '%_'
                  let $O_match = 'N'
                  let #posP    = #lenP + 1
                  break
              else
                 let #posS = #srch + length($srch)
                 let $srch = ''
              end-if
          end-if
       end-if
       !  Special Processing Characters
       evaluate $char
          when = $I_escape
            let $esc     = 'Y'
          when = '_'
            if $esc      = 'Y'
               let $srch = $srch || $char
            else
               if  #posS <= #lenS
                   let #posS = #posS + 1
               else
                   let #posP = #lenP + 1
                   let $O_match = 'N'
                   break
               end-if
               let $dubb = $last || $char
               let $last = $char
            end-if
            let $esc     = 'N'
          when = '%'
            if $esc      = 'Y'
               let $srch = $srch || $char
            else
               let $dubb = $last || $char
               let $last = $char
            end-if
            let $esc     = 'N'
       end-evaluate
   else
       let $srch    = $srch || $char
       let $esc     = 'N'
   end-if
   let #posP = #posP + 1
end-while
!   PHASE 2 - Matching "residue" outside Pattern Loop
if $O_match = 'Y'
   if $srch <> ''
      if instr($special,$char,1) = 0
         let #srch  = instr($I_string, $srch, #posS)
         if  #srch  = 0
             let $O_match = 'N'
         else
             if  $last  = '_'
             and #srch <> #posS
             and $dubb <> '%_'
                 let $O_match = 'N'
             else
                 let #posS = #srch + length($srch)
                 if  #posS < #lenS + 1
                     let $O_match = 'N'
                 end-if
             end-if
         end-if
      end-if
   else
      if  $char  = '_'
      and #posS  < #lenS + 1
          let $O_match = 'N'
      end-if
   end-if
end-if
end-procedure
#endif
                    
     |