This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_io_fm.f90
126 lines (112 loc) · 4 KB
/
file_io_fm.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
! file_io_fm.f90
submodule (file_io) file_io_tfm
contains
! ------------------------------------------------------------------------------
!> @brief Returns the unit value for the file object.
!!
!! @param[in,out] this The file_manager object.
!!
!! @return The Fortran unit value for this stream.
module function fm_get_unit(this) result(rst)
class(file_manager), intent(in) :: this
integer(int32) :: rst
rst = this%m_unit
end function
! ------------------------------------------------------------------------------
!> @brief Sets the Fortran unit value to associate with this file.
module subroutine fm_set_unit(this, x)
! Arguments
class(file_manager), intent(inout) :: this
integer(int32), intent(in) :: x
this%m_unit = x
end subroutine
! ------------------------------------------------------------------------------
!> @brief Determines if the file is already opened.
!!
!! @param[in] this The file_manager object.
!!
!! @return Returns true if the file is opened; else, false.
module function fm_get_is_opened(this) result(rst)
! Arguments
class(file_manager), intent(in) :: this
logical :: rst
! Process
if (this%m_unit == -1) then
rst = .false.
else
inquire(unit = this%m_unit, opened = rst)
end if
rst = .true.
end function
! ------------------------------------------------------------------------------
!> @brief Closes the file.
!!
!! @param[in,out] this The file_manager object.
!! @param[in] del An optional input, that if set, determines if the file
!! should be deleted once closed. The default is false such that the
!! file remains.
module subroutine fm_close(this, del)
! Arguments
class(file_manager), intent(inout) :: this
logical, intent(in), optional :: del
! Local Variables
logical :: check
! Process
check = .false.
if (present(del)) check = del
if (this%is_open()) then
if (check) then
close(this%get_unit(), status = "delete")
else
close(this%get_unit())
end if
this%m_unit = -1
end if
end subroutine
! ------------------------------------------------------------------------------
!> @brief Forces closure of the file, if open, whenever the object goes out
!! of scope.
!!
!! @param[in,out] this The file_manager object.
module subroutine fm_clean_up(this)
type(file_manager), intent(inout) :: this
call this%close(.false.)
end subroutine
! ------------------------------------------------------------------------------
!> @brief Gets the filename.
!!
!! @param[in] this The file_manager object.
!!
!! @return The filename.
pure module function fm_get_fname(this) result(rst)
class(file_manager), intent(in) :: this
character(len = :), allocatable :: rst
rst = this%m_fname
end function
! --------------------
!> @brief Sets the filename.
!!
!! @param[in,out] this The file_manager object.
!! @param[in] x The filename.
module subroutine fm_set_fname(this, x)
class(file_manager), intent(inout) :: this
character(len = *), intent(in) :: x
this%m_fname = x
end subroutine
! ------------------------------------------------------------------------------
!> @brief Gets the size of the currently open file.
!!
!! @param[in] this The file_manager object.
!!
!! @return The file size, in bytes.
module function fm_get_size(this) result(rst)
! Arguments
class(file_manager), intent(in) :: this
integer(int32) :: rst
! Process
rst = 0
if (.not.this%is_open()) return
inquire(file = this%get_filename(), size = rst)
end function
! ------------------------------------------------------------------------------
end submodule