Update the variables in the configartion with the values found in 'filename'
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(CFG_t), | intent(inout) | :: | cfg | |||
| character(len=*), | intent(in) | :: | filename |
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