Back to... Zip-Ada

Source file : zip_streams.ads



   1  --  Contributed by ITEC - NXP Semiconductors
   2  --  June 2008
   3  --
   4  --  The Zip_Streams package defines an abstract stream
   5  --  type, Root_Zipstream_Type, with name, time and an index for random access.
   6  --
   7  --  In addition, this package provides two ready-to-use derivations:
   8  --
   9  --    - Memory_Zipstream, for using in-memory streaming
  10  --    - File_Zipstream, for accessing files
  11  --
  12  --  The Zip_Streams package can be used as such, independently
  13  --  of the Zip-Ada library.
  14  --
  15  --  Pure Ada 95+ code, 100% portable: OS-, CPU- and compiler- independent.
  16  
  17  --  Legal licensing note:
  18  
  19  --  Copyright (c) 2008 .. 2023 Gautier de Montmollin (maintainer)
  20  --  SWITZERLAND
  21  
  22  --  Permission is hereby granted, free of charge, to any person obtaining a copy
  23  --  of this software and associated documentation files (the "Software"), to deal
  24  --  in the Software without restriction, including without limitation the rights
  25  --  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  26  --  copies of the Software, and to permit persons to whom the Software is
  27  --  furnished to do so, subject to the following conditions:
  28  
  29  --  The above copyright notice and this permission notice shall be included in
  30  --  all copies or substantial portions of the Software.
  31  
  32  --  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  33  --  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  34  --  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  35  --  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  36  --  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  37  --  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  38  --  THE SOFTWARE.
  39  
  40  --  NB: this is the MIT License, as found 21-Aug-2016 on the site
  41  --  http://www.opensource.org/licenses/mit-license.php
  42  
  43  --  Change log:
  44  --  ==========
  45  --
  46  --   8-Sep-2018: GdM: ZS_Size_Type is now 64-bit signed, enabling Zip.Create
  47  --                    to capture archive size overflows.
  48  --   5-Jul-2013: GdM: Added proper types for stream sizes and index
  49  --  20-Nov-2012: GdM: Added Is_Open method for File_Zipstream
  50  --  30-Oct-2012: GdM/NB: - Removed method profiles with 'access' as
  51  --                           overriding some methods with 'access' and some without
  52  --                           at different inheritance levels may be dangerous
  53  --                       - renamed Zipstream_Class Zipstream_Class_Access
  54  --                           (the right name for it)
  55  --  25-Oct-2012: GdM: All methods also with pointer-free profiles
  56  --                     (no more anonymous 'access', nor access types needed)
  57  --  20-Jul-2011: GdM/JH: - Underscore in Get_Name, Set_Name, Get_Time, Set_Time
  58  --                       - The 4 methods above are not anymore abstract
  59  --                       - Name and Modification_Time fields moved to Root_Zipstream_Type
  60  --                       - Unbounded_Stream becomes Memory_Zipstream
  61  --                       - ZipFile_Stream becomes File_Zipstream
  62  --  17-Jul-2011: JH : Added Set_Unicode_Name_Flag, Is_Unicode_Name
  63  --  25-Nov-2009: GdM: Added an own time type -> it is possible to bypass Ada.Calendar
  64  --  18-Jan-2009: GdM: Fixed Zip_Streams.Read which did read
  65  --                      only Item's first element
  66  
  67  with Ada.Calendar,
  68       Ada.Streams.Stream_IO,
  69       Ada.Strings.Unbounded;
  70  
  71  with Interfaces;
  72  
  73  package Zip_Streams is
  74  
  75     use Interfaces;
  76  
  77     --  We define an own Time (Ada.Calendar's body can be very time-consuming!)
  78     --  See subpackage Calendar below for own Split, Time_Of and Convert from/to
  79     --  Ada.Calendar.Time.
  80     type Time is private;
  81  
  82     default_time   : constant Time;  --  some default time
  83     special_time_1 : constant Time;  --  special time code (for users of Zip_Streams)
  84     special_time_2 : constant Time;  --  special time code (for users of Zip_Streams)
  85  
  86     ------------------------------------------------------
  87     --  Root_Zipstream_Type: root abstract stream type  --
  88     ------------------------------------------------------
  89  
  90     type Root_Zipstream_Type is abstract new Ada.Streams.Root_Stream_Type with private;
  91     type Zipstream_Class_Access is access all Root_Zipstream_Type'Class;
  92  
  93     subtype ZS_Size_Type is Integer_64 range 0 .. Integer_64'Last;
  94     subtype ZS_Index_Type is ZS_Size_Type range 1 .. ZS_Size_Type'Last;
  95  
  96     --  Set the index on the stream
  97     procedure Set_Index (S : in out Root_Zipstream_Type;
  98                          To : ZS_Index_Type) is abstract;
  99  
 100     --  Returns the index of the stream
 101     function Index (S : in Root_Zipstream_Type) return ZS_Index_Type is abstract;
 102  
 103     --  Returns the Size of the stream
 104     function Size (S : in Root_Zipstream_Type) return ZS_Size_Type is abstract;
 105  
 106     --  This procedure sets the name of the stream
 107     procedure Set_Name (S : in out Root_Zipstream_Type; Name : String);
 108  
 109     --  This procedure returns the name of the stream
 110     function Get_Name (S : in Root_Zipstream_Type) return String;
 111  
 112     procedure Set_Unicode_Name_Flag (S     : out Root_Zipstream_Type;
 113                                      Value : in Boolean);
 114     function Is_Unicode_Name (S : in Root_Zipstream_Type)
 115                               return Boolean;
 116  
 117     procedure Set_Read_Only_Flag (S     : out Root_Zipstream_Type;
 118                                   Value : in Boolean);
 119     function Is_Read_Only (S : in Root_Zipstream_Type)
 120                            return Boolean;
 121  
 122     --  This procedure sets the Modification_Time of the stream
 123     procedure Set_Time (S : in out Root_Zipstream_Type;
 124                         Modification_Time : Time);
 125  
 126     --  Set_Time again, but with the standard Ada Time type.
 127     --  Overriding is useless and potentially harmful, so we prevent it with
 128     --  a class-wide profile.
 129     procedure Set_Time (S : in out Root_Zipstream_Type'Class;
 130                         Modification_Time : Ada.Calendar.Time);
 131  
 132     --  This procedure returns the ModificationTime of the stream
 133     function Get_Time (S : in Root_Zipstream_Type)
 134                        return Time;
 135  
 136     --  Get_Time again, but with the standard Ada Time type.
 137     --  Overriding is useless and potentially harmful, so we prevent it with
 138     --  a class-wide profile.
 139     function Get_Time (S : in Root_Zipstream_Type'Class)
 140                        return Ada.Calendar.Time;
 141  
 142     --  Returns true if the index is at the end of the stream, else false
 143     function End_Of_Stream (S : in Root_Zipstream_Type)
 144        return Boolean is abstract;
 145  
 146     -----------------------------------------------------------------------
 147     --  Memory_Zipstream: stream based on an in-memory Unbounded_String  --
 148     -----------------------------------------------------------------------
 149     type Memory_Zipstream is new Root_Zipstream_Type with private;
 150  
 151     --  Get the complete value (contents) of the stream
 152     procedure Get (Str : Memory_Zipstream; Unb : out Ada.Strings.Unbounded.Unbounded_String);
 153  
 154     --  Set a value in the stream, the index will be set
 155     --  to null and old data in the stream will be lost.
 156     procedure Set (Str : in out Memory_Zipstream; Unb : Ada.Strings.Unbounded.Unbounded_String);
 157  
 158     ----------------------------------------------
 159     --  File_Zipstream: stream based on a file  --
 160     ----------------------------------------------
 161     type File_Zipstream is new Root_Zipstream_Type with private;
 162  
 163     type File_Mode is new Ada.Streams.Stream_IO.File_Mode;
 164  
 165     --  Open the File_Zipstream
 166     --  PRE: Str.Name must be set
 167     procedure Open (Str : in out File_Zipstream; Mode : File_Mode);
 168  
 169     --  Creates a file on the disk
 170     --  PRE: Str.Name must be set
 171     procedure Create (Str : in out File_Zipstream; Mode : File_Mode);
 172  
 173     --  Close the File_Zipstream
 174     procedure Close (Str : in out File_Zipstream);
 175  
 176     --  Is the File_Zipstream open ?
 177     function Is_Open (Str : in File_Zipstream) return Boolean;
 178  
 179     ----------------------------
 180     --  Routines around Time  --
 181     ----------------------------
 182  
 183     package Calendar is
 184        --
 185        function Convert (Date : in Ada.Calendar.Time) return Time;
 186        function Convert (Date : in Time) return Ada.Calendar.Time;
 187        --
 188        subtype DOS_Time is Interfaces.Unsigned_32;
 189        function Convert (Date : in DOS_Time) return Time;
 190        function Convert (Date : in Time) return DOS_Time;
 191        --
 192        Time_Error : exception;
 193        --
 194        procedure Split
 195          (Date       : Time;
 196           To_Year    : out Ada.Calendar.Year_Number;
 197           To_Month   : out Ada.Calendar.Month_Number;
 198           To_Day     : out Ada.Calendar.Day_Number;
 199           To_Seconds : out Ada.Calendar.Day_Duration);
 200        --
 201        function Time_Of
 202          (From_Year    : Ada.Calendar.Year_Number;
 203           From_Month   : Ada.Calendar.Month_Number;
 204           From_Day     : Ada.Calendar.Day_Number;
 205           From_Seconds : Ada.Calendar.Day_Duration := 0.0) return Time;
 206        --
 207        function ">" (Left, Right : Time) return Boolean;
 208     end Calendar;
 209  
 210    --  Parameter Form added to *_IO.[Open|Create]
 211    --  See RM A.8.2: File Management
 212    --  Example: "encoding=8bits", "encoding=utf8"
 213    --
 214    Form_For_IO_Open_and_Create : Ada.Strings.Unbounded.Unbounded_String
 215      := Ada.Strings.Unbounded.Null_Unbounded_String;
 216  
 217  private
 218  
 219     --  Time. Currently, DOS format (pkzip appnote.txt: part V., J.), as stored
 220     --  in Zip archives. Subject to change, this is why this type is private.
 221     type Time is new Interfaces.Unsigned_32;
 222  
 223     default_time   : constant Time := 16789 * 65536;
 224     special_time_1 : constant Time := default_time + 1;
 225     special_time_2 : constant Time := default_time + 2;
 226  
 227     type Root_Zipstream_Type is abstract new Ada.Streams.Root_Stream_Type with
 228        record
 229           Name              : Ada.Strings.Unbounded.Unbounded_String;
 230           Modification_Time : Time := default_time;
 231           Is_Unicode_Name   : Boolean := False;
 232           Is_Read_Only      : Boolean := False;  --  only indicative
 233        end record;
 234  
 235     --  Memory_Zipstream spec
 236     type Memory_Zipstream is new Root_Zipstream_Type with
 237        record
 238           Unb : Ada.Strings.Unbounded.Unbounded_String;
 239           Loc : Integer := 1;
 240        end record;
 241     --  Read data from the stream.
 242     overriding procedure Read
 243       (Stream : in out Memory_Zipstream;
 244        Item   :    out Ada.Streams.Stream_Element_Array;
 245        Last   :    out Ada.Streams.Stream_Element_Offset);
 246  
 247     --  Write data to the stream, starting from the current index.
 248     --  Data will be overwritten from index if already available.
 249     overriding procedure Write
 250       (Stream : in out Memory_Zipstream;
 251        Item   :        Ada.Streams.Stream_Element_Array);
 252  
 253     --  Set the index on the stream
 254     overriding procedure Set_Index (S : in out Memory_Zipstream; To : ZS_Index_Type);
 255  
 256     --  Returns the index of the stream
 257     overriding function Index (S : in Memory_Zipstream) return ZS_Index_Type;
 258  
 259     --  Returns the Size of the stream
 260     overriding function Size (S : in Memory_Zipstream) return ZS_Size_Type;
 261  
 262     --  Returns true if the index is at the end of the stream
 263     overriding function End_Of_Stream (S : in Memory_Zipstream) return Boolean;
 264  
 265     --  File_Zipstream spec
 266     type File_Zipstream is new Root_Zipstream_Type with
 267        record
 268           File : Ada.Streams.Stream_IO.File_Type;
 269        end record;
 270     --  Read data from the stream.
 271     overriding procedure Read
 272       (Stream : in out File_Zipstream;
 273        Item   :    out Ada.Streams.Stream_Element_Array;
 274        Last   :    out Ada.Streams.Stream_Element_Offset);
 275  
 276     --  Write data to the stream, starting from the current index.
 277     --  Data will be overwritten from index if already available.
 278     overriding procedure Write
 279       (Stream : in out File_Zipstream;
 280        Item   :        Ada.Streams.Stream_Element_Array);
 281  
 282     --  Set the index on the stream
 283     overriding procedure Set_Index (S : in out File_Zipstream; To : ZS_Index_Type);
 284  
 285     --  Returns the index of the stream
 286     overriding function Index (S : in File_Zipstream) return ZS_Index_Type;
 287  
 288     --  Returns the Size of the stream
 289     overriding function Size (S : in File_Zipstream) return ZS_Size_Type;
 290  
 291     --  Returns true if the index is at the end of the stream
 292     overriding function End_Of_Stream (S : in File_Zipstream) return Boolean;
 293  
 294  end Zip_Streams;

Web view of Ada source code generated by GNATHTML, project: ALI_Parse version 1.0.
Zip-Ada: Ada library for zip archive files (.zip). Ada programming.
Some news about Zip-Ada and other Ada projects on Gautier's blog.