---------------------------------------------------------------
--                                                           
--  RAPID - Rapid Ada Portable Interface Designer     
--                                                           
--  CARGV_HELPERS.ADB 
--  Description : handles translation of Arguments to Ada
--    TASH procedures.
--                       
--  By: Martin Carlisle
--                                         
-- RAPID is free software; you can redistribute it and/or 
-- modify it without restriction.  However, we ask that you
-- please retain the original author information, and clearly  
-- indicate if it has been modified.
--
-- RAPID is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty
-- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
--
---------------------------------------------------------------
with Interfaces.C.Strings;
with Ada.Integer_Text_IO;

package body Cargv_Helpers is
   use type Interfaces.C.Strings.Chars_Ptr;

   function Argument(Argv : Cargv.Chars_Ptr_Ptr;
                 Number : Integer) return String is
      Ptr : Cargv.Chars_Ptr_Ptr := Argv;
   begin -- Argument
      for i in 1..Number loop
         if Ptr.all = Interfaces.C.Strings.Null_Ptr then
            raise Not_Enough_Arguments;
         end if;
         Cargv.Argv_Pointer.Increment(Ptr);
      end loop;
      if Ptr.all = Interfaces.C.Strings.Null_Ptr then
         raise Not_Enough_Arguments;
      end if;
      return Interfaces.C.Strings.Value(Ptr.all);
   end Argument;


   function Argument(Argv : Cargv.Chars_Ptr_Ptr;
                 Number : Integer) return Integer is

      tempvar : Integer;
      last    : Natural;
   begin -- Argument
      declare
         result : String := Argument(Argv,Number);
      begin
         Ada.Integer_Text_IO.Get(From => result,
            Item => tempvar, Last => last);
      end;
      return tempvar;
   exception
      when Not_Enough_Arguments => raise;
      when others => raise Invalid_Argument;
   end Argument;

end Cargv_Helpers;
