-- main is main file of main project written with Ada.
-- Main is Copyright (C) 2025 Manuel De Girardi ; 
--
--   This program is free software; you can redistribute it and/or modify
--   it under the terms of the GNU General Public License as published by
--   the Free Software Foundation; either version 2 of the License, or
--   (at your option) any later version.
--
--   This program 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.  See the
--   GNU General Public License for more details.
--
--   You should have received a copy of the GNU General Public License
--   along with this program; if not, write to the Free Software
--   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
--
-- Date := 2026-10-08 13:45:24 ; 
-- Version := 0.45.0a ; 
with Ada.Wide_Text_Io;
package body M.Strings is
   package W_Io renames Ada.Wide_Text_Io;
   
   procedure Add_Line (Set : in out Wide_Set;
		       Last : in out Line_Index_Range;
		       Line : in Wide_String) is
   begin
      Last := Set.Line_Last;
      if Last = Set.Lines'Last then
	 Wide_Free (Set.Lines ( 1 ) );
	 for Line in 1..Last - 1 loop
	    Set.Lines(Line) := Set.Lines (Line + 1);
	 end loop;
	 Set.lines(Last) := new Wide_String ' (Line);
      else
	 Set.Lines(Last+1) := new Wide_String ' (Line);
	 Last := Last + 1;
      end if;
      Set.Line_Last := Last;
   end Add_Line;
   
   
   procedure Set_Free(Set : in out Wide_Set) is
   begin
      
      Set.Line_Last := 0;
      for Iter in Set.Lines ' range loop
	 if Set.Lines(Iter) /= null then
	    Wide_Free(Set.Lines(Iter));
	 end if;
      end loop;
      
   end Set_Free;

   
   procedure Add_Line (Set : in out Wide_String_Set;
		       Last : in out Line_Index_Range;
		       Line : in Wide_String) is           
   begin

      if Last = Set.Lines'Last then
	 Wide_Free (Set.Lines ( 1 ) );
	 for Line in 1..Last - 1 loop
	    Set.Lines(Line) := Set.Lines (Line + 1);
	 end loop;
	 Set.lines(Last) := new Wide_String ' (Line);
      else
	 Set.Lines(Last+1) := new Wide_String ' (Line);
	 Last := Last + 1;
      end if;
      Set.Line_Last := Last;
   end Add_Line;
   
   
   procedure Set_Free(Set : in out Wide_String_Set) is
   begin
      for Iter in Set.Lines ' range loop
	 Wide_Free(Set.Lines(Iter));
      end loop;
      Set.Line_Last := 0;
   end Set_Free;
   
   
   procedure Load_File (Set : in out Wide_String_Set; Name : in String; Last : out Line_Index_Range) is
      
      File : W_Io.File_Type;
      
   begin
      W_Io.Open(File, W_Io.In_File, Name, Form => "WCEM=8");
      Last := 0;
      
      while not W_Io.End_Of_File(File) loop

   	 declare
   	    Line : constant Wide_String :=W_Io.Get_Line(File);
   	 begin

   	    Set.Lines(Last+1) := new Wide_String ' (Line);
   	    Last := Last + 1;

   	 end;
      end loop;
      Set.Line_Last := Last;
      W_Io.Close(File);
   end Load_File;
   procedure Save_File(Filename : in string; Lines_Table : in Wide_String_Set) is
   begin
      -- save buffer.
      declare
	 File : W_Io.File_Type;
      begin

	 W_Io.Create(File, W_Io.Out_File, Filename, Form => "WCEM=8");
	 for Line_Iter in 1..Lines_Table.Line_Last loop
	    if Lines_Table.Lines(Line_Iter) /= null then

	       
	       declare
		  
		  Line : constant Wide_String := 
		    Lines_Table.Lines(Line_Iter).all;
		  
	       begin
		  
		  W_Io.Put_Line(File, Line);
		  
	       end;
	       
	    end if;
	 end loop;

	 W_Io.Close(File);

      end;
   end Save_File;

end M.Strings ;