diff --git a/fortran/src/include/utils.f90 b/fortran/src/include/utils.f90 index a8e07589..693e7513 100644 --- a/fortran/src/include/utils.f90 +++ b/fortran/src/include/utils.f90 @@ -67,8 +67,8 @@ type(AnswerT) function get_answer(id) result(answer) subroutine init_answer_cache() integer(i18t) :: i, j - integer :: ios, row_start, row_end, line_length - character(len=:), allocatable :: text + integer :: ios, line_length, unit_number, file_size + character(len=64) :: line character(len=32) :: val character(len=4) :: id_, type_, length @@ -76,22 +76,29 @@ subroutine init_answer_cache() cached_answers(i)%type = errort end do - text = get_data_file("answers.tsv") - if (.not. allocated(text)) then - text = '' ! Ensure text is defined if allocation failed + unit_number = prev_unit + 1 + prev_unit = unit_number + open(unit=unit_number, file=("../_data/answers.tsv"), status='old', action='read', iostat=ios) + if (ios /= 0) then + print *, "Error opening file: ../_data/answers.tsv" + return end if + row_start = 1 line_length = 1 do while (line_length > 0) - line_length = index(text(row_start:), char(10)) ! Find the next line - row_end = row_start + line_length - 1 + line = '' + read(unit_number, '(A)', iostat=ios) line + if (iostat /= 0) then + close(unit_number) + exit + end if + + line = trim(line) + line_length = len(line) if (line_length > 0) then - if (text(row_end:row_end) == char(13)) then ! if \r - row_end = row_end - 1 - end if - call parse_line(text(row_start:row_end), id_, type_, length, val) ! Parse values + call parse_line(line, id_, type_, length, val) ! Parse values if (id_ == "ID") then - row_start = row_start + line_length cycle end if read(id_, *, iostat=ios) i @@ -112,8 +119,7 @@ subroutine init_answer_cache() cached_answers(i)%string_value = trim(val) case default print *, "Invalid value type. Returning error type" - end select - row_start = row_start + line_length ! Move to the next line + end select end if end do