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.
| Type | Intent | Optional | 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. |
| 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 |
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