with -- Standart Ada units
  Ada.Characters.Handling,
  Ada.Text_IO;
package body Implementation_Options is

   Fixed_Parameters_String : constant Wide_String := "-CA -FM";

   -------------------------------------------------------------
   -- Internal Elements                                       --
   -------------------------------------------------------------

   -- This functions constructs a list of -I<name> options from
   -- the src_file indications in a Gnat project file

   function I_Options_From_Project (Project_File : String) return String is
      use Ada.Text_IO;
      F : File_Type;
      Key : constant String := "src_dir=";
      function Get_Next_Src return String is
         Buf  : String (1..500);
         Last : Natural;
      begin
         loop  -- Exit on End_Error
            Get_Line (F, Buf, Last);
            if Last > Key'Length and then
              Buf (1..Key'Length) = Key
            then
               return "-I" & Buf(Key'Length+1..Last) & ' ' & Get_Next_Src;
            end if;
         end loop;
      exception
         -- It is better to catch End_Error than to check End_Of_File
         -- in the case of malformed input files
         when End_Error =>
            return "";
      end Get_Next_Src;

   begin    -- I_Options_From_Project
      if Project_File = "" then
         -- No project file
         return "";
      end if;

      Open (F, In_File, Project_File);
      declare
         Result : String := Get_Next_Src;
      begin
         Close (F);
         return Result;
      end;
   exception
      when others =>
         if Is_Open (F) then
            Close (F);
         end if;
         raise;
   end I_Options_From_Project;

   -------------------------------------------------------------
   -- Exported Elements                                       --
   -------------------------------------------------------------

   -----------------------
   -- Parameters_String --
   -----------------------

   function Parameters_String (Project_File : String := "") return Wide_String is
      use Ada.Characters.Handling;
   begin
      return
        Fixed_Parameters_String &
        ' ' &
        To_Wide_String (I_Options_From_Project (Project_File));
   end Parameters_String;

end Implementation_Options;

