Skip to content

Commit

Permalink
Try to route around get_data_file
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Oct 7, 2024
1 parent a3fafc6 commit 52c5843
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions fortran/src/include/utils.f90
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,38 @@ 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

do i=1, size(cached_answers)
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
Expand All @@ -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

Expand Down

0 comments on commit 52c5843

Please sign in to comment.