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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
------------------------------------------------------------------------------
-- Ada Web Server --
-- --
-- Copyright (C) 2002-2006 --
-- AdaCore --
-- --
-- Authors: Dmitriy Anisimkov - Pascal Obry --
-- --
-- This library is free software; you can redistribute it and/or modify --
-- it under the terms of the GNU General Public License as published by --
-- the Free Software Foundation; either version 2 of the License, or (at --
-- your option) any later version. --
-- --
-- This library is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. --
-- --
-- You should have received a copy of the GNU General Public License --
-- along with this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
------------------------------------------------------------------------------
-- This programs create a parent package Root_Pck and one children for any
-- files passed as argument.
with Ada.Calendar;
with Ada.Command_Line;
with Ada.Integer_Text_IO;
with Ada.Streams;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Strings.Unbounded;
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
with GNAT.Command_Line;
with GNAT.Calendar.Time_IO;
with AWS.OS_Lib;
with AWS.Resources.Streams.Disk;
with AWS.Resources.Streams.ZLib;
with ZLib;
procedure AwsRes is
use Ada;
use Ada.Strings.Unbounded;
use AWS;
Syntax_Error : exception;
Version : constant String := "1.2";
Root_Pck : Unbounded_String := To_Unbounded_String ("res");
Quiet : Boolean := False;
RT_File : Text_IO.File_Type;
-- Root temp file
R_File : Text_IO.File_Type;
-- Root spec/body file
Compress : Boolean := False;
-- By default resources are not compressed
procedure Create (Filename : in String);
-- Create resource package for Filename
function Package_Name (Filename : in String) return String;
-- Returns package name for Filename
procedure Parse_Command_Line;
-- Parse command line
function Header return String;
-- Returns file header (AWSRes version, date, time)
------------
-- Create --
------------
procedure Create (Filename : in String) is
use Streams;
package RS renames AWS.Resources.Streams;
Max_Data : constant := 14;
-- Maximum number of data in a single line
Unit_Name : constant String := Package_Name (Filename);
Pck_Name : constant String
:= To_String (Root_Pck) & '-' & Unit_Name & ".ads";
Buffer : Stream_Element_Array (1 .. 1_024 * 200);
-- We need a buffer large enough to contain as much data as
-- possible. This is more efficient for the compression, 200kb is
-- certainly large enough for an embedded resource.
Last : Stream_Element_Offset;
I : Natural;
File_Time : Calendar.Time;
O_File : Text_IO.File_Type;
I_File : RS.Stream_Access := new RS.Disk.Stream_Type;
First : Boolean := True;
procedure Free is new Ada.Unchecked_Deallocation
(RS.Stream_Type'Class, RS.Stream_Access);
begin
if not Quiet then
Text_IO.Put ("creating " & Filename);
end if;
File_Time := AWS.OS_Lib.File_Time_Stamp (Filename);
Text_IO.Create (O_File, Text_IO.Out_File, Pck_Name);
RS.Disk.Open (RS.Disk.Stream_Type (I_File.all), Filename);
if Compress then
I_File := RS.ZLib.Deflate_Create (I_File, Header => ZLib.GZip);
end if;
-- Output package declaration
Text_IO.New_Line (O_File);
Text_IO.Put_Line (O_File, Header);
Text_IO.New_Line (O_File);
Text_IO.Put_Line (O_File, "with Ada.Streams;");
Text_IO.New_Line (O_File);
Text_IO.Put_Line (O_File, "package "
& To_String (Root_Pck) & '.' & Unit_Name & " is");
Text_IO.New_Line (O_File);
Text_IO.Put_Line (O_File, " use Ada.Streams;");
Text_IO.New_Line (O_File);
Text_IO.Put_Line
(O_File, " Content : aliased constant Stream_Element_Array :=");
-- Output file content
I := 0;
loop
RS.Read (I_File.all, Buffer, Last);
for K in Buffer'First .. Last loop
if I /= 0 then
Text_IO.Put (O_File, ",");
if I = Max_Data then
Text_IO.New_Line (O_File);
Text_IO.Put (O_File, " ");
I := 0;
end if;
end if;
if First then
-- No space after the open parentesis (style check)
declare
V : constant Integer := Integer (Buffer (K));
begin
if V < 10 then
Text_IO.Put
(O_File, " (");
Integer_Text_IO.Put (O_File, V, Width => 1);
elsif V < 100 then
Text_IO.Put
(O_File, " (");
Integer_Text_IO.Put (O_File, V, Width => 2);
else
Text_IO.Put
(O_File, " (");
Integer_Text_IO.Put (O_File, V, Width => 3);
end if;
end;
First := False;
else
Integer_Text_IO.Put
(O_File, Integer (Buffer (K)), Width => 4);
end if;
I := I + 1;
if not Quiet and then K mod 400 = 0 then
Text_IO.Put ('.');
end if;
end loop;
exit when Last < Buffer'Last;
end loop;
Text_IO.Put_Line (O_File, ");");
-- Output end of package
Text_IO.New_Line (O_File);
Text_IO.Put_Line
(O_File, "end " & To_String (Root_Pck) & '.' & Unit_Name & ';');
RS.Close (I_File.all);
Free (I_File);
Text_IO.Close (O_File);
if not Quiet then
Text_IO.New_Line;
end if;
-- Register package into root package body
Text_IO.Put_Line (RT_File, " Register");
if Compress then
Text_IO.Put_Line (RT_File, " (""" & Filename & ".gz"",");
else
Text_IO.Put_Line (RT_File, " (""" & Filename & """,");
end if;
Text_IO.Put_Line
(RT_File, " "
& To_String (Root_Pck) & '.' & Unit_Name & ".Content'Access,");
Text_IO.Put_Line
(RT_File, " GNAT.Calendar.Time_Of ("
& GNAT.Calendar.Time_IO.Image
(File_Time, "%Y, %m, %d, %H, %M, %S, 0.0));"));
if not Quiet then
Text_IO.Put_Line (" -> registered");
end if;
-- Add with clause to root body
Text_IO.Put_Line
(R_File, "with " & To_String (Root_Pck) & '.' & Unit_Name & ';');
end Create;
------------
-- Header --
------------
function Header return String is
begin
return "-- AWSRes v" & Version & " - Genarated on " &
GNAT.Calendar.Time_IO.Image
(Calendar.Clock, "%B %d %Y at %T");
end Header;
------------------
-- Package_Name --
------------------
function Package_Name (Filename : in String) return String is
From : constant String := ".";
To : constant String := "_";
Map : constant Strings.Maps.Character_Mapping
:= Strings.Maps.To_Mapping (From, To);
begin
return Strings.Fixed.Translate (Filename, Map);
end Package_Name;
------------------------
-- Parse_Command_Line --
------------------------
procedure Parse_Command_Line is
begin
GNAT.Command_Line.Initialize_Option_Scan
(Stop_At_First_Non_Switch => True);
loop
case GNAT.Command_Line.Getopt ("r: h q z u") is
when ASCII.NUL =>
exit;
when 'r' =>
Root_Pck := To_Unbounded_String (GNAT.Command_Line.Parameter);
when 'q' =>
Quiet := True;
when 'z' =>
Compress := True;
when 'u' =>
Compress := False;
when 'h' =>
raise Syntax_Error;
when others =>
raise Syntax_Error;
end case;
end loop;
end Parse_Command_Line;
Buffer : String (1 .. 100);
Last : Natural;
begin
Parse_Command_Line;
if not Quiet then
Text_IO.Put_Line ("AWSRes - Resource Creator v" & Version);
Text_IO.New_Line;
end if;
Text_IO.Create (RT_File, Text_IO.Out_File);
Text_IO.Create (R_File, Text_IO.Out_File, To_String (Root_Pck) & ".adb");
Text_IO.New_Line (R_File);
Text_IO.Put_Line (R_File, Header);
Text_IO.New_Line (R_File);
Text_IO.New_Line (RT_File);
Text_IO.Put_Line (RT_File, "with AWS.Resources.Embedded;");
Text_IO.Put_Line (RT_File, "with GNAT.Calendar;");
Text_IO.New_Line (RT_File);
Text_IO.Put_Line (RT_File, "package body " & To_String (Root_Pck) & " is");
Text_IO.New_Line (RT_File);
Text_IO.Put_Line (RT_File, " Initialized : Boolean := False;");
Text_IO.New_Line (RT_File);
Text_IO.Put_Line (RT_File, " procedure Init is");
Text_IO.Put_Line (RT_File, " use AWS.Resources.Embedded;");
Text_IO.Put_Line (RT_File, " begin");
Text_IO.Put_Line (RT_File, " if not Initialized then");
Text_IO.Put_Line (RT_File, " Initialized := True;");
-- Parse all files
loop
declare
S : constant String
:= GNAT.Command_Line.Get_Argument (Do_Expansion => True);
begin
exit when S'Length = 0;
if S = "-z" then
Compress := True;
elsif S = "-u" then
Compress := False;
else
Create (S);
end if;
end;
end loop;
Text_IO.Put_Line (RT_File, " end if;");
Text_IO.Put_Line (RT_File, " end Init;");
Text_IO.New_Line (RT_File);
Text_IO.Put_Line (RT_File, "begin");
Text_IO.Put_Line (RT_File, " Init;");
Text_IO.Put_Line (RT_File, "end " & To_String (Root_Pck) & ";");
-- Copy now all the temp root file into the body file
Text_IO.Reset (RT_File, Text_IO.In_File);
while not Text_IO.End_Of_File (RT_File) loop
Text_IO.Get_Line (RT_File, Buffer, Last);
Text_IO.Put_Line (R_File, Buffer (1 .. Last));
end loop;
Text_IO.Close (RT_File);
Text_IO.Close (R_File);
-- Generate now the root package spec
Text_IO.Create (R_File, Text_IO.Out_File, To_String (Root_Pck) & ".ads");
Text_IO.New_Line (R_File);
Text_IO.Put_Line (R_File, Header);
Text_IO.New_Line (R_File);
Text_IO.Put_Line (R_File, "package " & To_String (Root_Pck) & " is");
Text_IO.New_Line (R_File);
Text_IO.Put_Line (R_File, " procedure Init;");
Text_IO.Put_Line (R_File, " -- Register all resources files");
Text_IO.New_Line (R_File);
Text_IO.Put_Line (R_File, "end " & To_String (Root_Pck) & ";");
Text_IO.Close (R_File);
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
exception
when Syntax_Error =>
Text_IO.Put_Line ("AWSRes - Resource Creator v" & Version);
Text_IO.New_Line;
Text_IO.Put_Line ("Usage : awsres [-hrqzu] file1 [-zu] [file2...]");
Text_IO.New_Line;
Text_IO.Put_Line
(" -h : display help");
Text_IO.Put_Line
(" -r name : name of the root package (default res)");
Text_IO.Put_Line
(" -z : enable compression of following resources");
Text_IO.Put_Line
(" -u : disable compression of following resources");
Text_IO.Put_Line
(" -q : quiet mode");
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end AwsRes;
|