!= GTHST_NMLINFO åž‹ã®å¤‰æ•°ã®çµ‚äº†å‡¦ç†
!= Deconstructor of "GTHST_NMLINFO"
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: hstnmlinfoclose.f90,v 1.2 2009-06-01 15:17:18 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2007-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!
  subroutine HstNmlInfoClose( gthstnml, err )
    !
    ! GTHST_NMLINFO åž‹ã®å¤‰æ•°ã®çµ‚äº†å‡¦ç†ã‚’è¡Œã„ã¾ã™.
    !
    ! ã“ã®ã‚µãƒ–ãƒ«ãƒ¼ãƒãƒ³ã‚’ä½¿ç”¨ã™ã‚‹å‰ã«, *gthstnml* ã«æ ¼ç´ã•ã‚Œã¦ã„ã‚‹
    ! gtool_history_types#GT_HISTORY åž‹ã®å…¨ã¦ã®å¤‰æ•°ã«å¯¾ã—ã¦,
    ! gtool_history_generic#HistoryClose ã‚’ç”¨ã„ã¦çµ‚äº†å‡¦ç†ã‚’è¡Œã£ã¦ãã ã•ã„.
    ! çµ‚äº†å‡¦ç†ã•ã‚Œã¦ã„ãªã„ã‚‚ã®ãŒã‚ã‚‹å ´åˆ,
    ! ãƒ—ãƒã‚°ãƒ©ãƒ ã¯ã‚¨ãƒ©ãƒ¼ã‚’ç™ºç”Ÿã•ã›ã¾ã™.
    !
    ! ãªãŠ, ä¸Žãˆã‚‰ã‚ŒãŸ *gthstnml* ãŒ HstNmlInfoCreate ã«ã‚ˆã£ã¦åˆæœŸè¨å®š
    ! ã•ã‚Œã¦ã„ãªã„å ´åˆ, ãƒ—ãƒã‚°ãƒ©ãƒ ã¯ã‚¨ãƒ©ãƒ¼ã‚’ç™ºç”Ÿã•ã›ã¾ã™.
    !
    ! Deconstructor of "GTHST_NMLINFO".
    !
    ! Terminate all "gtool_history_types#GT_HISTORY" variables in *gthstnml*
    ! by "gtool_history_generic#HistoryClose" before this subroutine is used.
    ! If unterminated variables remain,
    ! error is occurred.
    !
    ! Note that if *gthstnml* is not initialized by "HstNmlInfoCreate" yet,
    ! error is occurred.
    !
    use gtool_history_nmlinfo_types, only: GTHST_NMLINFO, GTHST_NMLINFO_ENTRY
    use gtool_history_nmlinfo_internal, only: ListLast
    use gtool_history, only: HistoryInitialized
    use dc_trace, only: BeginSub, EndSub, DbgMessage
    use dc_string, only: PutLine, Printf, Split, StrInclude, StoA, JoinChar
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT, HST_ENOTTERMGTHIST
    implicit none
    type(GTHST_NMLINFO), intent(inout):: gthstnml
    logical, intent(out), optional:: err
                              ! ä¾‹å¤–å‡¦ç†ç”¨ãƒ•ãƒ©ã‚°.
                              ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆã§ã¯, ã“ã®æ‰‹ç¶šãå†…ã§ã‚¨ãƒ©ãƒ¼ãŒ
                              ! ç”Ÿã˜ãŸå ´åˆ, ãƒ—ãƒã‚°ãƒ©ãƒ ã¯å¼·åˆ¶çµ‚äº†ã—ã¾ã™.
                              ! å¼•æ•° *err* ãŒä¸Žãˆã‚‰ã‚Œã‚‹å ´åˆ,
                              ! ãƒ—ãƒã‚°ãƒ©ãƒ ã¯å¼·åˆ¶çµ‚äº†ã›ãš, ä»£ã‚ã‚Šã«
                              ! *err* ã« .true. ãŒä»£å…¥ã•ã‚Œã¾ã™.
                              !
                              ! Exception handling flag.
                              ! By default, when error occur in
                              ! this procedure, the program aborts.
                              ! If this *err* argument is given,
                              ! .true. is substituted to *err* and
                              ! the program does not abort.

    !-----------------------------------
    !  ä½œæ¥å¤‰æ•°
    !  Work variables
    type(GTHST_NMLINFO_ENTRY), pointer:: hptr =>null()
    type(GTHST_NMLINFO_ENTRY), pointer:: hptr_prev =>null()
    integer:: stat
    character(STRING):: cause_c
    character(*), parameter:: subname = 'HstNmlInfoClose'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

    !-----------------------------------------------------------------
    !  åˆæœŸè¨å®šã®ãƒã‚§ãƒƒã‚¯
    !  Check initialization
    !-----------------------------------------------------------------
    if ( .not. gthstnml % initialized ) then
      stat = DC_ENOTINIT
      cause_c = 'GTHST_NMLINFO'
      goto 999
    end if

    !-----------------------------------------------------------------
    !  "GTHST_NMLINFO" ã®è¨å®šã®æ¶ˆåŽ»
    !  Clear the settings for "GTHST_NMLINFO"
    !-----------------------------------------------------------------
    do
      hptr => gthstnml % gthstnml_list
      call ListLast( gthstnml_list = hptr, & ! (inout)
        & previous = hptr_prev )             ! (out)
      call DbgMessage( 'remove entry (%c)', c1 = trim(hptr % name) )
      if ( trim( hptr % name ) == '' ) exit
      if ( .not. gthstnml % define_mode ) then
        if ( HistoryInitialized( hptr % history ) ) then
          stat = HST_ENOTTERMGTHIST
          cause_c = hptr % name
          goto 999
        end if
      end if
      deallocate( hptr )
      nullify( hptr_prev % next )
    end do
    deallocate( gthstnml % gthstnml_list )

    !-----------------------------------------------------------------
    !  çµ‚äº†å‡¦ç†, ä¾‹å¤–å‡¦ç†
    !  Termination and Exception handling
    !-----------------------------------------------------------------
    gthstnml % initialized = .false.
    gthstnml % define_mode = .true.
999 continue
    nullify( hptr )
    call StoreError( stat, subname, err, cause_c )
    call EndSub( subname )
  end subroutine HstNmlInfoClose