Skip to content

Commit

Permalink
Revert 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 f57247d commit a3fafc6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
4 changes: 3 additions & 1 deletion docs/src/fortran/lib/constants.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ View source code :source:`fortran/include/constants.f90`
:type: integer
.. f:variable:: ERROR_PRIME_ALLOCATE_FAILED
:type: integer
.. f:variable:: ERROR_UTILS_ALLOCATE_FAILED
:type: integer

Denotes the exit codes of different failure modes, counting up from 1

Expand All @@ -42,7 +44,7 @@ View source code :source:`fortran/include/constants.f90`
.. f:variable:: ANSWERT_STR_SIZE
:type: integer

Denotes the exit codes of different failure modes, counting up from 1
Denotes the size of certain variables

.. literalinclude:: ../../../../fortran/src/include/constants.f90
:language: Fortran
Expand Down
8 changes: 4 additions & 4 deletions docs/src/fortran/lib/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ View source code :source:`fortran/include/utils.f90`

.. f:module:: utils
.. f:subroutine:: get_data_file(filename, contents)
.. f:function:: get_data_file(filename)
Given the name of a file in ``/_data`` (where ``/`` is the repository root), this function returns
the entirety of its contents, or an empty string if there is an error.

:p character(len=*) filename:
:pattrs filename: intent(in)
:p character(len=*) contents:
:pattrs contents: intent(out)
:returns contents:
:rtype contents: character(len=:)
:rattrs contents: allocatable

.. f:function:: get_answer(id)
Expand Down
25 changes: 17 additions & 8 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
subroutine get_data_file(filename, contents)
function get_data_file(filename) result(contents)
character(len=*), intent(in) :: filename
character(len=*), intent(inout) :: contents
character(len=:), allocatable :: contents
character(len=64) :: line
integer :: unit_number, iostat, file_size

Expand All @@ -28,20 +28,26 @@ subroutine get_data_file(filename, 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
if (iostat /= 0) then
close(unit_number)
exit
end if
print *, trim(line)
contents = contents // trim(line) // char(10)
end do
end if
close(unit_number)
print *, contents
end subroutine get_data_file
if (.not. allocated(contents)) then
contents = ''
end if
end function get_data_file

type(AnswerT) function get_answer(id) result(answer)
integer(i4t), intent(in) :: id
Expand All @@ -62,16 +68,18 @@ 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=ANSWERS_TSV_SIZE) :: text
character(len=:), allocatable :: text
character(len=32) :: val
character(len=4) :: id_, type_, length

do i=1, size(cached_answers)
cached_answers(i)%type = errort
end do

call get_data_file("answers.tsv", text)
print *, text
text = get_data_file("answers.tsv")
if (.not. allocated(text)) then
text = '' ! Ensure text is defined if allocation failed
end if
row_start = 1
line_length = 1
do while (line_length > 0)
Expand Down Expand Up @@ -109,6 +117,7 @@ subroutine init_answer_cache()
end if
end do

deallocate(text)
cache_inited = .true.
end subroutine

Expand Down

0 comments on commit a3fafc6

Please sign in to comment.