Skip to content

Commit 3a1f064

Browse files
authored
Use getline from stdlib_io (#34)
1 parent 1f94c45 commit 3a1f064

File tree

3 files changed

+3
-36
lines changed

3 files changed

+3
-36
lines changed

pages/tutorial/dependencies.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ The implementation is shown here
3232
:caption: src/demo.f90
3333
```
3434

35-
:::{note}
36-
To work with deferred length characters we added a small helper function to read a whole line.
37-
:::
38-
3935
Finally, we need a command line driver to make use of our new function.
4036

4137
```{code-block} fortran
Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module demo
2+
use stdlib_io, only : getline
23
use stdlib_strings, only : replace_all
34
implicit none
45
private
56

67
public :: substitute
7-
public :: getline
88

99
contains
1010

@@ -29,34 +29,4 @@ subroutine substitute(input, output, pattern, replacement)
2929
end do
3030
end subroutine substitute
3131

32-
!> Read a whole line from a formatted unit into a deferred length character variable
33-
subroutine getline(unit, line, iostat, iomsg)
34-
!> Formatted IO unit
35-
integer, intent(in) :: unit
36-
!> Line to read
37-
character(len=:), allocatable, intent(out) :: line
38-
!> Status of operation
39-
integer, intent(out) :: iostat
40-
!> Error message
41-
character(len=:), allocatable, optional :: iomsg
42-
43-
integer, parameter :: bufsize = 512
44-
character(len=bufsize) :: buffer, msg
45-
integer :: chunk, stat
46-
47-
allocate(character(len=0) :: line)
48-
do
49-
read(unit, '(a)', advance='no', iostat=stat, iomsg=msg, size=chunk) buffer
50-
if (stat > 0) exit
51-
line = line // buffer(:chunk)
52-
if (stat < 0) then
53-
if (is_iostat_eor(stat)) stat = 0
54-
exit
55-
end if
56-
end do
57-
58-
if (stat /= 0 .and. present(iomsg)) iomsg = trim(msg)
59-
iostat = stat
60-
end subroutine getline
61-
6232
end module demo

src/tutorial/dependencies/test/main.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module test_demo
2-
use demo, only : substitute, getline
2+
use demo, only : substitute
3+
use stdlib_io, only : getline
34
use testdrive, only : error_type, unittest_type, new_unittest, check
45
implicit none
56
private

0 commit comments

Comments
 (0)