CONSTRAINT_ERROR读取包含"["的文件

Pav*_*ska 1 ada gnat

我正在阅读一个简单的文本文件.一切都按预期工作,除非遇到一个开括号("[")字符.然后我得到一个CONSTRAINT_ERROR.

我的功能是:

----------------------------------------------
-- GET_FILE_CONTENTS
function Get_File_Contents (File_Name : in String)
    return String_Array is
    -- Loads the entire file into a dynamically sized
    -- array of Unbounded_Wide_String.

    -- The line count is used to dynamically size the array.
    Line_Count : Natural
               := 0;

    File : Ada.Wide_Text_IO.File_Type;
begin
    -- Get the line count before opening the file.
    Line_Count := Get_File_Line_Count (File_Name);

    Ada.Wide_Text_IO.Open (File,
                           In_File,
                           File_Name);
    declare
        Lines : String_Array (1 .. Line_Count);
    begin

        -- Step through the file and save each line.
        for Current_Line in reverse 1 .. Line_Count loop
            Lines(Current_Line) := To_Unbounded_Wide_String (Ada.Wide_Text_IO.Get_Line (File));
        end loop;

    -- Remember to close the file.
    Ada.Wide_Text_IO.Close (File);
    return Lines;

end;
end Get_File_Contents;
Run Code Online (Sandbox Code Playgroud)

CONSTRAINT_ERROR在"s-wchcnv.adb:207"中引发.该文件的相关部分是这个

when WCEM_Brackets =>
    if C /= '[' then
       return Character'Pos (C);
    end if;

    if In_Char /= '"' then
       raise Constraint_Error; <======= CONSTRAINT_ERROR
    end if;
Run Code Online (Sandbox Code Playgroud)

这是我发现的信息:

--  Note on the use of brackets encoding (WCEM_Brackets). The brackets
--  encoding method is ambiguous in the context of this function, since
--  there is no way to tell if ["1234"] is eight unencoded characters or
--  one encoded character. In the context of Ada sources, any sequence
--  starting [" must be the start of an encoding (since that sequence is
--  not valid in Ada source otherwise). The routines in this package use
--  the same approach. If the input string contains the sequence [" then
--  this is assumed to be the start of a brackets encoding sequence, and
--  if it does not match the syntax, an error is raised.
Run Code Online (Sandbox Code Playgroud)

如何将其关闭,以便"["不被解释为WCEM?

编辑:

编译器版本为GNAT GPL 2014(20140331)操作系统为Windows 7 x64

重现CONSTRAINT_ERROR的小程序:

with Ada.Wide_Text_IO; use Ada.Wide_Text_IO;

procedure Main is
    File : File_Type;
    File_Name : String := "Data.txt";
begin
    Open (File,
          In_File,
          File_Name);

    while not End_Of_File (File) loop
    declare
        Line : Wide_String := Get_Line (File);
    begin
        Put_Line (Line);
    end;
    end loop;

    Close (File);
end Main;
Run Code Online (Sandbox Code Playgroud)

"Data.txt"文件需要靠近可执行文件.

如果"Data.txt"包含如下文本,没问题.

Hello
abc
Run Code Online (Sandbox Code Playgroud)

添加"["并引发CONSTRAINT_ERROR:

Hello[
abc
Run Code Online (Sandbox Code Playgroud)

Bri*_*ond 6

使用"表单"参数进行打开调用,将字符编码指定为括号"WCEM = b"以外的值.

with Ada.Wide_Text_IO; use Ada.Wide_Text_IO;

procedure Main is
    File : File_Type;
    File_Name : String := "Data.txt";
begin
    Open (File,
          In_File,
          File_Name, 
          Form => "WCEM=8");

    while not End_Of_File (File) loop
    declare
        Line : Wide_String := Get_Line (File);
    begin
        Put_Line (Line);
    end;
    end loop;

    Close (File);
end Main;
Run Code Online (Sandbox Code Playgroud)

有关相关文档,请参见此处

Keith Thompson因找到相关文件而值得赞扬.