ADDED generated/natools-static_maps-s_expressions-templates-dates-cmds.adb Index: generated/natools-static_maps-s_expressions-templates-dates-cmds.adb ================================================================== --- generated/natools-static_maps-s_expressions-templates-dates-cmds.adb +++ generated/natools-static_maps-s_expressions-templates-dates-cmds.adb @@ -0,0 +1,34 @@ +with Interfaces; use Interfaces; + +package body Natools.Static_Maps.S_Expressions.Templates.Dates.Cmds is + + P : constant array (0 .. 5) of Natural := + (1, 2, 5, 11, 12, 15); + + T1 : constant array (0 .. 5) of Unsigned_8 := + (5, 22, 23, 31, 6, 10); + + T2 : constant array (0 .. 5) of Unsigned_8 := + (42, 40, 11, 44, 54, 41); + + G : constant array (0 .. 57) of Unsigned_8 := + (0, 0, 0, 0, 9, 0, 22, 3, 0, 0, 1, 11, 4, 0, 0, 0, 17, 0, 5, 0, 13, 0, + 0, 0, 0, 0, 0, 0, 0, 16, 0, 2, 0, 15, 0, 0, 5, 18, 0, 0, 0, 0, 6, 10, + 10, 20, 23, 0, 3, 3, 27, 16, 0, 20, 21, 9, 0, 22); + + function Hash (S : String) return Natural is + F : constant Natural := S'First - 1; + L : constant Natural := S'Length; + F1, F2 : Natural := 0; + J : Natural; + begin + for K in P'Range loop + exit when L < P (K); + J := Character'Pos (S (P (K) + F)); + F1 := (F1 + Natural (T1 (K)) * J) mod 58; + F2 := (F2 + Natural (T2 (K)) * J) mod 58; + end loop; + return (Natural (G (F1)) + Natural (G (F2))) mod 28; + end Hash; + +end Natools.Static_Maps.S_Expressions.Templates.Dates.Cmds; ADDED generated/natools-static_maps-s_expressions-templates-dates-cmds.ads Index: generated/natools-static_maps-s_expressions-templates-dates-cmds.ads ================================================================== --- generated/natools-static_maps-s_expressions-templates-dates-cmds.ads +++ generated/natools-static_maps-s_expressions-templates-dates-cmds.ads @@ -0,0 +1,4 @@ +package Natools.Static_Maps.S_Expressions.Templates.Dates.Cmds is + pragma Pure; + function Hash (S : String) return Natural; +end Natools.Static_Maps.S_Expressions.Templates.Dates.Cmds; ADDED generated/natools-static_maps-s_expressions-templates-dates-t.adb Index: generated/natools-static_maps-s_expressions-templates-dates-t.adb ================================================================== --- generated/natools-static_maps-s_expressions-templates-dates-t.adb +++ generated/natools-static_maps-s_expressions-templates-dates-t.adb @@ -0,0 +1,17 @@ +-- Generated at 2014-10-03 21:03:09 +0000 by Natools.Static_Hash_Maps +-- from src/natools-s_expressions-templates-dates-maps.sx + +with Natools.Static_Maps.S_Expressions.Templates.Dates.Cmds; +function Natools.Static_Maps.S_Expressions.Templates.Dates.T + return Boolean is +begin + for I in Map_1_Keys'Range loop + if Natools.Static_Maps.S_Expressions.Templates.Dates.Cmds.Hash + (Map_1_Keys (I).all) /= I + then + return False; + end if; + end loop; + + return True; +end Natools.Static_Maps.S_Expressions.Templates.Dates.T; ADDED generated/natools-static_maps-s_expressions-templates-dates-t.ads Index: generated/natools-static_maps-s_expressions-templates-dates-t.ads ================================================================== --- generated/natools-static_maps-s_expressions-templates-dates-t.ads +++ generated/natools-static_maps-s_expressions-templates-dates-t.ads @@ -0,0 +1,6 @@ +-- Generated at 2014-10-03 21:03:09 +0000 by Natools.Static_Hash_Maps +-- from src/natools-s_expressions-templates-dates-maps.sx + +function Natools.Static_Maps.S_Expressions.Templates.Dates.T + return Boolean; +pragma Pure (Natools.Static_Maps.S_Expressions.Templates.Dates.T); ADDED generated/natools-static_maps-s_expressions-templates-dates.adb Index: generated/natools-static_maps-s_expressions-templates-dates.adb ================================================================== --- generated/natools-static_maps-s_expressions-templates-dates.adb +++ generated/natools-static_maps-s_expressions-templates-dates.adb @@ -0,0 +1,19 @@ +-- Generated at 2014-10-03 21:03:09 +0000 by Natools.Static_Hash_Maps +-- from src/natools-s_expressions-templates-dates-maps.sx + +with Natools.Static_Maps.S_Expressions.Templates.Dates.Cmds; + +package body Natools.Static_Maps.S_Expressions.Templates.Dates is + + function Main (Key : String) return Main_Command is + N : constant Natural + := Natools.Static_Maps.S_Expressions.Templates.Dates.Cmds.Hash (Key); + begin + if Map_1_Keys (N).all = Key then + return Map_1_Elements (N); + else + return Error; + end if; + end Main; + +end Natools.Static_Maps.S_Expressions.Templates.Dates; ADDED generated/natools-static_maps-s_expressions-templates-dates.ads Index: generated/natools-static_maps-s_expressions-templates-dates.ads ================================================================== --- generated/natools-static_maps-s_expressions-templates-dates.ads +++ generated/natools-static_maps-s_expressions-templates-dates.ads @@ -0,0 +1,108 @@ +-- Generated at 2014-10-03 21:03:09 +0000 by Natools.Static_Hash_Maps +-- from src/natools-s_expressions-templates-dates-maps.sx + +package Natools.Static_Maps.S_Expressions.Templates.Dates is + pragma Pure; + + type Main_Command is + (Error, + Year, Month, Day, Hour, Minute, Second, + Padded_Month, Padded_Day, + Padded_Hour, Padded_Minute, Padded_Second, + Day_Of_Week, + Big_Endian_Date, Little_Endian_Date, + Big_Endian_Time, Little_Endian_Time, + RFC_3339); + + function Main (Key : String) return Main_Command; + +private + + Map_1_Key_0 : aliased constant String := "YYYYMMDD"; + Map_1_Key_1 : aliased constant String := "big-endian-date"; + Map_1_Key_2 : aliased constant String := "HHMMSS"; + Map_1_Key_3 : aliased constant String := "time"; + Map_1_Key_4 : aliased constant String := "big-endian-time"; + Map_1_Key_5 : aliased constant String := "day"; + Map_1_Key_6 : aliased constant String := "dow"; + Map_1_Key_7 : aliased constant String := "day-of-week"; + Map_1_Key_8 : aliased constant String := "hour"; + Map_1_Key_9 : aliased constant String := "DDMMYYYY"; + Map_1_Key_10 : aliased constant String := "little-endian-date"; + Map_1_Key_11 : aliased constant String := "SSMMHH"; + Map_1_Key_12 : aliased constant String := "little-endian-time"; + Map_1_Key_13 : aliased constant String := "minute"; + Map_1_Key_14 : aliased constant String := "month"; + Map_1_Key_15 : aliased constant String := "0day"; + Map_1_Key_16 : aliased constant String := "padded-day"; + Map_1_Key_17 : aliased constant String := "0hour"; + Map_1_Key_18 : aliased constant String := "padded-hour"; + Map_1_Key_19 : aliased constant String := "0minute"; + Map_1_Key_20 : aliased constant String := "padded-minute"; + Map_1_Key_21 : aliased constant String := "0month"; + Map_1_Key_22 : aliased constant String := "padded-month"; + Map_1_Key_23 : aliased constant String := "0second"; + Map_1_Key_24 : aliased constant String := "padded-second"; + Map_1_Key_25 : aliased constant String := "rfc-3339"; + Map_1_Key_26 : aliased constant String := "second"; + Map_1_Key_27 : aliased constant String := "year"; + Map_1_Keys : constant array (0 .. 27) of access constant String + := (Map_1_Key_0'Access, + Map_1_Key_1'Access, + Map_1_Key_2'Access, + Map_1_Key_3'Access, + Map_1_Key_4'Access, + Map_1_Key_5'Access, + Map_1_Key_6'Access, + Map_1_Key_7'Access, + Map_1_Key_8'Access, + Map_1_Key_9'Access, + Map_1_Key_10'Access, + Map_1_Key_11'Access, + Map_1_Key_12'Access, + Map_1_Key_13'Access, + Map_1_Key_14'Access, + Map_1_Key_15'Access, + Map_1_Key_16'Access, + Map_1_Key_17'Access, + Map_1_Key_18'Access, + Map_1_Key_19'Access, + Map_1_Key_20'Access, + Map_1_Key_21'Access, + Map_1_Key_22'Access, + Map_1_Key_23'Access, + Map_1_Key_24'Access, + Map_1_Key_25'Access, + Map_1_Key_26'Access, + Map_1_Key_27'Access); + Map_1_Elements : constant array (0 .. 27) of Main_Command + := (Big_Endian_Date, + Big_Endian_Date, + Big_Endian_Time, + Big_Endian_Time, + Big_Endian_Time, + Day, + Day_Of_Week, + Day_Of_Week, + Hour, + Little_Endian_Date, + Little_Endian_Date, + Little_Endian_Time, + Little_Endian_Time, + Minute, + Month, + Padded_Day, + Padded_Day, + Padded_Hour, + Padded_Hour, + Padded_Minute, + Padded_Minute, + Padded_Month, + Padded_Month, + Padded_Second, + Padded_Second, + RFC_3339, + Second, + Year); + +end Natools.Static_Maps.S_Expressions.Templates.Dates; ADDED src/natools-s_expressions-templates-dates-maps.sx Index: src/natools-s_expressions-templates-dates-maps.sx ================================================================== --- src/natools-s_expressions-templates-dates-maps.sx +++ src/natools-s_expressions-templates-dates-maps.sx @@ -0,0 +1,35 @@ +(Natools.Static_Maps.S_Expressions.Templates.Dates + pure + (test-function T) + (extra-decl "\ + type Main_Command is + (Error, + Year, Month, Day, Hour, Minute, Second, + Padded_Month, Padded_Day, + Padded_Hour, Padded_Minute, Padded_Second, + Day_Of_Week, + Big_Endian_Date, Little_Endian_Date, + Big_Endian_Time, Little_Endian_Time, + RFC_3339);") + (Main_Command + (hash-package Natools.Static_Maps.S_Expressions.Templates.Dates.Cmds) + (function Main) + (not-found Error) + (nodes + (Big_Endian_Date YYYYMMDD big-endian-date) + (Big_Endian_Time HHMMSS time big-endian-time) + (Day day) + (Day_Of_Week dow day-of-week) + (Hour hour) + (Little_Endian_Date DDMMYYYY little-endian-date) + (Little_Endian_Time SSMMHH little-endian-time) + (Minute minute) + (Month month) + (Padded_Day 0day padded-day) + (Padded_Hour 0hour padded-hour) + (Padded_Minute 0minute padded-minute) + (Padded_Month 0month padded-month) + (Padded_Second 0second padded-second) + (RFC_3339 rfc-3339) + (Second second) + (Year year)))) ADDED src/natools-s_expressions-templates-dates.adb Index: src/natools-s_expressions-templates-dates.adb ================================================================== --- src/natools-s_expressions-templates-dates.adb +++ src/natools-s_expressions-templates-dates.adb @@ -0,0 +1,300 @@ +------------------------------------------------------------------------------ +-- Copyright (c) 2014, Natacha Porté -- +-- -- +-- Permission to use, copy, modify, and distribute this software for any -- +-- purpose with or without fee is hereby granted, provided that the above -- +-- copyright notice and this permission notice appear in all copies. -- +-- -- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -- +-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -- +-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -- +-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -- +-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -- +-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -- +-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- +------------------------------------------------------------------------------ + +with Natools.S_Expressions.Interpreter_Loop; +with Natools.S_Expressions.Templates.Generic_Discrete_Render; +with Natools.S_Expressions.Templates.Integers; +with Natools.Static_Maps.S_Expressions.Templates.Dates; +with Natools.Time_IO.RFC_3339; + +package body Natools.S_Expressions.Templates.Dates is + + package Commands renames Natools.Static_Maps.S_Expressions.Templates.Dates; + + procedure Render_Day_Of_Week + is new Natools.S_Expressions.Templates.Generic_Discrete_Render + (Ada.Calendar.Formatting.Day_Name, + Ada.Calendar.Formatting.Day_Name'Image, + Ada.Calendar.Formatting."="); + + procedure Append + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Value : in Split_Time; + Data : in Atom); + + procedure Execute + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Value : in Split_Time; + Name : in Atom; + Arguments : in out Lockable.Descriptor'Class); + + function Two_Digit_Image (Value : Integer) return Atom + is ((1 => Character'Pos ('0') + Octet (Value / 10), + 2 => Character'Pos ('0') + Octet (Value mod 10))) + with Pre => Value in 0 .. 99; + + function Four_Digit_Image (Value : Integer) return Atom + is ((1 => Character'Pos ('0') + Octet (Value / 1000), + 2 => Character'Pos ('0') + Octet ((Value / 100) mod 10), + 3 => Character'Pos ('0') + Octet ((Value / 10) mod 10), + 4 => Character'Pos ('0') + Octet (Value mod 10))) + with Pre => Value in 0 .. 9999; + + procedure Render_Triplet + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Part_1, Part_2, Part_3 : in Atom; + Template : in out Lockable.Descriptor'Class); + + + ------------------------------ + -- Local Helper Subprograms -- + ------------------------------ + + procedure Render_Triplet + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Part_1, Part_2, Part_3 : in Atom; + Template : in out Lockable.Descriptor'Class) is + begin + Output.Write (Part_1); + + if Template.Current_Event = Events.Add_Atom then + declare + Separator : constant Atom := Template.Current_Atom; + Event : Events.Event; + begin + Template.Next (Event); + + Output.Write (Separator); + Output.Write (Part_2); + + if Event = Events.Add_Atom then + Output.Write (Template.Current_Atom); + else + Output.Write (Separator); + end if; + end; + else + Output.Write (Part_2); + end if; + + Output.Write (Part_3); + end Render_Triplet; + + + + ---------------------------- + -- Interpreter Components -- + ---------------------------- + + procedure Append + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Value : in Split_Time; + Data : in Atom) + is + pragma Unreferenced (Value); + begin + Output.Write (Data); + end Append; + + + procedure Execute + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Value : in Split_Time; + Name : in Atom; + Arguments : in out Lockable.Descriptor'Class) + is + Format : Integers.Format; + begin + case Commands.Main (To_String (Name)) is + when Commands.Error => + null; + + when Commands.Big_Endian_Date => + Render_Triplet + (Output, + Four_Digit_Image (Value.Year), + Two_Digit_Image (Value.Month), + Two_Digit_Image (Value.Day), + Arguments); + + when Commands.Big_Endian_Time => + Render_Triplet + (Output, + Two_Digit_Image (Value.Hour), + Two_Digit_Image (Value.Minute), + Two_Digit_Image (Value.Second), + Arguments); + + when Commands.Day => + Format.Set_Image (0, Null_Atom); + Integers.Render (Output, Format, Arguments, Value.Day); + + when Commands.Day_Of_Week => + Render_Day_Of_Week (Output, Arguments, Value.Day_Of_Week); + + when Commands.Hour => + Format.Set_Image (-1, Null_Atom); + Integers.Render (Output, Format, Arguments, Value.Hour); + + when Commands.Little_Endian_Date => + Render_Triplet + (Output, + Two_Digit_Image (Value.Day), + Two_Digit_Image (Value.Month), + Four_Digit_Image (Value.Year), + Arguments); + + when Commands.Little_Endian_Time => + Render_Triplet + (Output, + Two_Digit_Image (Value.Second), + Two_Digit_Image (Value.Minute), + Two_Digit_Image (Value.Hour), + Arguments); + + when Commands.Minute => + Format.Set_Image (-1, Null_Atom); + Integers.Render (Output, Format, Arguments, Value.Minute); + + when Commands.Month => + Format.Set_Image (0, Null_Atom); + Integers.Render (Output, Format, Arguments, Value.Month); + + when Commands.Padded_Day => + Format.Set_Image (0, Null_Atom); + Format.Set_Minimum_Width (2); + Format.Set_Left_Padding ((1 => Character'Pos ('0'))); + Format.Set_Align (Integers.Right_Aligned); + Integers.Render (Output, Format, Arguments, Value.Day); + + when Commands.Padded_Hour => + Format.Set_Image (-1, Null_Atom); + Format.Set_Minimum_Width (2); + Format.Set_Left_Padding ((1 => Character'Pos ('0'))); + Format.Set_Align (Integers.Right_Aligned); + Integers.Render (Output, Format, Arguments, Value.Hour); + + when Commands.Padded_Minute => + Format.Set_Image (-1, Null_Atom); + Format.Set_Minimum_Width (2); + Format.Set_Left_Padding ((1 => Character'Pos ('0'))); + Format.Set_Align (Integers.Right_Aligned); + Integers.Render (Output, Format, Arguments, Value.Minute); + + when Commands.Padded_Month => + Format.Set_Image (0, Null_Atom); + Format.Set_Minimum_Width (2); + Format.Set_Left_Padding ((1 => Character'Pos ('0'))); + Format.Set_Align (Integers.Right_Aligned); + Integers.Render (Output, Format, Arguments, Value.Month); + + when Commands.Padded_Second => + Format.Set_Image (-1, Null_Atom); + Format.Set_Minimum_Width (2); + Format.Set_Left_Padding ((1 => Character'Pos ('0'))); + Format.Set_Align (Integers.Right_Aligned); + Integers.Render (Output, Format, Arguments, Value.Second); + + when Commands.RFC_3339 => + Output.Write (To_Atom + (Time_IO.RFC_3339.Image (Value.Source, Value.Time_Zone))); + + when Commands.Second => + Format.Set_Image (-1, Null_Atom); + Integers.Render (Output, Format, Arguments, Value.Second); + + when Commands.Year => + Integers.Render (Output, Arguments, Value.Year); + end case; + end Execute; + + + procedure Interpreter is new Interpreter_Loop + (Ada.Streams.Root_Stream_Type'Class, Split_Time, Execute, Append); + + + + ---------------------- + -- Public Interface -- + ---------------------- + + function Split + (Value : Ada.Calendar.Time; + Time_Zone : Ada.Calendar.Time_Zones.Time_Offset) + return Split_Time + is + Year : Ada.Calendar.Year_Number; + Month : Ada.Calendar.Month_Number; + Day : Ada.Calendar.Day_Number; + Hour : Ada.Calendar.Formatting.Hour_Number; + Minute : Ada.Calendar.Formatting.Minute_Number; + Second : Ada.Calendar.Formatting.Second_Number; + Sub_Second : Ada.Calendar.Formatting.Second_Duration; + begin + Ada.Calendar.Formatting.Split + (Value, + Year, Month, Day, + Hour, Minute, Second, + Sub_Second, + Time_Zone); + + return Split_Time' + (Source => Value, + Time_Zone => Time_Zone, + Year => Year, + Month => Month, + Day => Day, + Day_Of_Week => Ada.Calendar.Formatting.Day_Of_Week (Value), + Hour => Hour, + Minute => Minute, + Second => Second, + Sub_Second => Sub_Second); + end Split; + + + procedure Render + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Template : in out Lockable.Descriptor'Class; + Value : in Ada.Calendar.Time) is + begin + Render + (Output, + Template, + Value, + Ada.Calendar.Time_Zones.UTC_Time_Offset (Value)); + end Render; + + + procedure Render + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Template : in out Lockable.Descriptor'Class; + Value : in Ada.Calendar.Time; + Time_Zone : Ada.Calendar.Time_Zones.Time_Offset) is + begin + Render (Output, Template, Split (Value, Time_Zone)); + end Render; + + + procedure Render + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Template : in out Lockable.Descriptor'Class; + Value : in Split_Time) is + begin + Interpreter (Template, Output, Value); + end Render; + +end Natools.S_Expressions.Templates.Dates; + ADDED src/natools-s_expressions-templates-dates.ads Index: src/natools-s_expressions-templates-dates.ads ================================================================== --- src/natools-s_expressions-templates-dates.ads +++ src/natools-s_expressions-templates-dates.ads @@ -0,0 +1,67 @@ +------------------------------------------------------------------------------ +-- Copyright (c) 2014, Natacha Porté -- +-- -- +-- Permission to use, copy, modify, and distribute this software for any -- +-- purpose with or without fee is hereby granted, provided that the above -- +-- copyright notice and this permission notice appear in all copies. -- +-- -- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -- +-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -- +-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -- +-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -- +-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -- +-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -- +-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- +------------------------------------------------------------------------------ + +------------------------------------------------------------------------------ +-- Natools.S_Expressions.Templates.Dates provides a template interpreter -- +-- for dates and times. -- +------------------------------------------------------------------------------ + +with Ada.Calendar.Formatting; +with Ada.Calendar.Time_Zones; +with Ada.Streams; +with Natools.S_Expressions.Lockable; + +package Natools.S_Expressions.Templates.Dates is + + type Split_Time is record + Source : Ada.Calendar.Time; + Time_Zone : Ada.Calendar.Time_Zones.Time_Offset; + Year : Ada.Calendar.Year_Number; + Month : Ada.Calendar.Month_Number; + Day : Ada.Calendar.Day_Number; + Day_Of_Week : Ada.Calendar.Formatting.Day_Name; + Hour : Ada.Calendar.Formatting.Hour_Number; + Minute : Ada.Calendar.Formatting.Minute_Number; + Second : Ada.Calendar.Formatting.Second_Number; + Sub_Second : Ada.Calendar.Formatting.Second_Duration; + end record; + + function Split + (Value : Ada.Calendar.Time; + Time_Zone : Ada.Calendar.Time_Zones.Time_Offset) + return Split_Time; + -- Pre process split time component from Value in Time_Zone + + procedure Render + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Template : in out Lockable.Descriptor'Class; + Value : in Split_Time); + -- Render the given time + + procedure Render + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Template : in out Lockable.Descriptor'Class; + Value : in Ada.Calendar.Time); + -- Render the given time considered in local time zone + + procedure Render + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Template : in out Lockable.Descriptor'Class; + Value : in Ada.Calendar.Time; + Time_Zone : Ada.Calendar.Time_Zones.Time_Offset); + -- Render the given time + +end Natools.S_Expressions.Templates.Dates;