Read and validate the prologue, returning the element count.
Fails with ERROR_VALIDATION when the schema in the stream is not
exactly schema, naming both, and when the element count record is
malformed or does not fit this build’s default_int. The envelope
itself – magic, byte order, version – is validated by
pic_serialize.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=default_int), | intent(in) | :: | unit |
Stream unit opened for unformatted stream reading. |
||
| character(len=*), | intent(in) | :: | schema |
Schema the calling container expects. |
||
| integer(kind=default_int), | intent(out) | :: | n |
Number of elements the stream holds, 0 when the prologue is rejected. |
||
| logical, | intent(out) | :: | swapped |
True when the stream came from the opposite byte order; pass it on
to every |
||
| type(error_t), | intent(inout) | :: | err |
Error state. |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer(kind=int64), | private, | allocatable | :: | counts(:) | |||
| character(len=:), | private, | allocatable | :: | found | |||
| integer(kind=int32), | private | :: | version |
subroutine soa_read_prologue(unit, schema, n, swapped, err) !! Read and validate the prologue, returning the element count. !! !! Fails with `ERROR_VALIDATION` when the schema in the stream is not !! exactly `schema`, naming both, and when the element count record is !! malformed or does not fit this build's `default_int`. The envelope !! itself -- magic, byte order, version -- is validated by !! `pic_serialize`. integer(default_int), intent(in) :: unit !! Stream unit opened for unformatted stream reading. character(len=*), intent(in) :: schema !! Schema the calling container expects. integer(default_int), intent(out) :: n !! Number of elements the stream holds, 0 when the prologue is !! rejected. logical, intent(out) :: swapped !! True when the stream came from the opposite byte order; pass it on !! to every `soa_read_field` call. type(error_t), intent(inout) :: err !! Error state. integer(int32) :: version character(len=:), allocatable :: found integer(int64), allocatable :: counts(:) n = 0 swapped = .false. call read_header(unit, version, swapped, err) if (err%has_error()) return call read_array(unit, found, swapped, err) if (err%has_error()) return ! Length is compared as well as content because Fortran character ! comparison blank-pads the shorter operand, which would let a schema ! with trailing blanks masquerade as this one. if (len(found) /= len(schema) .or. found /= schema) then call err%set(ERROR_VALIDATION, "soa layout mismatch: stream declares '"//found// & "', this container is '"//schema//"'") return end if call read_array(unit, counts, swapped, err) if (err%has_error()) return if (size(counts, kind=default_int) /= 1) then call err%set(ERROR_VALIDATION, "soa: element count record holds "// & to_char(int(size(counts, kind=default_int), int64))//" values, expected 1") return end if if (counts(1) < 0_int64 .or. counts(1) > int(huge(0_default_int), int64)) then call err%set(ERROR_VALIDATION, "soa: element count "//to_char(counts(1))// & " is out of range for this build") return end if n = int(counts(1), default_int) end subroutine soa_read_prologue