Skip to content

Commit

Permalink
Bring back get_data_file as subroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Oct 7, 2024
1 parent b531ea5 commit 7d77258
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions fortran/src/include/utils.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module utils
logical, private :: cache_inited = .false.
type(AnswerT), private, dimension(1024) :: cached_answers
contains
function get_data_file(filename) result(contents)
subroutine get_data_file(filename, contents)
character(len=*), intent(in) :: filename
character(len=:), allocatable :: contents
character(len=*), intent(inout) :: contents
character(len=64) :: line
integer :: unit_number, iostat, file_size

Expand All @@ -28,11 +28,6 @@ function get_data_file(filename) result(contents)

inquire(unit=unit_number, size=file_size)
if (file_size > 0) then
allocate(character(len=file_size) :: contents)
if (.not. allocated(contents)) then
print *, "Failed to allocate memory for read. Exiting."
stop ERROR_UTILS_ALLOCATE_FAILED
end if
contents = ''
do
read(unit_number, '(A)', iostat=iostat) line
Expand All @@ -44,13 +39,9 @@ function get_data_file(filename) result(contents)
end do
end if
close(unit_number)
if (.not. allocated(contents)) then
contents = ''
end if
end function get_data_file
end subroutine get_data_file

function get_answer(id) result(answer)
type(AnswerT) :: answer
type(AnswerT) function get_answer(id) result(answer)
integer(i4t), intent(in) :: id

if (id < 1 .or. id > size(cached_answers)) then
Expand All @@ -69,18 +60,15 @@ 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
character(len=ANSWERS_TSV_SIZE) :: text
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
end if
call get_data_file("answers.tsv", text)
row_start = 1
line_length = 1
do while (line_length > 0)
Expand Down Expand Up @@ -118,7 +106,6 @@ subroutine init_answer_cache()
end if
end do

deallocate(text)
cache_inited = .true.
end subroutine

Expand Down

0 comments on commit 7d77258

Please sign in to comment.