regerror Function

public function regerror(this, errcode) result(errmsg)

NAME

regerror(3f) - [M_regex] maps a non-zero errcode from either
regcomp(3f) or regexec(3f) to a human-readable, printable message.

SYNOPSIS

function regerror(this,errcode) result(errmsg)

  type(regex_type), intent(in) :: this
  integer, intent(in)          :: errcode
  character(len=:),allocatable :: errmsg

DESCRIPTION

  The REGCOMP() function compiles an RE written as a string into
  an internal form, REGEXEC() matches that internal form against
  a string and reports results, REGERROR() transforms error codes
  from either into human-readable messages, and REGFREE() frees any
  dynamically-allocated storage used by the internal form of an RE.

  These routines implement IEEE Std 1003.2 (POSIX.2) regular
  expressions (RE s); see re_format(7).

  Specifically, The REGERROR(3f) function maps a non-zero errcode from
  either REGCOMP(3f) or REGEXEC(3f) to a human-readable, printable
  message. If THIS is non-NULL, the error code should have arisen
  from use of the regex_t pointed to by THIS, and if the error code
  came from REGCOMP(), it should have been the result from the most
  recent REGCOMP() using that regex_t.

  Non-zero error codes from regcomp() and regexec() include the
  following:

  REG_NOMATCH   The regexec() function failed to match
  REG_BADPAT    invalid regular expression
  REG_ECOLLATE  invalid collating element
  REG_ECTYPE    invalid character class
  REG_EESCAPE      \    applied to unescapable character
  REG_ESUBREG   invalid backreference number
  REG_EBRACK    brackets    [ ]    not balanced
  REG_EPAREN    parentheses    ( )    not balanced
  REG_EBRACE    braces    { }    not balanced
  REG_BADBR     invalid repetition count(s) in    { }
  REG_ERANGE    invalid character range in    [ ]
  REG_ESPACE    ran out of memory
  REG_BADRPT       ?   ,    *   , or    +    operand invalid
  REG_EMPTY     empty (sub)expression
  REG_ASSERT    cannot happen - you found a bug
  REG_INVARG    invalid argument, e.g. negative-length string
  REG_ILLSEQ    illegal byte sequence (bad multibyte character)

OPTIONS

  THIS         a compiled regular expression from REGCOMP(3f)
  ERRCODE      the last error code generated by REGCOMP(3f) or REGEXEC(3f)
  ERRMSG       the error message
  ERRMSG_LEN   size required of ERRMSG to display the entire message

EXAMPLE

Sample program

program demo_regerror
use M_regex, only: regex_type, regcomp, regexec, regmatch, regfree, regerror
implicit none
type(regex_type)             :: regex
integer,parameter            :: maxmatch=10
integer                      :: matches(2,maxmatch)
integer                      :: i, istat

character(len=:),allocatable :: input_line
character(len=:),allocatable :: expression
logical                      :: match

   expression= "([0-9\.\-\*\/]+)+"
   expression= "([0-9\.\-\*\/+)+"  ! intentionally bad RE (Regular Expression)
   input_line= "30*0 250*1 5 6 7"
   call regcomp(regex,expression,'x',status=istat)
   if (istat/=0) then
     write(*,'("Regex runtime error in regcomp(3f):",a,", expression=",a)') regerror(regex,istat),expression
     stop 1
   endif
   match=regexec(regex,input_line,matches,status=istat)
   if (istat/=0) then
     write(*,'("Regex runtime error in regexec:(3f)",a)') regerror(regex,istat)
     stop 2
   endif
   if(match)then
      do i=1,maxmatch
         if(matches(1,i).le.0)exit
         write(*,*) 'match="',regmatch(i,input_line,matches),'"'
      enddo
   endif
   call regfree(regex)

end program demo_regerror

Expected output:

Regex runtime error in regcomp(3f):brackets ([ ]) not balanced , expression=([0-9\.\-\*\/+)+
stop 1

Arguments

Type IntentOptional Attributes Name
type(regex_type), intent(in) :: this
integer, intent(in) :: errcode

Return Value character(len=:), allocatable


Source Code

function regerror(this,errcode) result(errmsg)

! ident_4="@(#) M_exec regerror(3f) describe error generated by regcomp(3f) or regexec(3f)"

type(regex_type), intent(in)                :: this
integer,intent(in)                          :: errcode
character(len=:),allocatable                :: errmsg

   integer                                  :: errmsg_len
   character(len=1,kind=c_char)             :: buffer2(1024)
   integer                                  :: i
   interface
      function C_regerror(preg ,errc, errbuf)result(regerror)bind(C,name="my_regerror")
        import c_int, c_char, c_ptr, c_size_t
        type(C_ptr),intent(in),value        :: preg
        integer(C_size_t),value             :: errc
        character(len=1,kind=c_char)        :: errbuf(*)
        integer(C_size_t)                   :: regerror
      end function C_regerror
   end interface

   buffer2=' '
   errmsg_len = C_regerror(this%preg,int(errcode,C_size_t), buffer2)
   allocate(character(len=errmsg_len) :: errmsg)
   errmsg(:)=' '
   do i=1,min(errmsg_len,1024)
      if(buffer2(i).eq.char(0))exit
      errmsg(i:i)=buffer2(i)
   enddo

end function regerror