!
!-----------------------------------------------------------------------
!
!Purpose:
! Dispatch to a calculation module in DONJON. ANSI-C interoperable.
!
!Copyright:
! Copyright (C) 2009 Ecole Polytechnique de Montreal
! This library is free software; you can redistribute it and/or
! modify it under the terms of the GNU Lesser General Public
! License as published by the Free Software Foundation; either
! version 2.1 of the License, or (at your option) any later version.
!
!Author(s): A. Hebert
!
!-----------------------------------------------------------------------
!
integer(c_int) function donmod(cmodul, nentry, hentry, ientry, jentry, &
               kentry, hparam_c) bind(c)
!
   use GANLIB
   implicit none
!----
!  subroutine arguments
!----
   character(kind=c_char), dimension(*) :: cmodul
   integer(c_int), value :: nentry 
   character(kind=c_char), dimension(13,*) :: hentry
   integer(c_int), dimension(nentry) :: ientry, jentry
   type(c_ptr), dimension(nentry) :: kentry
   character(kind=c_char), dimension(73,*) :: hparam_c
!----
!  local variables
!----
   integer :: i, ier
   character :: hmodul*12, hsmg*131, hparam*72
   character(len=12), allocatable :: hentry_f(:)
   type(c_ptr) :: my_file
   integer, external :: DONDRV
!
   allocate(hentry_f(nentry))
   call STRFIL(hmodul, cmodul)
   do i=1,nentry
      call STRFIL(hentry_f(i), hentry(1,i))
      if(ientry(i) >= 3) then
!        open a Fortran file.
         call STRFIL(hparam, hparam_c(1,i))
         my_file=FILOPN(hparam,jentry(i),ientry(i)-1,0)
         if(.not.c_associated(my_file)) then
            write(hsmg,'(29hdonmod: unable to open file '',a12,2h''.)') hentry_f(i)
            call XABORT(hsmg)
         endif
         kentry(i)=my_file
      endif
   enddo
!  ----------------------------------------------------------
   donmod=DONDRV(hmodul,nentry,hentry_f,ientry,jentry,kentry)
!  ----------------------------------------------------------
   do i=1,nentry
      if(ientry(i) >= 3) then
!        close a Fortran file.
         ier=FILCLS(kentry(i),1)
         if(ier < 0) then
            write(hsmg,'(30hdonmod: unable to close file '',a12,2h''.)') hentry_f(i)
            call XABORT(hsmg)
         endif
      endif
   enddo
   deallocate(hentry_f)
   flush(6)
   return
end function donmod
