Skip to content

Use getline from stdlib_io #34

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 1 commit into from
Dec 19, 2021
Merged
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
4 changes: 0 additions & 4 deletions pages/tutorial/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ The implementation is shown here
:caption: src/demo.f90
```

:::{note}
To work with deferred length characters we added a small helper function to read a whole line.
:::

Finally, we need a command line driver to make use of our new function.

```{code-block} fortran
Expand Down
32 changes: 1 addition & 31 deletions src/tutorial/dependencies/src/demo.f90
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module demo
use stdlib_io, only : getline
use stdlib_strings, only : replace_all
implicit none
private

public :: substitute
public :: getline

contains

Expand All @@ -29,34 +29,4 @@ subroutine substitute(input, output, pattern, replacement)
end do
end subroutine substitute

!> Read a whole line from a formatted unit into a deferred length character variable
subroutine getline(unit, line, iostat, iomsg)
!> Formatted IO unit
integer, intent(in) :: unit
!> Line to read
character(len=:), allocatable, intent(out) :: line
!> Status of operation
integer, intent(out) :: iostat
!> Error message
character(len=:), allocatable, optional :: iomsg

integer, parameter :: bufsize = 512
character(len=bufsize) :: buffer, msg
integer :: chunk, stat

allocate(character(len=0) :: line)
do
read(unit, '(a)', advance='no', iostat=stat, iomsg=msg, size=chunk) buffer
if (stat > 0) exit
line = line // buffer(:chunk)
if (stat < 0) then
if (is_iostat_eor(stat)) stat = 0
exit
end if
end do

if (stat /= 0 .and. present(iomsg)) iomsg = trim(msg)
iostat = stat
end subroutine getline

end module demo
3 changes: 2 additions & 1 deletion src/tutorial/dependencies/test/main.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module test_demo
use demo, only : substitute, getline
use demo, only : substitute
use stdlib_io, only : getline
use testdrive, only : error_type, unittest_type, new_unittest, check
implicit none
private
Expand Down