-
Notifications
You must be signed in to change notification settings - Fork 191
Implement open(filename, mode) and use it #71
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ac61bf4
Implement open(filename, mode) and use it
certik c8a3fca
Implement mode="a"
certik d2d3bfa
Rename loadtxt test directory to io
certik 6a263ff
Add a test for "open"
certik ab5c469
Apply suggestions from code review
certik 13a668e
stdlib_experimental_io: addition of a parser and characters "+" and "…
jvdp1 2f1a0e8
test_open: addition of a test for stream files
jvdp1 576b4f7
Merge pull request #1 from jvdp1/opencertik
certik b5c14b4
Merge branch 'master' into open
certik 041a4e1
Clean test files
certik b0b3dbf
Add tests for parse_mode()
certik 08dc86b
Allow any order of characters in the mode
certik 5128458
Fix an out of bounds error
certik 48cbf24
Let parse_mode() set the default value
certik a98832c
Manual Makefiles: add a new dependency
certik 3ed454b
Merge branch 'master' into open
certik a8416a1
Use optval
certik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
module stdlib_experimental_io | ||
use iso_fortran_env, only: sp=>real32, dp=>real64, qp=>real128 | ||
use stdlib_experimental_error, only: error_stop | ||
implicit none | ||
private | ||
public :: loadtxt, savetxt | ||
public :: loadtxt, savetxt, open | ||
|
||
interface loadtxt | ||
module procedure sloadtxt | ||
|
@@ -46,7 +47,7 @@ subroutine sloadtxt(filename, d) | |
integer :: s | ||
integer :: nrow,ncol,i | ||
|
||
open(newunit=s, file=filename, status="old", action="read") | ||
s = open(filename) | ||
|
||
! determine number of columns | ||
ncol = number_of_columns(s) | ||
|
@@ -89,7 +90,7 @@ subroutine dloadtxt(filename, d) | |
integer :: s | ||
integer :: nrow,ncol,i | ||
|
||
open(newunit=s, file=filename, status="old", action="read") | ||
s = open(filename) | ||
|
||
! determine number of columns | ||
ncol = number_of_columns(s) | ||
|
@@ -132,7 +133,7 @@ subroutine qloadtxt(filename, d) | |
integer :: s | ||
integer :: nrow,ncol,i | ||
|
||
open(newunit=s, file=filename, status="old", action="read") | ||
s = open(filename) | ||
|
||
! determine number of columns | ||
ncol = number_of_columns(s) | ||
|
@@ -164,7 +165,7 @@ subroutine ssavetxt(filename, d) | |
! call savetxt("log.txt", data) | ||
|
||
integer :: s, i | ||
open(newunit=s, file=filename, status="replace", action="write") | ||
s = open(filename, "w") | ||
do i = 1, size(d, 1) | ||
write(s, *) d(i, :) | ||
end do | ||
|
@@ -187,7 +188,7 @@ subroutine dsavetxt(filename, d) | |
! call savetxt("log.txt", data) | ||
|
||
integer :: s, i | ||
open(newunit=s, file=filename, status="replace", action="write") | ||
s = open(filename, "w") | ||
do i = 1, size(d, 1) | ||
write(s, *) d(i, :) | ||
end do | ||
|
@@ -210,7 +211,7 @@ subroutine qsavetxt(filename, d) | |
! call savetxt("log.txt", data) | ||
|
||
integer :: s, i | ||
open(newunit=s, file=filename, status="replace", action="write") | ||
s = open(filename, "w") | ||
do i = 1, size(d, 1) | ||
write(s, *) d(i, :) | ||
end do | ||
|
@@ -268,4 +269,39 @@ logical function whitechar(char) ! white character | |
end if | ||
end function | ||
|
||
integer function open(filename, mode) result(u) | ||
! Open a file | ||
! | ||
! To open a file to read: | ||
! | ||
! u = open("somefile.txt") # The default `mode` is "r" | ||
certik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
! u = open("somefile.txt", "r") | ||
! | ||
! To open a file to write: | ||
! | ||
! u = open("somefile.txt", "w") | ||
|
||
! To append to the end of the file if it exists: | ||
! | ||
! u = open("somefile.txt", "a") | ||
|
||
character(*), intent(in) :: filename | ||
character(*), intent(in), optional :: mode | ||
character(:), allocatable :: mode_ | ||
mode_ = "r" | ||
certik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (present(mode)) mode_ = mode | ||
! Note: the Fortran standard says that the default values for `status` and | ||
! `action` are processor dependent, so we have to explicitly set them below | ||
if (mode_ == "r") then | ||
certik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
open(newunit=u, file=filename, status="old", action="read") | ||
certik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else if (mode_ == "w") then | ||
certik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
open(newunit=u, file=filename, status="replace", action="write") | ||
certik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else if (mode_ == "a") then | ||
certik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
open(newunit=u, file=filename, position="append", status="old", & | ||
action="write") | ||
certik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else | ||
certik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
call error_stop("Unsupported mode") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there some rules for error messages? Usually, I would mention something a bit more verbose to help the user, e.g.
Maybe worthwhile to open an issue about that? |
||
end if | ||
end function | ||
|
||
end module |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.