handle_standardize_only Subroutine

subroutine handle_standardize_only(input_file)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: input_file

Source Code

    subroutine handle_standardize_only(input_file)
        character(len=*), intent(in) :: input_file
        character(len=256) :: temp_output, error_msg
        character(len=1024) :: line
        integer :: unit, ios
        logical :: is_lowercase_fortran

        ! Check if input is a .lf file (lowercase fortran)
        is_lowercase_fortran = is_lazy_fortran_file(input_file)

        ! Create temporary output file
        temp_output = create_temp_file('fortran_main_output', '.f90')

        ! Process based on file type
        if (is_lowercase_fortran) then
            ! Transform using fortfront CLI
            block
                use system_utils, only: escape_shell_arg
                character(len=1024) :: command
                integer :: exit_status

                command = 'fortfront < "'//trim(escape_shell_arg(input_file))// &
                          '" > "'//trim(escape_shell_arg(temp_output))//'"'
                call execute_command_line(command, exitstat=exit_status, wait=.true.)

                if (exit_status /= 0) then
                    error_msg = 'fortfront transformation failed'
                else
                    error_msg = ''
                end if
            end block
        else
            ! For standard Fortran files, just copy them as-is
            block
                use system_utils, only: escape_shell_arg
                call execute_command_line('cp "'//trim(escape_shell_arg(input_file))// &
                          '" "'//trim(escape_shell_arg(temp_output))//'"', exitstat=ios)
            end block
            if (ios /= 0) then
                error_msg = 'Failed to copy file'
            else
                error_msg = ''
            end if
        end if

        if (len_trim(error_msg) > 0) then
            write (*, '(a,a)') 'Error: ', trim(error_msg)
            stop 1
        end if

        ! Output the preprocessed content to STDOUT
        open (newunit=unit, file=temp_output, status='old', action='read', iostat=ios)
        if (ios == 0) then
            do
                read (unit, '(A)', iostat=ios) line
                if (ios /= 0) exit
                write (*, '(A)') trim(line)
            end do
            close (unit)

            ! Clean up temporary file
            open (newunit=unit, file=temp_output, status='old', iostat=ios)
            if (ios == 0) then
                close (unit, status='delete')
            end if
        else
            write (*, '(a)') 'Error: Failed to read preprocessed output'
            stop 1
        end if

    end subroutine handle_standardize_only