1. -- 
  2. -- Copyright (c) 2012 Kevin Wellwood 
  3. -- All rights reserved. 
  4. -- 
  5. -- This source code is distributed under the Modified BSD License. For terms and 
  6. -- conditions, see license.txt. 
  7. -- 
  8.  
  9. package Streams.Buffers is 
  10.  
  11.     -- A Buffer_Stream is a stream implementation backed by a read-only buffer. 
  12.     -- The stream can be wrapped around an existing buffer to read its contents 
  13.     -- (use Stream constructor function), or a buffer can be consumed into the 
  14.     -- stream and freed automatically when the stream is closed (use Open_Stream 
  15.     -- constructor procedure). 
  16.     type Buffer_Stream is new Root_Stream_Type with private; 
  17.     type A_Buffer_Stream is access all Buffer_Stream'Class; 
  18.  
  19.     -- Creates a Buffer_Stream backed by 'buffer', which is consumed by this 
  20.     -- procedure. Closing the stream will deallocate the buffer. 
  21.     procedure Open_Stream( buffer : in out A_SEA; stream : out A_Buffer_Stream ); 
  22.     pragma Precondition( buffer /= null ); 
  23.     pragma Postcondition( buffer = null ); 
  24.     pragma Postcondition( stream /= null ); 
  25.  
  26.     -- Creates a Buffer_Stream wrapper around 'buffer'. The stream depends on 
  27.     -- the existance of buffer so be sure to call Close on the stream before 
  28.     -- deallocating the buffer. 
  29.     function Stream( buffer : not null A_SEA ) return A_Buffer_Stream; 
  30.     pragma Postcondition( Stream'Result /= null ); 
  31.  
  32.     -- Creates a Buffer_Stream wrapper around a copy of 'buffer'. It is 
  33.     -- recommended that only Characters be read from this type of buffer stream. 
  34.     function Stream( buffer : String ) return A_Buffer_Stream; 
  35.     pragma Postcondition( Stream'Result /= null ); 
  36.  
  37.     -- Closes the stream and deallocates its buffer (if it had ownership of it, 
  38.     -- given by calling Open_Stream.) 
  39.     procedure Close( stream : in out A_Buffer_Stream ); 
  40.     pragma Postcondition( stream = null ); 
  41.  
  42. private 
  43.  
  44.     type Buffer_Stream is new Root_Stream_Type with record 
  45.         buffer  : A_SEA := null; 
  46.         freeBuf : Boolean := False;  -- True if the stream's underlying 
  47.                                      -- buffer was consumed by the stream and 
  48.                                      -- should be freed when the stream is 
  49.                                      -- closed. 
  50.         readPos : Stream_Element_Offset; 
  51.     end record; 
  52.  
  53.     -- 'last' will be set to item'First - 1 when the stream is empty. 
  54.     procedure Read( stream : in out Buffer_Stream; 
  55.                     item   : out Stream_Element_Array; 
  56.                     last   : out Stream_Element_Offset ); 
  57.  
  58.     -- Writing is not supported. Raises STREAM_ERROR. 
  59.     procedure Write( stream : in out Buffer_Stream; 
  60.                      item   : Stream_Element_Array ); 
  61.  
  62. end Streams.Buffers;