file_utils::input_unit_exist fails if there is a trailing/leading tab in the line

Issue #112 resolved
Peter Hill created an issue

Reported by Rahul Gaur in Slack.

file_utils::input_unit_exist checks if a namelist with a given name exists in the input file. It compares the input to a line in the input file like: trim(adjustl(line)) == "&"//nml. However, because tab characters aren’t considered “blanks” by Fortran, they are not removed by trim.

The following program and inputfile reproduce the problem:

program mvce
  use file_utils
  implicit none
  logical :: list
  integer :: unit
  logical :: fail_exists, pass_exists

  call init_file_utils(list)
  unit = input_unit_exist("fail", fail_exists)
  unit = input_unit_exist("pass", pass_exists)
  print*, fail_exists, pass_exists
end program mvce
&fail   
/
&pass  
/

There is a tab on the fail line and spaces on the pass line.

I guess we also don’t strip comments, so they’re likely to trip things up too.

Note that a trivial workaround exists: use tr -d ‘\t’ to remove all tabs from an input file.

Comments (4)

  1. David Dickinson

    We do strip comments at some point in parsing the input file so it’s possible that would still work, and if that is the case then it may make that a good place to also strip tabs.

  2. David Dickinson

    Yes in file_utils:init_input_unit we read the provided input file and write it to a temporary file after calling strip_comments on it. We could probably add a strip_tabs or similar that we call.

    This is also where we have a nifty undocumented feature that allows us to !include files in our input file.

  3. Log in to comment