diff -Naur libxmlada,1/input_sources/input_sources-file.adb libxmlada,2/input_sources/input_sources-file.adb --- libxmlada,1/input_sources/input_sources-file.adb Mon Jun 10 13:57:34 2002 +++ libxmlada,2/input_sources/input_sources-file.adb Thu Jan 1 05:00:00 1970 @@ -1,138 +0,0 @@ ------------------------------------------------------------------------ --- XML/Ada - An XML suite for Ada95 -- --- -- --- Copyright (C) 2001-2002 -- --- ACT-Europe -- --- -- --- 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. -- --- -- --- As a special exception, if other files instantiate generics from -- --- this unit, or you link this unit with other files to produce an -- --- executable, this unit does not by itself cause the resulting -- --- executable to be covered by the GNU General Public License. This -- --- exception does not however invalidate any other reasons why the -- --- executable file might be covered by the GNU Public License. -- ------------------------------------------------------------------------ - -with Ada.Direct_IO; -with Unicode.CES; use Unicode.CES; -with Unicode.CES.Utf32; use Unicode.CES.Utf32; -with Unicode.CES.Utf16; use Unicode.CES.Utf16; -with Unicode.CES.Utf8; use Unicode.CES.Utf8; - -package body Input_Sources.File is - - procedure Fast_Read (The_File : in String; Buf : in Byte_Sequence_Access); - -- Read Buf'length characters in The_File and store it in Buf. - -- This procedure performs a single call to Read. - - --------------- - -- Fast_Read -- - --------------- - - procedure Fast_Read (The_File : in String; - Buf : in Byte_Sequence_Access) is - type Fixed_String is new String (Buf'Range); - - package Dir_Fast is new Ada.Direct_IO (Fixed_String); - use Dir_Fast; - - F : Dir_Fast.File_Type; - - begin - Dir_Fast.Open (F, In_File, The_File); - Dir_Fast.Read (F, Fixed_String (Buf.all)); - Dir_Fast.Close (F); - end Fast_Read; - - ---------- - -- Open -- - ---------- - - procedure Open (Filename : String; Input : out File_Input) is - package Dir is new Ada.Direct_IO (Character); - F : Dir.File_Type; - Length : Natural; - BOM : Bom_Type; - begin - Dir.Open (F, Dir.In_File, Filename); - Length := Natural (Dir.Size (F)); - Dir.Close (F); - - -- If the file is empty, we just create a reader that will not return - -- any character. This will fail later on when the XML document is - -- parsed, anyway. - if Length = 0 then - Input.Buffer := new String (1 .. 1); - Input.Index := 2; - return; - end if; - - Input.Buffer := new String (1 .. Length); - Fast_Read (Filename, Input.Buffer); - - Read_Bom (Input.Buffer.all, Input.Prolog_Size, BOM); - case BOM is - when Utf32_LE => - Set_Encoding (Input, Utf32_LE_Encoding); - when Utf32_BE => - Set_Encoding (Input, Utf32_BE_Encoding); - when Utf16_LE => - Set_Encoding (Input, Utf16_LE_Encoding); - when Utf16_BE => - Set_Encoding (Input, Utf16_BE_Encoding); - when Ucs4_BE | Ucs4_LE | Ucs4_2143 | Ucs4_3412 => - raise Invalid_Encoding; - when Utf8_All | Unknown => - Set_Encoding (Input, Utf8_Encoding); - end case; - - Input.Index := Input.Buffer'First + Input.Prolog_Size; - end Open; - - ----------- - -- Close -- - ----------- - - procedure Close (Input : in out File_Input) is - begin - Input_Sources.Close (Input_Source (Input)); - Free (Input.Buffer); - Input.Index := Natural'Last; - end Close; - - --------------- - -- Next_Char -- - --------------- - - procedure Next_Char - (From : in out File_Input; - C : out Unicode.Unicode_Char) is - begin - From.Es.Read (From.Buffer.all, From.Index, C); - C := From.Cs.To_Unicode (C); - end Next_Char; - - --------- - -- Eof -- - --------- - - function Eof (From : File_Input) return Boolean is - begin - return From.Index > From.Buffer'Length; - end Eof; - -end Input_Sources.File; diff -Naur libxmlada,1/input_sources/input_sources-file.ads libxmlada,2/input_sources/input_sources-file.ads --- libxmlada,1/input_sources/input_sources-file.ads Tue Jan 8 16:15:15 2002 +++ libxmlada,2/input_sources/input_sources-file.ads Thu Jul 28 17:35:51 2005 @@ -1,65 +1,5 @@ ------------------------------------------------------------------------ --- XML/Ada - An XML suite for Ada95 -- --- -- --- Copyright (C) 2001-2002 -- --- ACT-Europe -- --- -- --- 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. -- --- -- --- As a special exception, if other files instantiate generics from -- --- this unit, or you link this unit with other files to produce an -- --- executable, this unit does not by itself cause the resulting -- --- executable to be covered by the GNU General Public License. This -- --- exception does not however invalidate any other reasons why the -- --- executable file might be covered by the GNU Public License. -- ------------------------------------------------------------------------ +with Input_Sources.File_Impl.Stream; +-- with Input_Sources.File_Impl.Direct; -with Unicode; -with Unicode.CES; - -package Input_Sources.File is - - type File_Input is new Input_Source with private; - type File_Input_Access is access all File_Input'Class; - -- A special implementation of a reader, that reads from a file. - - procedure Open (Filename : String; Input : out File_Input); - -- Open a new file for reading. - -- Note that the file is read completly at once, and saved in memory. - -- This provides a much better access later on, however this might be a - -- problem for very big files. - -- The physical file on disk can be modified at any time afterwards, since - -- it is no longer read. - -- This function can decode a file if it is coded in Utf8, Utf16 or Utf32 - - procedure Close (Input : in out File_Input); - -- Close the file and free the memory - - procedure Next_Char - (From : in out File_Input; - C : out Unicode.Unicode_Char); - -- Return the next character in the file. - - function Eof (From : File_Input) return Boolean; - -- True if From is past the last character in the file. - -private - type File_Input is new Input_Source with - record - Index : Natural; - Buffer : Unicode.CES.Byte_Sequence_Access; - end record; -end Input_Sources.File; +package Input_Sources.File renames Input_Sources.File_Impl.Stream; +--package Input_Sources.File renames Input_Sources.File_Impl.Direct; diff -Naur libxmlada,1/input_sources/input_sources-file_impl-direct.adb libxmlada,2/input_sources/input_sources-file_impl-direct.adb --- libxmlada,1/input_sources/input_sources-file_impl-direct.adb Thu Jan 1 05:00:00 1970 +++ libxmlada,2/input_sources/input_sources-file_impl-direct.adb Thu Jul 28 17:52:00 2005 @@ -0,0 +1,138 @@ +----------------------------------------------------------------------- +-- XML/Ada - An XML suite for Ada95 -- +-- -- +-- Copyright (C) 2001-2002 -- +-- ACT-Europe -- +-- -- +-- 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. -- +-- -- +-- As a special exception, if other files instantiate generics from -- +-- this unit, or you link this unit with other files to produce an -- +-- executable, this unit does not by itself cause the resulting -- +-- executable to be covered by the GNU General Public License. This -- +-- exception does not however invalidate any other reasons why the -- +-- executable file might be covered by the GNU Public License. -- +----------------------------------------------------------------------- + +with Ada.Direct_IO; +with Unicode.CES; use Unicode.CES; +with Unicode.CES.Utf32; use Unicode.CES.Utf32; +with Unicode.CES.Utf16; use Unicode.CES.Utf16; +with Unicode.CES.Utf8; use Unicode.CES.Utf8; + +package body Input_Sources.File_Impl.Direct is + + procedure Fast_Read (The_File : in String; Buf : in Byte_Sequence_Access); + -- Read Buf'length characters in The_File and store it in Buf. + -- This procedure performs a single call to Read. + + --------------- + -- Fast_Read -- + --------------- + + procedure Fast_Read (The_File : in String; + Buf : in Byte_Sequence_Access) is + type Fixed_String is new String (Buf'Range); + + package Dir_Fast is new Ada.Direct_IO (Fixed_String); + use Dir_Fast; + + F : Dir_Fast.File_Type; + + begin + Dir_Fast.Open (F, In_File, The_File); + Dir_Fast.Read (F, Fixed_String (Buf.all)); + Dir_Fast.Close (F); + end Fast_Read; + + ---------- + -- Open -- + ---------- + + procedure Open (Filename : String; Input : out File_Input) is + package Dir is new Ada.Direct_IO (Character); + F : Dir.File_Type; + Length : Natural; + BOM : Bom_Type; + begin + Dir.Open (F, Dir.In_File, Filename); + Length := Natural (Dir.Size (F)); + Dir.Close (F); + + -- If the file is empty, we just create a reader that will not return + -- any character. This will fail later on when the XML document is + -- parsed, anyway. + if Length = 0 then + Input.Buffer := new String (1 .. 1); + Input.Index := 2; + return; + end if; + + Input.Buffer := new String (1 .. Length); + Fast_Read (Filename, Input.Buffer); + + Read_Bom (Input.Buffer.all, Input.Prolog_Size, BOM); + case BOM is + when Utf32_LE => + Set_Encoding (Input, Utf32_LE_Encoding); + when Utf32_BE => + Set_Encoding (Input, Utf32_BE_Encoding); + when Utf16_LE => + Set_Encoding (Input, Utf16_LE_Encoding); + when Utf16_BE => + Set_Encoding (Input, Utf16_BE_Encoding); + when Ucs4_BE | Ucs4_LE | Ucs4_2143 | Ucs4_3412 => + raise Invalid_Encoding; + when Utf8_All | Unknown => + Set_Encoding (Input, Utf8_Encoding); + end case; + + Input.Index := Input.Buffer'First + Input.Prolog_Size; + end Open; + + ----------- + -- Close -- + ----------- + + procedure Close (Input : in out File_Input) is + begin + Input_Sources.Close (Input_Source (Input)); + Free (Input.Buffer); + Input.Index := Natural'Last; + end Close; + + --------------- + -- Next_Char -- + --------------- + + procedure Next_Char + (From : in out File_Input; + C : out Unicode.Unicode_Char) is + begin + From.Es.Read (From.Buffer.all, From.Index, C); + C := From.Cs.To_Unicode (C); + end Next_Char; + + --------- + -- Eof -- + --------- + + function Eof (From : File_Input) return Boolean is + begin + return From.Index > From.Buffer'Length; + end Eof; + +end Input_Sources.File_Impl.Direct; diff -Naur libxmlada,1/input_sources/input_sources-file_impl-direct.ads libxmlada,2/input_sources/input_sources-file_impl-direct.ads --- libxmlada,1/input_sources/input_sources-file_impl-direct.ads Thu Jan 1 05:00:00 1970 +++ libxmlada,2/input_sources/input_sources-file_impl-direct.ads Thu Jul 28 17:31:47 2005 @@ -0,0 +1,65 @@ +----------------------------------------------------------------------- +-- XML/Ada - An XML suite for Ada95 -- +-- -- +-- Copyright (C) 2001-2002 -- +-- ACT-Europe -- +-- -- +-- 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. -- +-- -- +-- As a special exception, if other files instantiate generics from -- +-- this unit, or you link this unit with other files to produce an -- +-- executable, this unit does not by itself cause the resulting -- +-- executable to be covered by the GNU General Public License. This -- +-- exception does not however invalidate any other reasons why the -- +-- executable file might be covered by the GNU Public License. -- +----------------------------------------------------------------------- + +with Unicode; +with Unicode.CES; + +package Input_Sources.File_Impl.Direct is + + type File_Input is new Input_Source with private; + type File_Input_Access is access all File_Input'Class; + -- A special implementation of a reader, that reads from a file. + + procedure Open (Filename : String; Input : out File_Input); + -- Open a new file for reading. + -- Note that the file is read completly at once, and saved in memory. + -- This provides a much better access later on, however this might be a + -- problem for very big files. + -- The physical file on disk can be modified at any time afterwards, since + -- it is no longer read. + -- This function can decode a file if it is coded in Utf8, Utf16 or Utf32 + + procedure Close (Input : in out File_Input); + -- Close the file and free the memory + + procedure Next_Char + (From : in out File_Input; + C : out Unicode.Unicode_Char); + -- Return the next character in the file. + + function Eof (From : File_Input) return Boolean; + -- True if From is past the last character in the file. + +private + type File_Input is new Input_Source with + record + Index : Natural; + Buffer : Unicode.CES.Byte_Sequence_Access; + end record; +end Input_Sources.File_Impl.Direct; diff -Naur libxmlada,1/input_sources/input_sources-file_impl-stream.adb libxmlada,2/input_sources/input_sources-file_impl-stream.adb --- libxmlada,1/input_sources/input_sources-file_impl-stream.adb Thu Jan 1 05:00:00 1970 +++ libxmlada,2/input_sources/input_sources-file_impl-stream.adb Thu Jul 28 18:19:09 2005 @@ -0,0 +1,156 @@ +----------------------------------------------------------------------- +-- XML/Ada - An XML suite for Ada95 -- +-- -- +-- Copyright (C) 2001-2002 -- +-- ACT-Europe -- +-- -- +-- 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. -- +-- -- +-- As a special exception, if other files instantiate generics from -- +-- this unit, or you link this unit with other files to produce an -- +-- executable, this unit does not by itself cause the resulting -- +-- executable to be covered by the GNU General Public License. This -- +-- exception does not however invalidate any other reasons why the -- +-- executable file might be covered by the GNU Public License. -- +----------------------------------------------------------------------- + +with Ada.Streams; +with Ada.Unchecked_Conversion; +with Unicode.CES; use Unicode.CES; +with Unicode.CES.Utf32; use Unicode.CES.Utf32; +with Unicode.CES.Utf16; use Unicode.CES.Utf16; +with Unicode.CES.Utf8; use Unicode.CES.Utf8; + +package body Input_Sources.File_Impl.Stream is + + use Ada.Streams; + use Ada.Streams.Stream_IO; + + procedure Do_Read (Input : in out File_Input); + + ------------- + -- Do_Read -- + ------------- + + procedure Do_Read (Input : in out File_Input) is + Read_Size : Positive_Count := 32768; + -- Read multiplying the read chunk two times each iteration. + begin + Read_Loop: + loop + declare + Arr : Stream_Element_Array (1 .. Stream_Element_Offset (Read_Size)); + Last : Stream_Element_Offset; + begin + Read (Input.F, Arr, Last); + exit Read_Loop when Last < Arr'First; -- Ensure no empty array conversion + declare + subtype Arr_Type is Stream_Element_Array (1 .. Last ); + subtype Seq_Type is Byte_Sequence (1 .. Integer (Last)); + Old_Buffer : Byte_Sequence_Access := Input.Buffer; + function To_BS is + new Ada.Unchecked_Conversion (Source => Arr_Type, Target => Seq_Type); + begin + if Old_Buffer = null then + Input.Buffer := new Byte_Sequence'(To_BS (Arr)); + else + Input.Buffer := new Byte_Sequence'(Old_Buffer.all & To_BS (Arr)); + Free (Old_Buffer); + end if; + end; + exit Read_Loop when Last < Arr'Last; + end; + + -- FIXME: Need also check that we don't go beyound Integer'Last (for Buffer indices). + if Read_Size <= Count'Last / 2 then + Read_Size := Read_Size * 2; + else + Read_Size := Count'Last; + end if; + end loop Read_Loop; + end Do_Read; + + ---------- + -- Open -- + ---------- + + procedure Open (Filename : String; Input : out File_Input) is + BOM : Bom_Type; + begin + Open (Input.F, In_File, Filename); + Do_Read (Input); + + -- If the file is empty, we just create a reader that will not return + -- any character. This will fail later on when the XML document is + -- parsed, anyway. + if Input.Buffer = null then + Input.Buffer := new String (1 .. 1); + Input.Index := 2; + return; + end if; + + Read_Bom (Input.Buffer.all, Input.Prolog_Size, BOM); + case BOM is + when Utf32_LE => + Set_Encoding (Input, Utf32_LE_Encoding); + when Utf32_BE => + Set_Encoding (Input, Utf32_BE_Encoding); + when Utf16_LE => + Set_Encoding (Input, Utf16_LE_Encoding); + when Utf16_BE => + Set_Encoding (Input, Utf16_BE_Encoding); + when Ucs4_BE | Ucs4_LE | Ucs4_2143 | Ucs4_3412 => + raise Invalid_Encoding; + when Utf8_All | Unknown => + Set_Encoding (Input, Utf8_Encoding); + end case; + + Input.Index := Input.Buffer'First + Input.Prolog_Size; + end Open; + + ----------- + -- Close -- + ----------- + + procedure Close (Input : in out File_Input) is + begin + Input_Sources.Close (Input_Source (Input)); + Free (Input.Buffer); + Input.Index := Natural'Last; + end Close; + + --------------- + -- Next_Char -- + --------------- + + procedure Next_Char + (From : in out File_Input; + C : out Unicode.Unicode_Char) is + begin + From.Es.Read (From.Buffer.all, From.Index, C); + C := From.Cs.To_Unicode (C); + end Next_Char; + + --------- + -- Eof -- + --------- + + function Eof (From : File_Input) return Boolean is + begin + return From.Index > From.Buffer'Length; + end Eof; + +end Input_Sources.File_Impl.Stream; diff -Naur libxmlada,1/input_sources/input_sources-file_impl-stream.ads libxmlada,2/input_sources/input_sources-file_impl-stream.ads --- libxmlada,1/input_sources/input_sources-file_impl-stream.ads Thu Jan 1 05:00:00 1970 +++ libxmlada,2/input_sources/input_sources-file_impl-stream.ads Thu Jul 28 17:36:34 2005 @@ -0,0 +1,67 @@ +----------------------------------------------------------------------- +-- XML/Ada - An XML suite for Ada95 -- +-- -- +-- Copyright (C) 2001-2002 -- +-- ACT-Europe -- +-- -- +-- 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. -- +-- -- +-- As a special exception, if other files instantiate generics from -- +-- this unit, or you link this unit with other files to produce an -- +-- executable, this unit does not by itself cause the resulting -- +-- executable to be covered by the GNU General Public License. This -- +-- exception does not however invalidate any other reasons why the -- +-- executable file might be covered by the GNU Public License. -- +----------------------------------------------------------------------- + +with Unicode; +with Unicode.CES; +with Ada.Streams.Stream_IO; + +package Input_Sources.File_Impl.Stream is + + type File_Input is new Input_Source with private; + type File_Input_Access is access all File_Input'Class; + -- A special implementation of a reader, that reads from a file. + + procedure Open (Filename : String; Input : out File_Input); + -- Open a new file for reading. + -- Note that the file is read completly at once, and saved in memory. + -- This provides a much better access later on, however this might be a + -- problem for very big files. + -- The physical file on disk can be modified at any time afterwards, since + -- it is no longer read. + -- This function can decode a file if it is coded in Utf8, Utf16 or Utf32 + + procedure Close (Input : in out File_Input); + -- Close the file and free the memory + + procedure Next_Char + (From : in out File_Input; + C : out Unicode.Unicode_Char); + -- Return the next character in the file. + + function Eof (From : File_Input) return Boolean; + -- True if From is past the last character in the file. + +private + type File_Input is new Input_Source with + record + Index : Natural; + Buffer : Unicode.CES.Byte_Sequence_Access; + F : Ada.Streams.Stream_IO.File_Type; + end record; +end Input_Sources.File_Impl.Stream; diff -Naur libxmlada,1/input_sources/input_sources-file_impl.ads libxmlada,2/input_sources/input_sources-file_impl.ads --- libxmlada,1/input_sources/input_sources-file_impl.ads Thu Jan 1 05:00:00 1970 +++ libxmlada,2/input_sources/input_sources-file_impl.ads Thu Jul 28 17:34:17 2005 @@ -0,0 +1,2 @@ +package Input_Sources.File_Impl is +end Input_Sources.File_Impl;