stcarrez / ada-util

Ada Utility Library - Composing streams, processes, logs, serialization, encoders and more
Apache License 2.0
69 stars 14 forks source link

Input stream reader to read parts from another stream #40

Closed stcarrez closed 1 year ago

stcarrez commented 1 year ago

Some files are composed of mime multi-part sections as described in the RFC 1341. Each multi-part section is separated by boundaries.

We can take the advantages of the stream composing framework available in Ada Utility Library to allow reading such multi-part files.

We could use such new Input_Part_Stream type as follows:

SEP     : constant String := "" & ASCII.LF;
File    : aliased Util.Streams.Files.File_Stream;
Parts   : Util.Streams.Buffered.Parts.Input_Part_Stream;
Head, Msg, sign : Ada.Strings.Unbounded.Unbounded_String;

  File.Open (Ada.Streams.Stream_IO.In_File, Path);
  Parts.Initialize (Input => File'Unchecked_Access, Size => 4096);
  Parts.Set_Boundary (SEP & "-----BEGIN PGP SIGNED MESSAGE-----" & SEP);
  Parts.Read (Head);
  Parts.Set_Boundary (SEP & "-----BEGIN PGP SIGNATURE-----" & SEP);
  Parts.Read (Msg);
  Parts.Set_Boundary (SEP & "-----END PGP SIGNATURE-----" & SEP);
  Parts.Read (Sign);