subroutine read_file(filename, val, error)
use mctc_io_utils, only : next_line, read_next_token, io_error, token_type
character(len=*), intent(in) :: filename
integer, intent(out) :: val
type(error_type), allocatable, intent(out) :: error
integer :: io, stat, lnum, pos
type(token_type) :: token
character(len=:), allocatable :: line
lnum = 0
open(file=filename, newunit=io, status='old', iostat=stat)
if (stat /= 0) then
call fatal_error(error, "Error: Could not open file '"//filename//"'")
return
end if
call next_line(io, line, pos, lnum, stat)
if (stat == 0) &
call read_next_token(line, pos, token, val, stat)
if (stat /= 0) then
call io_error(error, "Cannot read value from file", line, token, &
filename, lnum, "expected integer value")
return
end if
close(io, iostat=stat)
end subroutine read_file