Open file, once initialized.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(file_stl_object), | intent(inout) | :: | self | File STL. |
||
| character(len=*), | intent(in) | :: | file_action | File action, "read" or "write". |
||
| logical, | intent(in), | optional | :: | guess_format | Sentinel to try to guess format directly from file. |
subroutine open_file(self, file_action, guess_format)
!< Open file, once initialized.
class(file_stl_object), intent(inout) :: self !< File STL.
character(*), intent(in) :: file_action !< File action, "read" or "write".
logical, intent(in), optional :: guess_format !< Sentinel to try to guess format directly from file.
logical :: guess_format_ !< Sentinel to try to guess format directly from file, local var.
logical :: file_exist !< Sentinel to check if file exist.
character(5) :: ascii_header !< Ascii header sentinel.
if (allocated(self%file_name)) then
select case(trim(adjustl(file_action)))
case('read')
guess_format_ = .false. ; if (present(guess_format)) guess_format_ = guess_format
inquire(file=self%file_name, exist=file_exist)
if (file_exist) then
if (guess_format_) then
open(newunit=self%file_unit, file=self%file_name, form='formatted')
read(self%file_unit, '(A)') ascii_header
close(self%file_unit)
if (ascii_header=='solid') then
self%is_ascii = .true.
else
self%is_ascii = .false.
endif
endif
if (self%is_ascii) then
open(newunit=self%file_unit, file=self%file_name, form='formatted')
else
open(newunit=self%file_unit, file=self%file_name, access='stream', form='unformatted')
endif
self%is_open = .true.
else
write(stderr, '(A)') 'error: file "'//self%file_name//'" does not exist, impossible to open file!'
endif
case('write')
if (self%is_ascii) then
open(newunit=self%file_unit, file=self%file_name, form='formatted')
else
open(newunit=self%file_unit, file=self%file_name, access='stream', form='unformatted')
endif
self%is_open = .true.
case default
write(stderr, '(A)') 'error: file action "'//trim(adjustl(file_action))//'" unknown!'
endselect
else
write(stderr, '(A)') 'error: file name has not be initialized, impossible to open file!'
endif
endsubroutine open_file