midrange.com code scratchpad
Name:
Dennis Lovelady
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
02/03/2010 05:27:19 pm
IP:
Logged
Description:
Regular Expressions header file (Scott Klement)
Code:
  1.       ** Header file for calling the "Regular Expression" functions
  2.       **   provided by the ILE C Runtime Library from an RPG IV
  3.       **   program.                 Scott Klement, 2001-05-04
  4.       **                       Converted to qualified DS 2003-11-29
  5.       **
  6.      D/if defined(REGEX_H)
  7.      D/eof
  8.      D/endif
  9.      D/define REGEX_H
  10.  
  11.  
  12.       **------------------------------------------------------------
  13.       * cflags for regcomp()
  14.       **------------------------------------------------------------
  15.      D REG_BASIC       C                   CONST(0)
  16.      D REG_EXTENDED    C                   CONST(1)
  17.      D REG_ICASE       C                   CONST(2)
  18.      D REG_NEWLINE     C                   CONST(4)
  19.      D REG_NOSUB       C                   CONST(8)
  20.  
  21.  
  22.       **------------------------------------------------------------
  23.       * eflags for regexec()
  24.       **------------------------------------------------------------
  25.      D REG_NOTBOL      C                   CONST(256)
  26.      D REG_NOTEOL      C                   CONST(512)
  27.  
  28.  
  29.       **------------------------------------------------------------
  30.       *  errors returned
  31.       **------------------------------------------------------------
  32.      D REG_NOMATCH     C                   CONST(1)
  33.      D REG_BADPAT      C                   CONST(2)
  34.      D REG_ECOLLATE    C                   CONST(3)
  35.      D REG_ECTYPE      C                   CONST(4)
  36.      D REG_EESCAPE     C                   CONST(5)
  37.      D REG_ESUBREG     C                   CONST(6)
  38.      D REG_EBRACK      C                   CONST(7)
  39.      D REG_EPAREN      C                   CONST(8)
  40.      D REG_EBRACE      C                   CONST(9)
  41.      D REG_BADBR       C                   CONST(10)
  42.      D REG_ERANGE      C                   CONST(11)
  43.      D REG_ESPACE      C                   CONST(12)
  44.      D REG_BADRPT      C                   CONST(13)
  45.      D REG_ECHAR       C                   CONST(14)
  46.      D REG_EBOL        C                   CONST(15)
  47.      D REG_EEOL        C                   CONST(16)
  48.      D REG_ECOMP       C                   CONST(17)
  49.      D REG_EEXEC       C                   CONST(18)
  50.  
  51.  
  52.       **------------------------------------------------------------
  53.       *  Structure of a compiled regular expression:
  54.       *
  55.       *     #define __REG_SUBEXP_MAX       9
  56.       *     typedef struct {
  57.       *         size_t        re_nsub;
  58.       *         void          *re_comp;
  59.       *         int           re_cflags;
  60.       *         size_t        re_erroff;
  61.       *         size_t        re_len;
  62.       *         _LC_colval_t  re_ucoll[2];
  63.       *         void          *re_lsub[__REG_SUBEXP_MAX+1];
  64.       *         void          *re_esub[__REG_SUBEXP_MAX+1];
  65.       *         unsigned char re_map[256];
  66.       *         mbstate_t     re_shift;
  67.       *         short         re_dbcs;
  68.       *     } regex_t;
  69.       **------------------------------------------------------------
  70.      D REG_SUBEXP_MAX  C                   10
  71.      D regex_t         DS                  qualified
  72.      D                                     align based(prototype_only)
  73.      D   re_nsub                     10I 0
  74.      D   re_comp                       *
  75.      D   re_cflags                   10I 0
  76.      D   re_erroff                   10I 0
  77.      D   re_len                      10I 0
  78.      D   re_ucoll                    10I 0 dim(2)
  79.      D   re_lsub                       *   DIM(REG_SUBEXP_MAX)
  80.      D   re_esub                       *   DIM(REG_SUBEXP_MAX)
  81.      D   re_map                     256A
  82.      D   re_shift                     5I 0
  83.      D   re_dbcs                      5I 0
  84.  
  85.  
  86.       **------------------------------------------------------------
  87.       *  structure used to report matches found by regexec()
  88.       *
  89.       *     typedef struct {
  90.       *         _off_t     rm_so; /* offset of substring             */
  91.       *         mbstate_t  rm_ss; /* shift state at start of subst   */
  92.       *         _off_t     rm_eo; /* offset of next char after subst */
  93.       *         mbstate_t  rm_es; /* shift state at end of subst     */
  94.       *     } regmatch_t;
  95.       *
  96.       * NOTE: It's important to remember that C starts numbering
  97.       *    string positions with '0' and RPG starts with '1'.
  98.       *    Thus, rm_so+1 is the first char in substring, rm_eo is
  99.       *    the last char in the substring in RPG.
  100.       **------------------------------------------------------------
  101.      D regmatch_t      DS                  qualified
  102.      D                                     align based(prototype_only)
  103.      D   rm_so                       10I 0
  104.      D   rm_ss                        5I 0
  105.      D   rm_eo                       10I 0
  106.      D   rm_es                        5I 0
  107.  
  108.  
  109.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  110.       * regcomp() -- Compile a Regular Expression ("RE")
  111.       *
  112.       *     int regcomp(regex_t *preg, const char *pattern,
  113.       *              int cflags);
  114.       *
  115.       * where:
  116.       *       preg (output) = the compiled regular expression.
  117.       *    pattern (input)  = the RE to be compiled.
  118.       *     cflags (input)  = the sum of the cflag constants
  119.       *                       (listed above) for this RE.
  120.       *
  121.       * Returns 0 = success, otherwise an error number.
  122.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  123.      D regcomp         PR            10I 0 extproc('regcomp')
  124.      D   preg                              like(regex_t)
  125.      D   pattern                       *   value options(*string)
  126.      D   cflags                      10I 0 value
  127.  
  128.  
  129.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  130.       * regexec() -- Execute a compiled Regular Expression ("RE")
  131.       *
  132.       *     int regexec(const regex_t *preg, const char *string,
  133.       *              size_t nmatch, regmatch_t *pmatch, int eflags);
  134.       *
  135.       * where:
  136.       *       preg (input)  = the compiled regular expression
  137.       *                       (the output of regcomp())
  138.       *     string (input)  = string to run the RE upon
  139.       *     nmatch (input)  = the number of matches to return.
  140.       *     pmatch (output) = array of regmatch_t DS's
  141.       *                       showing what matches were found.
  142.       *     eflags (input)  = the sum of the flags (constants
  143.       *                       provided above) modifying the RE
  144.       *
  145.       * Returns 0 = success, otherwise an error number.
  146.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  147.      D regexec         PR            10I 0 extproc('regexec')
  148.      D   preg                              like(regex_t) const
  149.      D   string                        *   value options(*string)
  150.      D   nmatch                      10U 0 value
  151.      D   pmatch                            like(regmatch_t) dim(100)
  152.      D                                     options(*varsize)
  153.      D   eflags                      10I 0 value
  154.  
  155.  
  156.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  157.       * regerror() -- return error information from regcomp/regexec
  158.       *
  159.       *   size_t regerror(int errcode, const regex_t *preg,
  160.       *              char *errbuf, size_t errbuf_size);
  161.       *
  162.       *  where:
  163.       *    errcode (input)  = the error code to return info on
  164.       *                      (obtained as the return value from
  165.       *                      either regcomp() or regexec())
  166.       *       preg (input)  = the (compiled) RE to return the
  167.       *                      error for.
  168.       *     errbuf (output) = buffer containing human-readable
  169.       *                      error message.
  170.       * errbuf_size (input) = size of errbuf (max length of msg
  171.       *                      that will be returned)
  172.       *
  173.       * returns:  length of buffer needed to get entire error msg
  174.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  175.      D regerror        PR            10U 0 extproc('regerror')
  176.      D   errcode                     10I 0 value
  177.      D   preg                              like(regex_t) const
  178.      D   errbuf                        *   value
  179.      D   errbuf_size                 10I 0 value
  180.  
  181.  
  182.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  183.       * regfree() -- free memory locked by Regular Expression
  184.       *
  185.       *    void regfree(regex_t *preg);
  186.       *
  187.       *   where:
  188.       *        preg (input) = regular expression to free mem for.
  189.       *
  190.       *   NOTE:  regcomp() will always allocate extra memory
  191.       *        to be pointed to by the various pointers in
  192.       *        the regex_t structure.  If you don't call this,
  193.       *        that memory will never be returned to the system!
  194.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  195.      D regfree         PR                  extproc('regfree')
  196.      D   preg                              like(regex_t)
  197. 
© 2004-2019 by midrange.com generated in 0.011s valid xhtml & css