Skip to content

Addition of access in the API of open function #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 41 additions & 27 deletions src/stdlib_experimental_io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ integer function number_of_rows_numeric(s)

end function

integer function open(filename, mode, iostat) result(u)
integer function open(filename, mode, iostat, access) result(u)
! Open a file
!
! To open a file to read:
Expand All @@ -287,6 +287,7 @@ integer function open(filename, mode, iostat) result(u)

character(*), intent(in) :: filename
character(*), intent(in), optional :: mode
character(*), intent(in), optional :: access
integer, intent(out), optional :: iostat

integer :: io_
Expand All @@ -298,52 +299,65 @@ integer function open(filename, mode, iostat) result(u)

select case (mode_(1:2))
case('r')
action_='read'
position_='asis'
status_='old'
action_ = 'read'
position_ = 'asis'
status_ = 'old'
case('w')
action_='write'
position_='asis'
status_='replace'
action_ = 'write'
position_ = 'asis'
status_ = 'replace'
case('a')
action_='write'
position_='append'
status_='old'
action_ = 'write'
position_ = 'append'
status_ = 'old'
case('x')
action_='write'
position_='asis'
status_='new'
action_ = 'write'
position_ = 'asis'
status_ = 'new'
case('r+')
action_='readwrite'
position_='asis'
status_='old'
action_ = 'readwrite'
position_ = 'asis'
status_ = 'old'
case('w+')
action_='readwrite'
position_='asis'
status_='replace'
action_ = 'readwrite'
position_ = 'asis'
status_ = 'replace'
case('a+')
action_='readwrite'
position_='append'
status_='old'
action_ = 'readwrite'
position_ = 'append'
status_ = 'old'
case('x+')
action_='readwrite'
position_='asis'
status_='new'
action_ = 'readwrite'
position_ = 'asis'
status_ = 'new'
case default
call error_stop("Unsupported mode: "//mode_(1:2))
end select

select case (mode_(3:3))
case('t')
form_='formatted'
form_ = 'formatted'
case('b')
form_='unformatted'
form_ = 'unformatted'
case default
call error_stop("Unsupported mode: "//mode_(3:3))
end select

access_ = 'stream'

if (present(access)) then
select case (trim(adjustl(access)))
! case('direct')
! access_ = 'direct'
case('sequential')
access_ = 'sequential'
case('stream')
access_ = 'stream'
case default
call error_stop("Unsupported access: "//trim(access))
end select
end if

if (present(iostat)) then
open(newunit=u, file=filename, &
action = action_, position = position_, status = status_, &
Expand Down
4 changes: 3 additions & 1 deletion src/tests/io/Makefile.manual
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ PROGS_SRC = test_loadtxt.f90 \
test_parse_mode.f90 \
test_open.f90

CLEAN_FILES = tmp.dat tmp_qp.dat io_open.dat io_open.stream
CLEAN_FILES = tmp.dat tmp_qp.dat \
io_open_seq.dat io_open_stream.dat \
io_open_seq.bin io_open_stream.bin


include ../Makefile.manual.test.mk
60 changes: 55 additions & 5 deletions src/tests/io/test_open.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ program test_open
character(:), allocatable :: filename
integer :: io, u, a(3)

! Text file
filename = get_outpath() // "/io_open.dat"
! Stream text file
filename = get_outpath() // "/io_open_stream.dat"

! Test mode "w"
u = open(filename, "w")
Expand All @@ -32,9 +32,34 @@ program test_open
close(u)


! Sequential text file
filename = get_outpath() // "/io_open_seq.dat"

! Stream file
filename = get_outpath() // "/io_open.stream"
! Test mode "w"
u = open(filename, "w", access = 'sequential')
write(u, *) 1, 2, 3
close(u)

! Test mode "r"
u = open(filename, "r", access = 'sequential')
read(u, *) a
call assert(all(a == [1, 2, 3]))
close(u)

! Test mode "a"
u = open(filename, "a", access = 'sequential')
write(u, *) 4, 5, 6
close(u)
u = open(filename, "r", access = 'sequential')
read(u, *) a
call assert(all(a == [1, 2, 3]))
read(u, *) a
call assert(all(a == [4, 5, 6]))
close(u)


! Stream binary file
filename = get_outpath() // "/io_open_stream.bin"

! Test mode "w"
u = open(filename, "wb")
Expand All @@ -59,9 +84,34 @@ program test_open
close(u)


! Sequential binary file
filename = get_outpath() // "/io_open_seq.bin"

! Test mode "w"
u = open(filename, "wb", access = 'sequential')
write(u) 1, 2, 3
close(u)

! Test mode "r"
u = open(filename, "rb", access = 'sequential')
read(u) a
call assert(all(a == [1, 2, 3]))
close(u)

! Test mode "a"
u = open(filename, "ab", access = 'sequential')
write(u) 4, 5, 6
close(u)
u = open(filename, "rb", access = 'sequential')
read(u) a
call assert(all(a == [1, 2, 3]))
read(u) a
call assert(all(a == [4, 5, 6]))
close(u)


!0 and non-0 open
filename = get_outpath() // "/io_open.stream"
filename = get_outpath() // "/io_open_stream.bin"

u = open(filename, "rb", io)
call assert(io == 0)
Expand Down