---------------------------------------------------------------------- ---------- -- XML/Ada - An XML suite for Ada95 -- -- -- -- Copyright (C) 2001-2012, AdaCore -- -- -- -- This library is free software; you can redistribute it and/or modify it -- -- under terms of the GNU General Public License as published by the Free -- -- Software Foundation; either version 3, 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 MERCHAN- -- -- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. -- -- -- -- -- -- -- -- -- -- -- -- You should have received a copy of the GNU General Public License and -- -- a copy of the GCC Runtime Library Exception along with this program; -- -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- -- . -- -- -- ------------------------------------------------------------------------------ -- This package implements a basic 8bit encoding. -- Only code points from 16#00# to 16#FF# can be encoded in such strings. -- -- However, then can be used to read files that contain accented characters, -- in combination with Unicode.CCS.Iso_8859_1 for instance with Unicode.CES.Utf32; use Unicode.CES.Utf32; with Unicode.CCS; use Unicode.CCS; package body Unicode.CES.Basic_8bit is function Convert (Str : basic_8bit_string; Convert : Unicode.CCS.conversion_function := Identity'access; Order : byte_order := Default_Byte_Order) return basic_8bit_string; -- Internal conversion function ------------ -- Encode -- ------------ procedure Encode (Char : unicode_char; Output : in out byte_sequence; Index : in out Natural) is begin if Char > 16#FF# then raise Invalid_Encoding; end if; Index := Index + 1; Output (Index) := Character'val (Char); end Encode; ---------- -- Read -- ---------- procedure Read (Str : basic_8bit_string; Index : in out Positive; Char : out unicode_char) is begin Char := Character'pos (Str (Index)); Index := Index + 1; end Read; ----------- -- Width -- ----------- function Width (Char : unicode_char) return Natural is pragma warnings (Off, Char); begin return 1; end Width; ------------ -- Length -- ------------ function Length (Str : basic_8bit_string) return Natural is begin return Str'length; end Length; ---------------- -- From_Utf32 -- ---------------- function From_Utf32 (Str : Unicode.CES.Utf32.utf32_le_string) return basic_8bit_string is Result : basic_8bit_string (1 .. Str'length / utf32_char_width); R_Index : Natural := Result'first - 1; C : unicode_char; J : Positive := Str'first; begin while J <= Str'last loop Unicode.CES.Utf32.Read (Str, J, Char => C); Encode (C, Result, R_Index); end loop; return Result; end From_Utf32; -------------- -- To_Utf32 -- -------------- function To_Utf32 (Str : basic_8bit_string) return Unicode.CES.Utf32.utf32_le_string is Result : utf32_le_string (1 .. Str'length * utf32_char_width); R_Index : Natural := Result'first - 1; J : Positive := Str'first; C : unicode_char; begin while J <= Str'last loop Read (Str, J, C); Unicode.CES.Utf32.Encode (C, Result, R_Index); end loop; return Result; end To_Utf32; ------------- -- Convert -- ------------- function Convert (Str : basic_8bit_string; Convert : Unicode.CCS.conversion_function := Identity'access; Order : byte_order := Default_Byte_Order) return basic_8bit_string is pragma warnings (Off, Order); S : String (Str'range); C : unicode_char; J : Positive := Str'first; J_Out : Natural := S'first - 1; begin if Convert = Identity'access then return Str; else while J <= Str'last loop Read (Str, J, C); Encode (Convert (C), S, J_Out); end loop; return S; end if; end Convert; ------------------- -- To_Unicode_LE -- ------------------- function To_Unicode_LE (Str : basic_8bit_string; Cs : Unicode.CCS.character_set := Unicode.CCS.Unicode_Character_Set; Order : byte_order := Default_Byte_Order) return basic_8bit_string is begin return Convert (Str, Cs.To_Unicode, Order); end To_Unicode_LE; ----------- -- To_CS -- ----------- function To_CS (Str : basic_8bit_string; Cs : Unicode.CCS.character_set := Unicode.CCS.Unicode_Character_Set; Order : byte_order := Default_Byte_Order) return basic_8bit_string is begin return Convert (Str, Cs.To_CS, Order); end To_CS; end Unicode.CES.Basic_8bit;