are_1.4.0_a458cb9e/src/are.ads

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
-----------------------------------------------------------------------
--  are -- Advanced Resource Embedder
--  Copyright (C) 2021, 2023 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
private with Ada.Strings.Unbounded;
private with Ada.Finalization;
private with Ada.Streams;
private with Ada.Strings.Maps;
private with Ada.Containers.Indefinite_Ordered_Maps;
private with Ada.Containers.Indefinite_Vectors;
private with Ada.Directories;
private with Ada.Calendar;
private with Ada.Command_Line;
private with GNAT.Strings;
private with GNAT.Regpat;
private with Util.Strings.Vectors;
private with Util.Strings.Maps;
package Are is

   function "-" (Message : in String) return String is (Message);

   type Context_Type is tagged limited private;

   type Resource_Type is tagged limited private;

   type Resource_Access is access all Resource_Type;

   type Resource_List is limited private;

private

   subtype UString is Ada.Strings.Unbounded.Unbounded_String;

   subtype Character_Set is Ada.Strings.Maps.Character_Set;

   type Stream_Element_Access is access all Ada.Streams.Stream_Element_Array;

   type File_Format is (FORMAT_RAW, FORMAT_GZIP);

   --  The information about a file being embedded.
   type File_Info is record
      Content    : Stream_Element_Access;
      Length     : Ada.Directories.File_Size;
      Modtime    : Ada.Calendar.Time;
      Format     : File_Format := FORMAT_RAW;
      Line_Count : Natural := 0;
   end record;

   --  An ordered map of files being embedded.
   package File_Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => String,
                                                 Element_Type => File_Info,
                                                 "<"          => "<",
                                                 "="          => "=");

   type Format_Type is (R_BINARY, R_STRING, R_LINES, R_MAP);
   type Mapper_Type is (M_NONE, M_TEXT, M_JSON);

   type Line_Filter_Type (Size : GNAT.Regpat.Program_Size;
                          Replace_Length : Natural) is
      record
         Pattern : GNAT.Regpat.Pattern_Matcher (Size);
         Replace : String (1 .. Replace_Length);
      end record;

   package Filter_Vectors is
     new Ada.Containers.Indefinite_Vectors (Index_Type   => Positive,
                                            Element_Type => Line_Filter_Type);

   --  A resource is composed of a set of files.
   type Resource_Type is limited new Ada.Finalization.Limited_Controlled with record
      Next                : Resource_Access;
      Name                : UString;
      Format              : Format_Type := R_BINARY;
      Mapper              : Mapper_Type := M_NONE;
      Keep_Empty_Lines    : Boolean := False;
      Files               : File_Maps.Map;
      Separators          : Character_Set := Ada.Strings.Maps.Null_Set;
      Filters             : Filter_Vectors.Vector;
      Type_Name           : UString;
      Index_Type_Name     : UString;
      Content_Type_Name   : UString;
      Function_Name       : UString;
      Member_Content_Name : UString;
      Member_Length_Name  : UString;
      Member_Modtime_Name : UString;
      Member_Format_Name  : UString;
      Headers_Spec        : Util.Strings.Vectors.Vector;
      Headers_Impl        : Util.Strings.Vectors.Vector;
   end record;

   --  Get the name of type and struct/record members for the generator:
   --  - get the value from the resource library definition,
   --  - otherwise use the global context,
   --  - otherwise use the pre-defined value.
   function Get_Type_Name (Resource : in Resource_Type;
                           Context  : in Context_Type'Class;
                           Default  : in String) return String;
   function Get_Index_Type_Name (Resource : in Resource_Type;
                                 Context  : in Context_Type'Class;
                                 Default  : in String) return String;
   function Get_Content_Type_Name (Resource : in Resource_Type;
                                   Context  : in Context_Type'Class;
                                   Default  : in String) return String;
   function Get_Function_Name (Resource : in Resource_Type;
                               Context  : in Context_Type'Class;
                               Default  : in String) return String;
   function Get_Member_Content_Name (Resource : in Resource_Type;
                                     Context  : in Context_Type'Class;
                                     Default  : in String) return String;
   function Get_Member_Length_Name (Resource : in Resource_Type;
                                    Context  : in Context_Type'Class;
                                    Default  : in String) return String;
   function Get_Member_Modtime_Name (Resource : in Resource_Type;
                                     Context  : in Context_Type'Class;
                                     Default  : in String) return String;
   function Get_Member_Format_Name (Resource : in Resource_Type;
                                    Context  : in Context_Type'Class;
                                    Default  : in String) return String;

   --  Release the resources.
   overriding
   procedure Finalize (Context : in out Resource_Type);

   --  Load and add the file in the resource library.
   procedure Add_File (Resource : in out Resource_Type;
                       Name     : in String;
                       Path     : in String;
                       Override : in Boolean := False) with
     Pre  => Name'Length > 0 and then Path'Length > 0,
     Post => Resource.Files.Contains (Name);

   --  Load and add the file in the resource library.
   procedure Add_File (Resource : in out Resource_Type;
                       Name     : in String;
                       Path     : in String;
                       Modtime  : in Ada.Calendar.Time;
                       Override : in Boolean := False) with
     Pre  => Name'Length > 0 and then Path'Length > 0,
     Post => Resource.Files.Contains (Name);

   --  Add a line filter that will replace contents matching the pattern
   --  by the replacement string.
   procedure Add_Line_Filter (Resource    : in out Resource_Type;
                              Pattern     : in String;
                              Replacement : in String);

   --  Convert the file content to a list of string lines.
   procedure Convert_To_Lines (Resource : in Resource_Type;
                               File     : in File_Info;
                               Lines    : in out Util.Strings.Vectors.Vector);

   --  Convert the file content to a mapping table.
   procedure Convert_To_Map (Resource : in Resource_Type;
                             Path     : in String;
                             File     : in File_Info;
                             Context  : in out Context_Type'Class;
                             Map      : in out Util.Strings.Maps.Map);

   --  Collect the list of files names for the resource (list is sorted).
   procedure Collect_Names (Resource    : in Resource_Type;
                            Ignore_Case : in Boolean;
                            Names       : in out Util.Strings.Vectors.Vector);

   --  List of resources.
   type Resource_List is limited record
      Head   : Resource_Access;
   end record;

   --  Create a new resource with the given name.
   procedure Create_Resource (List     : in out Resource_List;
                              Name     : in String;
                              Resource : out Resource_Access) with
     Pre  => Name'Length > 0,
     Post => Resource /= null;

   --  Get the number of resources in the list.
   function Length (List : in Resource_List) return Natural;

   --  The context holding and describing information to embed.
   type Context_Type is limited new Ada.Finalization.Limited_Controlled with record
      Status      : Ada.Command_Line.Exit_Status := Ada.Command_Line.Success;
      Resources   : Resource_List;
      Verbose     : aliased Boolean := False;
      Debug       : aliased Boolean := False;
      Version     : aliased Boolean := False;
      Ignore_Case : aliased Boolean := False;
      Name_Index  : aliased Boolean := False;
      Declare_Var : aliased Boolean := False;
      No_Type_Declaration : aliased Boolean := False;
      List_Content        : aliased Boolean := False;
      Keep_Temporary      : aliased Boolean := False;
      Rule_File           : aliased GNAT.Strings.String_Access;
      Language            : aliased GNAT.Strings.String_Access;
      Output              : aliased GNAT.Strings.String_Access;
      Tmp_Dir             : aliased GNAT.Strings.String_Access;
      Type_Name           : aliased GNAT.Strings.String_Access;
      Index_Type_Name     : aliased GNAT.Strings.String_Access;
      Function_Name       : aliased GNAT.Strings.String_Access;
      Member_Content_Name : aliased GNAT.Strings.String_Access;
      Member_Length_Name  : aliased GNAT.Strings.String_Access;
      Member_Modtime_Name : aliased GNAT.Strings.String_Access;
      Member_Format_Name  : aliased GNAT.Strings.String_Access;
      Resource_Name       : aliased GNAT.Strings.String_Access;
      Fileset_Pattern     : aliased GNAT.Strings.String_Access;
      Var_Prefix          : aliased GNAT.Strings.String_Access;
   end record;

   --  Release the context information.
   overriding
   procedure Finalize (Context : in out Context_Type);

   --  Report an error and set the exit status accordingly
   procedure Error (Context : in out Context_Type;
                    Message : in String;
                    Arg1    : in String;
                    Arg2    : in String := "");
   procedure Error (Context : in out Context_Type;
                    Message : in String;
                    Arg1    : in UString;
                    Arg2    : in String := "");

   --  Get a path for the resource generation directory.
   function Get_Generation_Path (Context : in Context_Type;
                                 Name    : in String) return String;

   --  Get the path to write a file taking into account the output directory.
   function Get_Output_Path (Context : in Context_Type;
                             Name    : in String) return String;

   --  Configure the logs.
   procedure Configure_Logs (Debug   : in Boolean;
                             Verbose : in Boolean);

end Are;