CFG_read_file Subroutine

public subroutine CFG_read_file(cfg, filename)

Update the variables in the configartion with the values found in 'filename'

Arguments

Type IntentOptional Attributes Name
type(CFG_t), intent(inout) :: cfg
character(len=*), intent(in) :: filename

Calls

proc~~cfg_read_file~~CallsGraph proc~cfg_read_file CFG_read_file proc~get_var_index get_var_index proc~cfg_read_file->proc~get_var_index proc~handle_error handle_error proc~cfg_read_file->proc~handle_error proc~prepare_store_var prepare_store_var proc~cfg_read_file->proc~prepare_store_var proc~read_variable read_variable proc~cfg_read_file->proc~read_variable proc~trim_comment trim_comment proc~cfg_read_file->proc~trim_comment proc~binary_search_variable binary_search_variable proc~get_var_index->proc~binary_search_variable proc~prepare_store_var->proc~get_var_index proc~prepare_store_var->proc~handle_error proc~ensure_free_storage ensure_free_storage proc~prepare_store_var->proc~ensure_free_storage proc~read_variable->proc~handle_error proc~get_fields_string~2 get_fields_string proc~read_variable->proc~get_fields_string~2 proc~resize_storage resize_storage proc~read_variable->proc~resize_storage

Called by

proc~~cfg_read_file~~CalledByGraph proc~cfg_read_file CFG_read_file proc~cfg_update_from_arguments CFG_update_from_arguments proc~cfg_update_from_arguments->proc~cfg_read_file

Source Code

  subroutine CFG_read_file(cfg, filename)
    type(CFG_t), intent(inout)   :: cfg
    character(len=*), intent(in) :: filename

    integer, parameter            :: my_unit = 123
    integer                       :: io_state, equal_sign_ix
    integer                       :: ix, line_number
    character(len=CFG_name_len)   :: var_name, category
    character(len=CFG_name_len)   :: line_fmt
    character(len=CFG_string_len) :: err_string
    character(len=CFG_string_len) :: line

    open(my_unit, FILE=trim(filename), status = "OLD", action="READ", err=998,&
        iostat=io_state)
    line_number = 0

    write(line_fmt, "(A,I0,A)") "(A", CFG_string_len, ")"

    ! Default category is empty
    category = ""

    do
       read(my_unit, FMT=trim(line_fmt), ERR=998, end=999) line
       line_number = line_number + 1

       call trim_comment(line, '#')

       ! Skip empty lines
       if (line == "") cycle

       ! Locate the '=' sign
       equal_sign_ix = scan(line, '=')

       ! if there is no '='-sign then a category is indicated
       if (equal_sign_ix == 0) then
          line = adjustl(line)

          ! The category name should appear like this: [category_name]
          ix = scan(line, ']')
          if (line(1:1) /= '[' .or. ix == 0) then
             write(err_string, *) "Cannot read line ", line_number, " from ",&
                 trim(filename)
             call handle_error(err_string)
          else
             category = line(2:ix-1)
             cycle
          end if
       end if

       var_name = line(1 : equal_sign_ix - 1) ! Set variable name

       ! If there is no indent, reset to no category
       if (var_name(1:1) /= " " .and. var_name(1:1) /= char(9)) then
          category = ""
       end if

       ! Remove leading blanks
       var_name = adjustl(var_name)

       ! Add category if it is defined
       if (category /= "") then
          var_name = trim(category) // CFG_category_separator // var_name
       end if

       line     = line(equal_sign_ix + 1:) !Set line to the values behind the '=' sign
       line     = adjustl(line)               ! Remove leading blanks

       ! Find variable corresponding to name in file
       call get_var_index(cfg, var_name, ix)

       if (ix <= 0) then
          ! Variable still needs to be created, for now store data as a string
          call prepare_store_var(cfg, trim(var_name), CFG_unknown_type, 1,&
              "Not yet created", ix, .false.)
          cfg%vars(ix)%stored_data = line
       else
          cfg%vars(ix)%stored_data = line
          call read_variable(cfg%vars(ix))
       end if
    end do

998 write(err_string, *) "io_state = ", io_state, " while reading from ",&
    trim(filename), " at line ", line_number
    call handle_error("CFG_read_file:" // err_string)

    ! Routine ends here if the end of "filename" is reached
999 close(my_unit, iostat=io_state)

  end subroutine CFG_read_file