1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- GNAT COMPILER COMPONENTS --
|
4 |
|
|
-- --
|
5 |
|
|
-- G N A T . B Y T E _ O R D E R _ M A R K --
|
6 |
|
|
-- --
|
7 |
|
|
-- S p e c --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 2006-2008, AdaCore --
|
10 |
|
|
-- --
|
11 |
|
|
-- GNAT is free software; you can redistribute it and/or modify it under --
|
12 |
|
|
-- terms of the GNU General Public License as published by the Free Soft- --
|
13 |
|
|
-- ware Foundation; either version 2, or (at your option) any later ver- --
|
14 |
|
|
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
|
15 |
|
|
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
|
16 |
|
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
|
17 |
|
|
-- for more details. You should have received a copy of the GNU General --
|
18 |
|
|
-- Public License distributed with GNAT; see file COPYING. If not, write --
|
19 |
|
|
-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
|
20 |
|
|
-- Boston, MA 02110-1301, USA. --
|
21 |
|
|
-- --
|
22 |
|
|
-- As a special exception, if other files instantiate generics from this --
|
23 |
|
|
-- unit, or you link this unit with other files to produce an executable, --
|
24 |
|
|
-- this unit does not by itself cause the resulting executable to be --
|
25 |
|
|
-- covered by the GNU General Public License. This exception does not --
|
26 |
|
|
-- however invalidate any other reasons why the executable file might be --
|
27 |
|
|
-- covered by the GNU Public License. --
|
28 |
|
|
-- --
|
29 |
|
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
30 |
|
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
31 |
|
|
-- --
|
32 |
|
|
------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
-- This package provides a procedure for reading and interpreting the BOM
|
35 |
|
|
-- (byte order mark) used to publish the encoding method for a string (for
|
36 |
|
|
-- example, a UTF-8 encoded file in windows will start with the appropriate
|
37 |
|
|
-- BOM sequence to signal UTF-8 encoding.
|
38 |
|
|
|
39 |
|
|
-- There are two cases
|
40 |
|
|
|
41 |
|
|
-- Case 1. UTF encodings for Unicode files
|
42 |
|
|
|
43 |
|
|
-- Here the convention is to have the first character of the file be a
|
44 |
|
|
-- non-breaking zero width space character (16#0000_FEFF#). For the UTF
|
45 |
|
|
-- encodings, the representation of this character can be used to uniquely
|
46 |
|
|
-- determine the encoding. Furthermore, the possibility of any confusion
|
47 |
|
|
-- with unencoded files is minimal, since for example the UTF-8 encoding
|
48 |
|
|
-- of this character looks like the sequence:
|
49 |
|
|
|
50 |
|
|
-- LC_I_Diaeresis
|
51 |
|
|
-- Right_Angle_Quotation
|
52 |
|
|
-- Fraction_One_Half
|
53 |
|
|
|
54 |
|
|
-- which is so unlikely to occur legitimately in normal use that it can
|
55 |
|
|
-- safely be ignored in most cases (for example, no legitimate Ada source
|
56 |
|
|
-- file could start with this sequence of characters).
|
57 |
|
|
|
58 |
|
|
-- Case 2. Specialized XML encodings
|
59 |
|
|
|
60 |
|
|
-- The XML standard defines a number of other possible encodings and also
|
61 |
|
|
-- defines standardized sequences for marking these encodings. This package
|
62 |
|
|
-- can also optionally handle these XML defined BOM sequences. These XML
|
63 |
|
|
-- cases depend on the first character of the XML file being < so that the
|
64 |
|
|
-- encoding of this character can be recognized.
|
65 |
|
|
|
66 |
|
|
pragma Compiler_Unit;
|
67 |
|
|
|
68 |
|
|
package GNAT.Byte_Order_Mark is
|
69 |
|
|
|
70 |
|
|
type BOM_Kind is
|
71 |
|
|
(UTF8_All, -- UTF8-encoding
|
72 |
|
|
UTF16_LE, -- UTF16 little-endian encoding
|
73 |
|
|
UTF16_BE, -- UTF16 big-endian encoding
|
74 |
|
|
UTF32_LE, -- UTF32 little-endian encoding
|
75 |
|
|
UTF32_BE, -- UTF32 big-endian encoding
|
76 |
|
|
|
77 |
|
|
-- The following cases are for XML only
|
78 |
|
|
|
79 |
|
|
UCS4_BE, -- UCS-4, big endian machine (1234 order)
|
80 |
|
|
UCS4_LE, -- UCS-4, little endian machine (4321 order)
|
81 |
|
|
UCS4_2143, -- UCS-4, unusual byte order (2143 order)
|
82 |
|
|
UCS4_3412, -- UCS-4, unusual byte order (3412 order)
|
83 |
|
|
|
84 |
|
|
-- Value returned if no BOM recognized
|
85 |
|
|
|
86 |
|
|
Unknown); -- Unknown, assumed to be ASCII compatible
|
87 |
|
|
|
88 |
|
|
procedure Read_BOM
|
89 |
|
|
(Str : String;
|
90 |
|
|
Len : out Natural;
|
91 |
|
|
BOM : out BOM_Kind;
|
92 |
|
|
XML_Support : Boolean := False);
|
93 |
|
|
-- This is the routine to read the BOM from the start of the given string
|
94 |
|
|
-- Str. On return BOM is set to the appropriate BOM_Kind and Len is set to
|
95 |
|
|
-- its length. The caller will typically skip the first Len characters in
|
96 |
|
|
-- the string to ignore the BOM sequence. The special XML possibilities are
|
97 |
|
|
-- recognized only if flag XML_Support is set to True. Note that for the
|
98 |
|
|
-- XML cases, Len is always set to zero on return (not to the length of the
|
99 |
|
|
-- relevant sequence) since in the XML cases, the sequence recognized is
|
100 |
|
|
-- for the first real character in the file (<) which is not to be skipped.
|
101 |
|
|
|
102 |
|
|
end GNAT.Byte_Order_Mark;
|