The wide character encoding used for Text_IO files is supposed to match the encoding of the source files, as specified by the -gnatW switch, unless it is specified explicitly in the Form string. Instead it incorrectly defaults to brackets encoding. The same applies to Wide_Text_IO and Wide_Wide_Text_IO.
This already worked properly for standard output. This patch fixes the problem for files opened by Text_IO.Create and Text_IO.Open. When compiled without -gnatW, the following test should send Latin-1 text to standard output and to log1, and UTF-8 text to log2. With -gnatW8, it should use UTF-8 for all three. with Ada.Text_IO; use Ada.Text_IO; procedure encode_text_io is file1 : File_Type; file2 : File_Type; begin Put (Character'Val (16#A1#)); Create (file1, Out_File, "log1"); Put (file1, Character'Val (16#A1#)); Create (file2, Out_File, "log2", Form=>"WCEM=8"); Put (file2, Character'Val (16#A1#)); Close (file1); Close (file2); end; UTF-8 encoding is: ¡ Tested on x86_64-pc-linux-gnu, committed on trunk 2017-09-08 Bob Duff <d...@adacore.com> PR ada/80888 * a-textio.adb, a-witeio.adb, a-ztexio.adb (Set_WCEM): Use Default_WCEM by default (i.e. if the encoding is not specified by the Form string).
Index: a-ztexio.adb =================================================================== --- a-ztexio.adb (revision 251863) +++ a-ztexio.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1992-2014, Free Software Foundation, Inc. -- +-- Copyright (C) 1992-2017, Free Software Foundation, Inc. -- -- -- -- GNAT is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- @@ -1626,11 +1626,10 @@ Stop : Natural; begin - File.WC_Method := WCEM_Brackets; FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop); if Start = 0 then - File.WC_Method := WCEM_Brackets; + File.WC_Method := Default_WCEM; else if Stop = Start then Index: a-textio.adb =================================================================== --- a-textio.adb (revision 251863) +++ a-textio.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1992-2016, Free Software Foundation, Inc. -- +-- Copyright (C) 1992-2017, Free Software Foundation, Inc. -- -- -- -- GNAT is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- @@ -1810,11 +1810,10 @@ Stop : Natural; begin - File.WC_Method := WCEM_Brackets; FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop); if Start = 0 then - File.WC_Method := WCEM_Brackets; + File.WC_Method := Default_WCEM; else if Stop = Start then Index: a-witeio.adb =================================================================== --- a-witeio.adb (revision 251863) +++ a-witeio.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1992-2014, Free Software Foundation, Inc. -- +-- Copyright (C) 1992-2017, Free Software Foundation, Inc. -- -- -- -- GNAT is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- @@ -1652,11 +1652,10 @@ Stop : Natural; begin - File.WC_Method := WCEM_Brackets; FIO.Form_Parameter (File.Form.all, "wcem", Start, Stop); if Start = 0 then - File.WC_Method := WCEM_Brackets; + File.WC_Method := Default_WCEM; else if Stop = Start then