read_header Subroutine

public subroutine read_header(unit, version, swapped, err)

Read and validate the stream header.

Reports ERROR_IO when the file is shorter than a header, and ERROR_VALIDATION when the magic, the byte order mark or the version is not something this build understands.

Arguments

Type IntentOptional Attributes Name
integer(kind=default_int), intent(in) :: unit

Stream unit opened for unformatted stream reading.

integer(kind=int32), intent(out) :: version

Format version found in the file, 0 if the header was rejected.

logical, intent(out) :: swapped

True when the file was written by a machine of the opposite byte order, so every multi-byte field needs reversing on the way in.

type(error_t), intent(inout) :: err

Error state.


Calls

proc~~read_header~~CallsGraph proc~read_header read_header interface~to_char to_char proc~read_header->interface~to_char proc~bswap_int32 bswap_int32 proc~read_header->proc~bswap_int32 proc~check_io check_io proc~read_header->proc~check_io proc~error_set error_t%error_set proc~read_header->proc~error_set proc~to_char_char to_char_char interface~to_char->proc~to_char_char proc~to_char_dp to_char_dp interface~to_char->proc~to_char_dp proc~to_char_int32 to_char_int32 interface~to_char->proc~to_char_int32 proc~to_char_int64 to_char_int64 interface~to_char->proc~to_char_int64 proc~to_char_logical to_char_logical interface~to_char->proc~to_char_logical proc~to_char_matrix_dp to_char_matrix_dp interface~to_char->proc~to_char_matrix_dp proc~to_char_matrix_int32 to_char_matrix_int32 interface~to_char->proc~to_char_matrix_int32 proc~to_char_matrix_int64 to_char_matrix_int64 interface~to_char->proc~to_char_matrix_int64 proc~to_char_matrix_sp to_char_matrix_sp interface~to_char->proc~to_char_matrix_sp proc~to_char_sp to_char_sp interface~to_char->proc~to_char_sp proc~to_char_vector_dp to_char_vector_dp interface~to_char->proc~to_char_vector_dp proc~to_char_vector_int32 to_char_vector_int32 interface~to_char->proc~to_char_vector_int32 proc~to_char_vector_int64 to_char_vector_int64 interface~to_char->proc~to_char_vector_int64 proc~to_char_vector_sp to_char_vector_sp interface~to_char->proc~to_char_vector_sp proc~check_io->interface~to_char proc~check_io->proc~error_set

Called by

proc~~read_header~~CalledByGraph proc~read_header read_header proc~soa_read_prologue soa_read_prologue proc~soa_read_prologue->proc~read_header proc~particle_soa_deserialize particle_soa_t%particle_soa_deserialize proc~particle_soa_deserialize->proc~soa_read_prologue

Variables

Type Visibility Attributes Name Initial
integer(kind=default_int), private :: ios
character(len=len), private :: magic
integer(kind=int32), private :: mark
integer(kind=int32), private :: raw_version
integer(kind=int32), private :: reserved

Source Code

   subroutine read_header(unit, version, swapped, err)
      !! Read and validate the stream header.
      !!
      !! Reports `ERROR_IO` when the file is shorter than a header, and
      !! `ERROR_VALIDATION` when the magic, the byte order mark or the version
      !! is not something this build understands.
      integer(default_int), intent(in) :: unit
         !! Stream unit opened for unformatted stream reading.
      integer(int32), intent(out) :: version
         !! Format version found in the file, 0 if the header was rejected.
      logical, intent(out) :: swapped
         !! True when the file was written by a machine of the opposite byte
         !! order, so every multi-byte field needs reversing on the way in.
      type(error_t), intent(inout) :: err
         !! Error state.

      integer(default_int) :: ios
      character(len=len(PIC_MAGIC)) :: magic
      integer(int32) :: mark, raw_version, reserved

      version = 0_int32
      swapped = .false.

      read (unit, iostat=ios) magic
      call check_io(ios, "unexpected end of stream reading magic", err)
      if (ios /= 0) return

      if (magic /= PIC_MAGIC) then
         call err%set(ERROR_VALIDATION, "bad magic: expected '"//PIC_MAGIC//"', found '"//magic//"'")
         return
      end if

      read (unit, iostat=ios) mark
      call check_io(ios, "unexpected end of stream reading byte-order mark", err)
      if (ios /= 0) return

      if (mark == PIC_ENDIAN_MARK) then
         swapped = .false.
      else if (mark == bswap_int32(PIC_ENDIAN_MARK)) then
         swapped = .true.
      else
         call err%set(ERROR_VALIDATION, "bad byte-order mark "//to_char(mark)// &
                      ", this is not a PIC serialized stream")
         return
      end if

      read (unit, iostat=ios) raw_version
      call check_io(ios, "unexpected end of stream reading version", err)
      if (ios /= 0) return

      if (swapped) raw_version = bswap_int32(raw_version)
      if (raw_version < 1_int32 .or. raw_version > PIC_FORMAT_VERSION) then
         call err%set(ERROR_VALIDATION, "unsupported format version "//to_char(raw_version)// &
                      ", this build understands 1 to "//to_char(PIC_FORMAT_VERSION))
         return
      end if

      read (unit, iostat=ios) reserved
      call check_io(ios, "unexpected end of stream reading reserved word", err)
      if (ios /= 0) return

      version = raw_version
   end subroutine read_header