get_cache_dir Function

public function get_cache_dir() result(cache_dir)

Arguments

None

Return Value character(len=:), allocatable


Source Code

    function get_cache_dir() result(cache_dir)
        character(len=:), allocatable :: cache_dir
        character(len=:), allocatable :: temp_dir, home_dir
        character(len=256) :: temp_buffer, home_buffer
        integer :: status

        ! Try to get XDG_CACHE_HOME first (Linux standard)
        call get_environment_variable('XDG_CACHE_HOME', temp_buffer, status=status)

        if (status == 0 .and. len_trim(temp_buffer) > 0) then
            temp_dir = trim(temp_buffer)
            cache_dir = join_path(temp_dir, 'fortran')
        else
            ! Fallback to HOME directory
            call get_environment_variable('HOME', home_buffer, status=status)

            if (status == 0) then
                home_dir = trim(home_buffer)
                ! Linux/macOS: ~/.cache/fortran
                cache_dir = join_path(home_dir, '.cache', 'fortran')
            else
                ! Windows fallback: try LOCALAPPDATA
               call get_environment_variable('LOCALAPPDATA', temp_buffer, status=status)
                if (status == 0) then
                    temp_dir = trim(temp_buffer)
                    cache_dir = join_path(temp_dir, 'fortran', 'cache')
                else
                    ! Last resort - use system temp directory
                    block
                        use system_utils, only: sys_get_temp_dir
                        character(len=:), allocatable :: temp_path

                        temp_path = sys_get_temp_dir()
                        if (len_trim(temp_path) > 0) then
                            cache_dir = join_path(temp_path, 'fortran-cache')
                        else
                            ! Ultimate fallback - use current directory
                            cache_dir = '.fortran-cache'
                        end if
                    end block
                end if
            end if
        end if

        ! Ensure result is trimmed to avoid issues with fixed-length strings
        cache_dir = trim(cache_dir)

    end function get_cache_dir