|
| 1 | +submodule(file_m) file_s |
| 2 | + use iso_fortran_env, only : iostat_end, iostat_eor, output_unit |
| 3 | + use assert_m, only : assert |
| 4 | + implicit none |
| 5 | + |
| 6 | +contains |
| 7 | + |
| 8 | + module procedure construct |
| 9 | + file_object%lines_ = lines |
| 10 | + end procedure |
| 11 | + |
| 12 | + module procedure write_lines |
| 13 | + |
| 14 | + integer file_unit, io_status, l |
| 15 | + |
| 16 | + call assert(allocated(self%lines_), "file_t%write_lines: allocated(self%lines_)") |
| 17 | + |
| 18 | + if (present(file_name)) then |
| 19 | + open(newunit=file_unit, file=file_name%string(), form='formatted', status='unknown', iostat=io_status, action='write') |
| 20 | + call assert(io_status==0,"write_lines: io_status==0 after 'open' statement", file_name%string()) |
| 21 | + else |
| 22 | + file_unit = output_unit |
| 23 | + end if |
| 24 | + |
| 25 | + do l = 1, size(self%lines_) |
| 26 | + write(file_unit, *) self%lines_(l)%string() |
| 27 | + end do |
| 28 | + |
| 29 | + if (present(file_name)) close(file_unit) |
| 30 | + end procedure |
| 31 | + |
| 32 | + module procedure read_lines |
| 33 | + |
| 34 | + integer io_status, file_unit, line_num |
| 35 | + character(len=:), allocatable :: line |
| 36 | + integer, parameter :: max_message_length=128 |
| 37 | + character(len=max_message_length) error_message |
| 38 | + integer, allocatable :: lengths(:) |
| 39 | + |
| 40 | + open(newunit=file_unit, file=file_name%string(), form='formatted', status='old', iostat=io_status, action='read') |
| 41 | + call assert(io_status==0,"read_lines: io_status==0 after 'open' statement", file_name%string()) |
| 42 | + |
| 43 | + lengths = line_lengths(file_unit) |
| 44 | + |
| 45 | + associate(num_lines => size(lengths)) |
| 46 | + |
| 47 | + allocate(file_object%lines_(num_lines)) |
| 48 | + |
| 49 | + do line_num = 1, num_lines |
| 50 | + allocate(character(len=lengths(line_num)) :: line) |
| 51 | + read(file_unit, '(a)', iostat=io_status, iomsg=error_message) line |
| 52 | + call assert(io_status==0,"read_lines: io_status==0 after line read", error_message) |
| 53 | + file_object%lines_(line_num) = string_t(line) |
| 54 | + deallocate(line) |
| 55 | + end do |
| 56 | + |
| 57 | + end associate |
| 58 | + |
| 59 | + close(file_unit) |
| 60 | + |
| 61 | + contains |
| 62 | + |
| 63 | + function line_count(file_unit) result(num_lines) |
| 64 | + integer, intent(in) :: file_unit |
| 65 | + integer num_lines |
| 66 | + |
| 67 | + rewind(file_unit) |
| 68 | + num_lines = 0 |
| 69 | + do |
| 70 | + read(file_unit, *, iostat=io_status) |
| 71 | + if (io_status==iostat_end) exit |
| 72 | + num_lines = num_lines + 1 |
| 73 | + end do |
| 74 | + rewind(file_unit) |
| 75 | + end function |
| 76 | + |
| 77 | + function line_lengths(file_unit) result(lengths) |
| 78 | + integer, intent(in) :: file_unit |
| 79 | + integer, allocatable :: lengths(:) |
| 80 | + integer io_status |
| 81 | + character(len=1) c |
| 82 | + |
| 83 | + associate(num_lines => line_count(file_unit)) |
| 84 | + |
| 85 | + allocate(lengths(num_lines), source = 0) |
| 86 | + rewind(file_unit) |
| 87 | + |
| 88 | + do line_num = 1, num_lines |
| 89 | + do |
| 90 | + read(file_unit, '(a)', advance='no', iostat=io_status, iomsg=error_message) c |
| 91 | + if (io_status==iostat_eor .or. io_status==iostat_end) exit |
| 92 | + lengths(line_num) = lengths(line_num) + 1 |
| 93 | + end do |
| 94 | + end do |
| 95 | + |
| 96 | + rewind(file_unit) |
| 97 | + |
| 98 | + end associate |
| 99 | + end function |
| 100 | + |
| 101 | + end procedure |
| 102 | + |
| 103 | + module procedure lines |
| 104 | + my_lines = self%lines_ |
| 105 | + end procedure |
| 106 | + |
| 107 | +end submodule file_s |
0 commit comments