cgi-conversions.adb


------------------------------------------------------------------------------
--
--  package CGI.Conversions (body)
--
--  This package contains routines to translate strings between plain text and
--  CGI argument encoding.
--
------------------------------------------------------------------------------
--  Update information:
--
--  1996.05.17 (Jacob Sparre Andersen)
--    Written.
--    Child of David A. Wheeler's CGI package.
--
--  1996.05.20 (Jacob Sparre Andersen)
--    Corrected bugs concerning '+' and ' '.
--
--  (Insert additional update information above this line.)
------------------------------------------------------------------------------

with Ada.Characters.Handling;
with Ada.Strings.Unbounded;
with Ada.Text_IO;

package body CGI.Conversions is

   ---------------------------------------------------------------------------
   --  function To_Argument:

   function To_Argument (Source : in String) return String is

      use Ada.Characters.Handling;
      use Ada.Strings.Unbounded;

      package Int_IO is new Ada.Text_IO.Integer_IO (Num => Integer);
      use Int_IO;

      Buffer : Unbounded_String;

      Hex_Image : String (1 .. 6) := "000000";

   begin -- To_Argument
      for Index in Source'Range loop
         if Is_Letter (Source (Index)) then
            Append (Buffer, Source (Index .. Index));
         elsif Source (Index) = ' ' then
            Append (Buffer, "+");
         else
            Put (Item => Character'Pos (Source (Index)),
                 To   => Hex_Image,
                 Base => 16);

            if Hex_Image (1 .. 4) = " 16#" and then Hex_Image (6) = '#' then
              Append (Buffer, "%0" & Hex_Image (5));
            elsif Hex_Image (1 .. 3) = "16#" and then Hex_Image (6) = '#' then
              Append (Buffer, "%" & Hex_Image (4 .. 5));
            else
              Ada.Text_IO.Put_Line (Ada.Text_IO.Current_Error,
                                    "CGI.Conversions.To_Argument: What!!! " &
                                      Hex_Image);
            end if;
         end if;
      end loop;

      return To_String (Buffer);
   end To_Argument;

   ---------------------------------------------------------------------------
   --  function To_String:

   function To_String (Argument : in String) return String is

      use Ada.Strings.Unbounded;

      package Int_IO is new Ada.Text_IO.Integer_IO (Num => Integer);
      use Int_IO;

      Result : Unbounded_String;
      Index  : Natural := Argument'First - 1;

      Number    : Integer;
      Last_Char : Positive;

   begin --  To_String
      while Index < Argument'Last loop
         Index := Index + 1;

         if Argument (Index) = '%' then
            Get (Item => Number,
                 From => "16#" & Argument (Index + 1 .. Index + 2) & "#",
                 Last => Last_Char);
            Append (Result, (1 => Character'Val (Number)));
            Index := Index + 2;
         elsif Argument (Index) = '%' then
            Append (Result, " ");
         else
            Append (Result, Argument (Index .. Index));
         end if;
      end loop;

      return To_String (Result);
   end To_String;

   ---------------------------------------------------------------------------

end CGI.Conversions;

Typeset with Ada_To_HTML (Jacob and Jesper)