1
+ submodule (forlab_io) forlab_io_read_line
2
+
3
+ use , intrinsic :: iso_fortran_env, only: stdin = > input_unit
4
+ implicit none
5
+ character (* ), parameter :: nl = new_line(" \n" )
6
+
7
+ contains
8
+
9
+ ! > Read a line from the input unit.
10
+ subroutine read_line (line , unit , iostat )
11
+
12
+ character (:), allocatable , intent (out ) :: line
13
+ integer , intent (in ), optional :: unit
14
+ integer , intent (out ), optional :: iostat
15
+
16
+ integer :: unit_, iostat_
17
+ character (len= 512 ) :: line_
18
+ character (len= 512 ) :: msg
19
+
20
+ unit_ = optval(unit, stdin)
21
+
22
+ line = " "
23
+ read (unit_, " (A512)" , iostat= iostat_, iomsg= msg) line_
24
+ if (present (iostat)) then
25
+ iostat = iostat_
26
+ if (iostat_ == 0 ) line = trim (line_)
27
+ else
28
+ if (iostat_ == 0 ) then
29
+ line = trim (line_)
30
+ else
31
+ error stop msg
32
+ end if
33
+ end if
34
+
35
+ end subroutine read_line
36
+
37
+ ! > Read ASCII file as a string.
38
+ subroutine read_file (string , file , iostat , keep_newline )
39
+
40
+ character (:), allocatable , intent (out ) :: string
41
+ character (* ), intent (in ) :: file
42
+ integer , intent (out ), optional :: iostat
43
+ logical , intent (in ), optional :: keep_newline
44
+
45
+ integer :: iostat_, unit, count, i
46
+ character (:), allocatable :: string_
47
+ character (len= 512 ) :: msg
48
+ logical :: keep_newline_
49
+
50
+ keep_newline_ = optval(keep_newline, .true. )
51
+ open (newunit= unit, file= file)
52
+
53
+ string = " "
54
+ count = countlines(file)
55
+ do i = 1 , count
56
+ call read_line(string_, unit, iostat_)
57
+ if (keep_newline_) string_ = string_ // nl
58
+ string = string// string_
59
+ end do
60
+ close (unit)
61
+
62
+ end subroutine read_file
63
+
64
+ end submodule forlab_io_read_line
0 commit comments