Skip to content

Commit ac61bf4

Browse files
committed
Implement open(filename, mode) and use it
1 parent 924ee54 commit ac61bf4

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

src/stdlib_experimental_io.f90

+36-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module stdlib_experimental_io
22
use iso_fortran_env, only: sp=>real32, dp=>real64, qp=>real128
3+
use stdlib_experimental_error, only: error_stop
34
implicit none
45
private
5-
public :: loadtxt, savetxt
6+
public :: loadtxt, savetxt, open
67

78
interface loadtxt
89
module procedure sloadtxt
@@ -46,7 +47,7 @@ subroutine sloadtxt(filename, d)
4647
integer :: s
4748
integer :: nrow,ncol,i
4849

49-
open(newunit=s, file=filename, status="old", action="read")
50+
s = open(filename)
5051

5152
! determine number of columns
5253
ncol = number_of_columns(s)
@@ -89,7 +90,7 @@ subroutine dloadtxt(filename, d)
8990
integer :: s
9091
integer :: nrow,ncol,i
9192

92-
open(newunit=s, file=filename, status="old", action="read")
93+
s = open(filename)
9394

9495
! determine number of columns
9596
ncol = number_of_columns(s)
@@ -132,7 +133,7 @@ subroutine qloadtxt(filename, d)
132133
integer :: s
133134
integer :: nrow,ncol,i
134135

135-
open(newunit=s, file=filename, status="old", action="read")
136+
s = open(filename)
136137

137138
! determine number of columns
138139
ncol = number_of_columns(s)
@@ -164,7 +165,7 @@ subroutine ssavetxt(filename, d)
164165
! call savetxt("log.txt", data)
165166

166167
integer :: s, i
167-
open(newunit=s, file=filename, status="replace", action="write")
168+
s = open(filename, "w")
168169
do i = 1, size(d, 1)
169170
write(s, *) d(i, :)
170171
end do
@@ -187,7 +188,7 @@ subroutine dsavetxt(filename, d)
187188
! call savetxt("log.txt", data)
188189

189190
integer :: s, i
190-
open(newunit=s, file=filename, status="replace", action="write")
191+
s = open(filename, "w")
191192
do i = 1, size(d, 1)
192193
write(s, *) d(i, :)
193194
end do
@@ -210,7 +211,7 @@ subroutine qsavetxt(filename, d)
210211
! call savetxt("log.txt", data)
211212

212213
integer :: s, i
213-
open(newunit=s, file=filename, status="replace", action="write")
214+
s = open(filename, "w")
214215
do i = 1, size(d, 1)
215216
write(s, *) d(i, :)
216217
end do
@@ -268,4 +269,32 @@ logical function whitechar(char) ! white character
268269
end if
269270
end function
270271

272+
integer function open(filename, mode) result(u)
273+
! Open a file
274+
!
275+
! To open a file to read:
276+
!
277+
! u = open("somefile.txt") # The default `mode` is "r"
278+
! u = open("somefile.txt", "r")
279+
!
280+
! To open a file to write:
281+
!
282+
! u = open("somefile.txt", "w")
283+
284+
character(*), intent(in) :: filename
285+
character(*), intent(in), optional :: mode
286+
character(:), allocatable :: mode_
287+
mode_ = "r"
288+
if (present(mode)) mode_ = mode
289+
! Note: the Fortran standard says that the default values for `status` and
290+
! `action` are processor dependent, so we have to explicitly set them below
291+
if (mode_ == "r") then
292+
open(newunit=u, file=filename, status="old", action="read")
293+
else if (mode_ == "w") then
294+
open(newunit=u, file=filename, status="replace", action="write")
295+
else
296+
call error_stop("Unsupported mode")
297+
end if
298+
end function
299+
271300
end module

0 commit comments

Comments
 (0)