From a3fafc65114c7b4a6fc53cce5830027678065fc7 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Sat, 5 Oct 2024 23:54:15 -0500 Subject: [PATCH] Revert get_data_file as subroutine --- docs/src/fortran/lib/constants.rst | 4 +++- docs/src/fortran/lib/utils.rst | 8 ++++---- fortran/src/include/utils.f90 | 25 +++++++++++++++++-------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/docs/src/fortran/lib/constants.rst b/docs/src/fortran/lib/constants.rst index 728b1a0f..e68ab720 100644 --- a/docs/src/fortran/lib/constants.rst +++ b/docs/src/fortran/lib/constants.rst @@ -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 @@ -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 diff --git a/docs/src/fortran/lib/utils.rst b/docs/src/fortran/lib/utils.rst index 4e2ecb82..142a916a 100644 --- a/docs/src/fortran/lib/utils.rst +++ b/docs/src/fortran/lib/utils.rst @@ -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) diff --git a/fortran/src/include/utils.f90 b/fortran/src/include/utils.f90 index 65daf99b..a8e07589 100644 --- a/fortran/src/include/utils.f90 +++ b/fortran/src/include/utils.f90 @@ -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 @@ -28,6 +28,11 @@ 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 @@ -35,13 +40,14 @@ subroutine get_data_file(filename, contents) 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 @@ -62,7 +68,7 @@ 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 @@ -70,8 +76,10 @@ subroutine init_answer_cache() 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) @@ -109,6 +117,7 @@ subroutine init_answer_cache() end if end do + deallocate(text) cache_inited = .true. end subroutine