# open_config_file Subroutine

## public subroutine open_config_file(L, filename, ErrCode, ErrString, buffer)

Subroutine to load and execute a script from a file.

If you are using MPI for parallelization, have a look at the tem_open_distconf routine in the treelm library instead.

### Arguments

Type IntentOptional AttributesName
type(flu_State) :: L

Handle to the Lua script

character(len=*), intent(in) :: filename

Name of file to load the Lua code from

integer, intent(out), optional :: ErrCode

This optional parameter might be used to react on errors in the calling side. If neither ErrCode nor ErrString are given, this subroutine will stop the program execution and print the error message from Lua to the stdout.

character(len=*), intent(out), optional :: ErrString

Obtained error description from the Lua stack.

This optional argument holds the Lua error message in case somehting went wrong. It can be used to provide some feedback to the user in the calling routine. If neither ErrCode nor ErrString are provided, open_config() will print the error message and stop program execution.

type(cbuf_type), intent(out), optional :: buffer

Optional argument to return the compiled script after loading it to the caller.

It might be handy to reuse the loaded script later on, this argument allows you to obtain the script in compiled form, before it is executed. The buffer will be allocated and filled with the Lua data. It contains the actual string in buffer%buffer which is a character pointer, and the original c_ptr to this

## Source Code

  subroutine open_config_file(L, filename, ErrCode, ErrString, buffer)
type(flu_State) :: L !! Handle to the Lua script

!> Name of file to load the Lua code from
character(len=*), intent(in) :: filename

!> Error code returned by Lua during loading or executing the file.
!!
!! This optional parameter might be used to react on errors in the calling
!! side. If neither ErrCode nor ErrString are given, this subroutine will
!! stop the program execution and print the error message from Lua to the
!! stdout.
integer, intent(out), optional :: ErrCode

!> Obtained error description from the Lua stack.
!!
!! This optional argument holds the Lua error message in case somehting
!! went wrong. It can be used to provide some feedback to the user in the
!! calling routine. If neither ErrCode nor ErrString are provided,
!! open_config() will print the error message and stop program execution.
character(len=*), intent(out), optional :: ErrString

!> Optional argument to return the compiled script after loading it to
!! the caller.
!!
!! It might be handy to reuse the loaded script later on, this argument
!! allows you to obtain the script in compiled form, before it is executed.
!! The buffer will be allocated and filled with the Lua data.
!! It contains the actual string in buffer%buffer which is a character
!! pointer, and the original c_ptr to this
type(cbuf_type), intent(out), optional :: buffer

integer :: err
integer :: length

if (.not.flu_isopen(L)) L = fluL_newstate()

call aot_err_handler(L, err, 'Cannot load configuration file:', ErrString, &
&                  ErrCode)

if (err == 0) then
if (present(buffer)) then
call flu_dump(L, buffer, length, err)
end if
call fluL_openlibs(L)

err = flu_pcall(L, 0, 0, 0)
call aot_err_handler(L, err, 'Cannot run configuration file:',  &
&                  ErrString, ErrCode)
end if

end subroutine open_config_file