File : lin-io.adb


with Ada.Text_IO, Reps.IO;
use Ada.Text_IO, Reps.IO;

pragma Elaborate_All (Ada.Text_IO);

package body Lin.IO is

  package Txt_Int_IO is new Ada.Text_IO.Integer_IO(Integer);
  use Txt_Int_IO;

  procedure ShortPut(F: in File_Type; I: in Integer) is
    K,D: Integer;
  begin
    K := Abs(I);
    if I<0 then D := 2; else D := 1; end if;
    while K>9 loop
      K := K/10;
      D := D+1;
    end loop;
    Put(F,I,D,10);
  end ShortPut;

  procedure Show(W: in Sparse; Cut: in Rep := Zero) is
  begin
    for K in 1 .. Length(W) loop
      if Abs(W(K).R)>Cut then
        Put("W(");
        ShortPut(Current_Output,W(K).I);
        Put(",");
        ShortPut(Current_Output,W(K).J);
        Put(") = ");
        Txt_Put(Current_Output,W(K).R);
        New_Line;
      end if;
    end loop;
    New_Line;
  end Show;

  procedure Read(Name: in String; W: out Sparse; Decimal: in Boolean := True) is
    F: File_Type;
    U: Trip;
  begin
    Put_Line("reading " & Name);
    SetZero(W);
    Open(F,In_File,Name);
    while not End_Of_File(F) loop
      Get(F,U.I);
      Get(F,U.J);
      Txt_Get(F,U.R,Decimal);
      AddComponent(U,W);
    end loop;
    Close(F);
    Put("Length =");
    Put(Current_Output,Length(W),7,10);
    New_Line;
  end Read;

  procedure Write(Name: in String; W: in Sparse; Decimal: in Boolean := True) is
    F: File_Type;
  begin
    Put_Line("writing " & Name);
    Create(F,Out_File,Name);
    for K in 1 .. Length(W) loop
      if W(K).R /= Zero then
        ShortPut(F,W(K).I);
        Put(F," ");
        ShortPut(F,W(K).J);
        if W(K).R<Zero then
          Put(F," ");
        end if;
        Txt_Put(F,W(K).R,Decimal);
        New_Line(F);
      end if;
    end loop;
    Close(F);
  end Write;

end Lin.IO;