iounits.f 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2. !
  3. ! This module is a dynamic I/O unit manager. It keeps track of
  4. ! which units are in use and reserves units for stdin, stdout, and
  5. ! stderr.
  6. !
  7. !-----------------------------------------------------------------------
  8. !
  9. ! CVS:$Id: iounits.f,v 1.2 2000/04/19 21:56:25 pwjones Exp $
  10. !
  11. ! Copyright (c) 1997, 1998 the Regents of the University of
  12. ! California.
  13. !
  14. ! This software and ancillary information (herein called software)
  15. ! called SCRIP is made available under the terms described here.
  16. ! The software has been approved for release with associated
  17. ! LA-CC Number 98-45.
  18. !
  19. ! Unless otherwise indicated, this software has been authored
  20. ! by an employee or employees of the University of California,
  21. ! operator of the Los Alamos National Laboratory under Contract
  22. ! No. W-7405-ENG-36 with the U.S. Department of Energy. The U.S.
  23. ! Government has rights to use, reproduce, and distribute this
  24. ! software. The public may copy and use this software without
  25. ! charge, provided that this Notice and any statement of authorship
  26. ! are reproduced on all copies. Neither the Government nor the
  27. ! University makes any warranty, express or implied, or assumes
  28. ! any liability or responsibility for the use of this software.
  29. !
  30. ! If software is modified to produce derivative works, such modified
  31. ! software should be clearly marked, so as not to confuse it with
  32. ! the version available from Los Alamos National Laboratory.
  33. !
  34. !***********************************************************************
  35. module iounits
  36. !-----------------------------------------------------------------------
  37. use kinds_mod ! defines data types
  38. implicit none
  39. !-----------------------------------------------------------------------
  40. logical (kind=log_kind), dimension(99), save ::
  41. & unit_free ! flags to determine whether unit is free for use
  42. integer (kind=int_kind), parameter ::
  43. & stdin = 5, ! reserves unit for standard input
  44. & stdout = 6, ! reserves unit for standard output
  45. & stderr = 6 ! reserves unit for standard error
  46. !***********************************************************************
  47. contains
  48. !***********************************************************************
  49. subroutine get_unit(iunit)
  50. !-----------------------------------------------------------------------
  51. !
  52. ! This routine returns the next available I/O unit number.
  53. !
  54. !-----------------------------------------------------------------------
  55. !-----------------------------------------------------------------------
  56. !
  57. ! output variables
  58. !
  59. !-----------------------------------------------------------------------
  60. integer (kind=int_kind), intent(out) ::
  61. & iunit ! next free I/O unit
  62. !-----------------------------------------------------------------------
  63. !
  64. ! local variables
  65. !
  66. !-----------------------------------------------------------------------
  67. integer (kind=int_kind) :: n
  68. logical (kind=log_kind), save :: first_call = .true.
  69. !-----------------------------------------------------------------------
  70. !
  71. ! if this is the first call, reserve stdout, stdin and stderr
  72. !
  73. !-----------------------------------------------------------------------
  74. if (first_call) then
  75. unit_free = .true.
  76. unit_free(stdin) = .false.
  77. unit_free(stdout) = .false.
  78. unit_free(stderr) = .false.
  79. first_call = .false.
  80. endif
  81. !-----------------------------------------------------------------------
  82. !
  83. ! search for next available unit
  84. !
  85. !-----------------------------------------------------------------------
  86. srch_unit: do n=1,99
  87. if (unit_free(n)) then
  88. iunit = n
  89. unit_free(n) = .false.
  90. exit srch_unit
  91. endif
  92. end do srch_unit
  93. !-----------------------------------------------------------------------
  94. end subroutine get_unit
  95. !***********************************************************************
  96. subroutine release_unit(iunit)
  97. !-----------------------------------------------------------------------
  98. !
  99. ! This routine releases the specified unit and closes the file.
  100. !
  101. !-----------------------------------------------------------------------
  102. !-----------------------------------------------------------------------
  103. !
  104. ! input variables
  105. !
  106. !-----------------------------------------------------------------------
  107. integer (kind=int_kind), intent(in) ::
  108. & iunit ! I/O unit to release
  109. !-----------------------------------------------------------------------
  110. !
  111. ! closes I/O unit and declares it free
  112. !
  113. !-----------------------------------------------------------------------
  114. unit_free(iunit) = .true.
  115. close(iunit)
  116. !-----------------------------------------------------------------------
  117. end subroutine release_unit
  118. !***********************************************************************
  119. end module iounits
  120. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!