Tuesday 18 October 2011

RPGLE - Scan memory (or userspace) for a given string

RPGLE  - Scan memory (or userspace) for a given string

*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      *  ScanSpc():  Scan memory (or userspace) for a given string
      *
      *       Space = pointer to area of memory or user space to scan
      *      String = string to search for
      *     SpcSize = size of space to search
      *
      *  Returns 0 if nothing found, otherwise the position of the
      *             string in the space.
      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P ScanSpc         B
     D ScanSpc         PI            10I 0
     D   Space                         *   value
     D   String                     256A   const varying options(*varsize)
     D   SpcSize                     10I 0 value

      * memchr(): Search memory for a character
     D memchr          PR              *   extproc('memchr')
     D   buf                           *   value
     D   chartofind                  10I 0 value
     D   bufsize                     10I 0 value

      * memcmp(): Compare two areas of memory
     D memcmp          PR            10I 0 extproc('memcmp')
     D   buf1                          *   value
     D   buf2                     65535A   const options(*varsize)
     D   size                        10I 0 value

     D                 DS
     D  Char                          1A
     D  Num                           3U 0 overlay(Char)

     D p_search        s               *
     D p_found         s               *

      /free

           if (%len(String) < 1);
              return 0;
           endif;

           Char = %subst(String: 1: 1);

           p_search = Space;

           dou (memcmp(p_found: String: %len(String)) = 0);
               p_found = memchr(p_search: Num: SpcSize);
               if (p_found = *NULL);
                  return 0;
               endif;
               p_search = p_found + 1;
           enddo;

           return (p_found - space) + 1;

      /end-free
     P                 E


No comments:

Post a Comment