Skip to content

Commit

Permalink
Merge commit 'de0ca0df99bdeca53314a3e3bf373734fbf902c5'
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisteunissen committed Nov 17, 2023
2 parents 70c096c + de0ca0d commit b94432f
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/config_fortran/m_config.f90
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ module m_config

integer, parameter :: CFG_name_len = 80 !< Maximum length of variable names
integer, parameter :: CFG_string_len = 200 !< Fixed length of string type
!> Maximum length of line containing multiple arguments/values
integer, parameter :: CFG_max_line_len = 1000

!> Maximum number of entries in a variable (if it's an array)
integer, parameter :: CFG_max_array_size = 1000
Expand Down Expand Up @@ -63,7 +65,7 @@ module m_config
!> How the variable has been set (default, command line, file)
integer :: set_by = CFG_set_by_default
!> Data that has been read in for this variable
character(len=CFG_string_len) :: stored_data
character(len=CFG_max_line_len) :: stored_data

! These are the arrays used for storage. In the future, a "pointer" based
! approach could be used.
Expand Down Expand Up @@ -115,6 +117,7 @@ module m_config
! Constants
public :: CFG_name_len
public :: CFG_string_len
public :: CFG_max_line_len
public :: CFG_max_array_size

! Public methods
Expand Down Expand Up @@ -143,15 +146,21 @@ subroutine CFG_update_from_arguments(cfg, ignore_unknown)
type(CFG_t),intent(inout) :: cfg
!> Ignore unknown arguments (default: false)
logical, intent(in), optional :: ignore_unknown
character(len=CFG_string_len) :: arg
integer :: ix, n
character(len=CFG_max_line_len) :: arg
integer :: ix, n, arg_status
logical :: valid_syntax, strict
character(len=4) :: extension

strict = .true.; if (present(ignore_unknown)) strict = .not. ignore_unknown

do ix = 1, command_argument_count()
call get_command_argument(ix, arg)
call get_command_argument(ix, arg, status=arg_status)

if (arg_status > 0) then
call handle_error("Error in get_command_argument (status > 0)")
else if (arg_status == -1) then
call handle_error("Argument too long, increase CFG_max_line_len")
end if

n = len_trim(arg)
if (n > 3) extension = arg(n-3:)
Expand Down Expand Up @@ -236,11 +245,11 @@ subroutine CFG_read_file(cfg, filename)
logical :: valid_syntax
character(len=CFG_name_len) :: line_fmt
character(len=CFG_string_len) :: err_string
character(len=CFG_string_len) :: line
character(len=CFG_max_line_len) :: line
character(len=CFG_name_len) :: category

open(my_unit, file=trim(filename), status="old", action="read")
write(line_fmt, "(A,I0,A)") "(A", CFG_string_len, ")"
write(line_fmt, "(A,I0,A)") "(A", CFG_max_line_len, ")"

category = "" ! Default category is empty
line_number = 0
Expand All @@ -249,6 +258,12 @@ subroutine CFG_read_file(cfg, filename)
read(my_unit, FMT=trim(line_fmt), ERR=998, end=999) line
line_number = line_number + 1

if (len_trim(line) > CFG_max_line_len - 2) then
write(err_string, *) "Possible truncation in line ", line_number, &
" from ", trim(filename)
call handle_error(err_string)
end if

call parse_line(cfg, CFG_set_by_file, line, valid_syntax, category)

if (.not. valid_syntax) then
Expand Down Expand Up @@ -279,7 +294,7 @@ subroutine parse_line(cfg, set_by, line_arg, valid_syntax, category_arg)
character(len=CFG_name_len) :: var_name, category
integer :: ix, equal_sign_ix
logical :: append
character(len=CFG_string_len) :: line
character(len=CFG_max_line_len) :: line

valid_syntax = .true.

Expand Down

0 comments on commit b94432f

Please sign in to comment.