Search code examples
replaceadahtml-entitieslatin1

HTML Entity Converter in Ada


I want to write an Ada program which replaces Latin1 characters with applicable HTML entities, but my code does not work: text.txt and converted.txt are always the same. My tutor said that code is correct. Thanks in advance!

Here is my code:

with Ada.Text_IO;
procedure Entity_Converter is
   use Ada.Text_IO;

   Source : File_Type;
   Target : File_Type;
   Source_Char : Character;
begin
   Open (Source, In_File, "test.txt");
   Create (Target, Out_File, "converted.txt");
   while not End_Of_File (Source) loop
      Get (Source, Source_Char);
      case Source_Char is
         when 'ä' =>
            Put (Target, "ä");
         when 'Ä' =>
            Put (Target, "Ä");
         when 'ö' =>
            Put (Target, "ö");
         when 'Ö' =>
            Put (Target, "Ö");
         when 'ü' =>
            Put (Target, "ü");
         when 'Ü' =>
            Put (Target, "Ü");
         when 'ß' =>
            Put (Target, "ß");
         when others =>
            Put (Target, Source_Char);
      end case;
   end loop;
   Close (Source);
   Close (Target);
end Entity_Converter;

Solution

  • The result depends on the encoding of both the source text, as well as the test file.

    To address the former, use the constants of the package Ada.Characters.Latin_1:

    with Ada.Characters.Latin_1;
    use Ada.Characters.Latin_1;
    ...
       case Source_Char is
          when LC_A_Diaeresis =>
             Put (Target, "ä");
          when UC_A_Diaeresis =>
             Put (Target, "Ä");
          ...
          when LC_German_Sharp_S =>
             Put (Target, "ß");
          when others =>
             Put (Target, Source_Char);
       end case;
    

    The latter depends on your editor.