load_config Subroutine

public subroutine load_config(path, fpm, cfg, found, stat, errmsg)

Read the configuration in file path: the whole file, or with fpm its [extra.fformat] table. found is false if an fpm.toml has no such table.

Arguments

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

the TOML file

logical, intent(in) :: fpm

path is an fpm.toml

type(config_t), intent(out) :: cfg

the configuration; empty if not found

logical, intent(out) :: found

the file has a configuration

integer, intent(out) :: stat

nonzero for bad TOML, an unknown key or a bad value

character(len=:), intent(out), allocatable :: errmsg

what is wrong, with the file name, if stat is nonzero


Calls

proc~~load_config~~CallsGraph proc~load_config load_config get get proc~load_config->get get_keys get_keys proc~load_config->get_keys get_type get_type proc~load_config->get_type get_value get_value proc~load_config->get_value proc~empty empty proc~load_config->proc~empty proc~to_s to_s proc~load_config->proc~to_s toml_load toml_load proc~load_config->toml_load

Called by

proc~~load_config~~CalledByGraph proc~load_config load_config proc~config_for config_for proc~config_for->proc~load_config proc~find_config find_config proc~config_for->proc~find_config proc~find_config->proc~load_config proc~directory_files directory_files proc~directory_files->proc~config_for proc~options_for options_for proc~options_for->proc~config_for proc~process_text process_text proc~process_text->proc~options_for proc~run run proc~run->proc~directory_files proc~process_file process_file proc~run->proc~process_file proc~process_stdin process_stdin proc~run->proc~process_stdin proc~process_file->proc~process_text proc~process_stdin->proc~process_text

Source Code

subroutine load_config(path, fpm, cfg, found, stat, errmsg)
    !! Read the configuration in file `path`: the whole file, or with `fpm` its
    !! `[extra.fformat]` table. `found` is false if an `fpm.toml` has no such table.
    character(len=*), intent(in) :: path                    !! the TOML file
    logical, intent(in) :: fpm                              !! `path` is an `fpm.toml`
    type(config_t), intent(out) :: cfg                      !! the configuration; empty if not `found`
    logical, intent(out) :: found                           !! the file has a configuration
    integer, intent(out) :: stat                            !! nonzero for bad TOML, an unknown key or a bad value
    character(len=:), allocatable, intent(out) :: errmsg    !! what is wrong, with the file name, if `stat` is nonzero

    type(toml_table), allocatable, target :: top
    type(toml_table), pointer :: extra, table
    type(toml_error), allocatable :: error

    stat = 0
    found = .false.
    call empty(cfg)
    call toml_load(top, path, error=error)
    if (allocated(error)) then
        stat = 1
        errmsg = path//': '//error%message
        return
    end if
    if (fpm) then
        call get_value(top, 'extra', extra, requested=.false.)
        if (.not. associated(extra)) return
        call get_value(extra, 'fformat', table, requested=.false.)
        if (.not. associated(table)) return
    else
        table => top
    end if
    found = .true.
    cfg%path = path
    call read_table(table, '')

contains

    recursive subroutine read_table(t, prefix)
        !! Add the settings in table `t`, with keys prefixed by `prefix`.
        type(toml_table), intent(inout) :: t   !! the table to read
        character(len=*), intent(in) :: prefix  !! `''` for the top level, or `spaces.` / `case.`
        type(toml_key), allocatable :: keys(:)
        class(toml_value), pointer :: v
        type(toml_table), pointer :: sub
        character(len=:), allocatable :: key, text
        integer :: k

        call t%get_keys(keys)
        do k = 1, size(keys)
            key = keys(k)%key
            call t%get(key, v)
            select type (v)
            type is (toml_table)
                sub => v
                if (prefix == '' .and. key == 'files') then
                    call read_files(sub)
                else if (prefix == '' .and. (key == 'spaces' .or. key == 'case')) then
                    call read_table(sub, key//'.')
                else
                    call bad('unknown table "'//prefix//key//'"')
                end if
            type is (toml_keyval)
                text = value_text(t, key, v%get_type())
                if (stat /= 0) return
                ! `all` comes first, so that the other entries of the table override it
                if (key == 'all') then
                    cfg%settings = [setting_t(prefix//key, text), cfg%settings]
                else
                    cfg%settings = [cfg%settings, setting_t(prefix//key, text)]
                end if
            class default
                call bad('unexpected value for "'//prefix//key//'"')
            end select
            if (stat /= 0) return
        end do
    end subroutine read_table

    function value_text(t, key, kind) result(text)
        !! A string, integer or boolean value as text.
        type(toml_table), intent(inout) :: t    !! the table holding the value
        character(len=*), intent(in) :: key     !! the value's key in `t`
        integer, intent(in) :: kind             !! the value's type, a `toml_type` member
        character(len=:), allocatable :: text
        integer :: i
        logical :: flag
        text = ''
        if (kind == toml_type%string) then
            call get_value(t, key, text)
        else if (kind == toml_type%int) then
            call get_value(t, key, i)
            text = to_s(i)
        else if (kind == toml_type%boolean) then
            call get_value(t, key, flag)
            text = merge('true ', 'false', flag)
            text = trim(text)
        else
            call bad('expected a string, number or true/false for "'//key//'"')
        end if
    end function value_text

    subroutine read_files(t)
        !! Read the `[files]` table `t` into `cfg%extensions` and `cfg%exclude`.
        type(toml_table), intent(inout) :: t   !! the `[files]` table
        type(toml_key), allocatable :: keys(:)
        integer :: k
        call t%get_keys(keys)
        do k = 1, size(keys)
            select case (keys(k)%key)
            case ('extensions')
                call read_list(t, 'extensions', cfg%extensions)
            case ('exclude')
                call read_list(t, 'exclude', cfg%exclude)
            case default
                call bad('unknown setting "files.'//keys(k)%key//'"')
            end select
            if (stat /= 0) return
        end do
    end subroutine read_files

    subroutine read_list(t, key, list)
        !! An array of strings.
        type(toml_table), intent(inout) :: t                    !! the `[files]` table
        character(len=*), intent(in) :: key                     !! the array's key in `t`
        type(string_t), allocatable, intent(inout) :: list(:)   !! the strings, replacing any earlier list
        type(toml_array), pointer :: arr
        character(len=:), allocatable :: item
        integer :: i, ios
        call get_value(t, key, arr, requested=.false.)
        if (.not. associated(arr)) then
            call bad('expected a list of strings for "files.'//key//'"')
            return
        end if
        if (allocated(list)) deallocate (list)
        allocate (list(0))
        do i = 1, len(arr)
            call get_value(arr, i, item, stat=ios)
            if (ios /= 0 .or. .not. allocated(item)) then
                call bad('expected a list of strings for "files.'//key//'"')
                return
            end if
            list = [list, string_t(item)]
        end do
    end subroutine read_list

    subroutine bad(msg)
        !! Make loading fail with message `msg`, prefixed with the file name.
        character(len=*), intent(in) :: msg   !! what is wrong
        stat = 1
        errmsg = path//': '//msg
    end subroutine bad

end subroutine load_config