how to designate standard input from another file 
Author Message
 how to designate standard input from another file

A certain program reads two files designated by their names on
the command line.  I would like to be able to use the string "-"
to designate standard input in place of a either file name, as is
often done in Unix utilities.

Its pretty obvious how to open a file and designate it as the
default input (Ada.Text_IO.Set_Input), but I don't know how to
read standard input designated by a Text_IO File_Type.

Something like this would be nice except that File_Type is
limited private:

declare
   File : Ada.Text_IO.File_Type;
begin
   if File_Name = "-" then
      File := Ada.Text_IO.Standard_Input;
   else
      Ada.Text_IO.Open (File, In_File, File_Name);
   end if;
end;

Then, I can just read from File rather than testing
on every read whether I'm reading from File or Standard_Input.

This has got to be easy; what am I missing?

--
Terry J. Westley, Software Systems Engineering Supervisor
Veridian Engineering, Calspan Operations

-------------------------------------------------------
Author of TASH, an Ada binding to Tcl/Tk.
Visit the TASH web site at http://www.*-*-*.com/ .
-------------------------------------------------------



Fri, 22 Nov 2002 03:00:00 GMT  
 how to designate standard input from another file


Quote:
> Something like this would be nice except that File_Type is
> limited private:

> declare
>    File : Ada.Text_IO.File_Type;
> begin
>    if File_Name = "-" then
>       File := Ada.Text_IO.Standard_Input;
>    else
>       Ada.Text_IO.Open (File, In_File, File_Name);
>    end if;
> end;

Do it the other way round, i.e. read from Standard_Input, and redirect it if necessary:
 declare
    File : Ada.Text_IO.File_Type;
 begin
    if File_Name /= "-" then
       Ada.Text_IO.Open (File, In_File, File_Name);
       Ada.Text_IO.Set_Input (File);
    end if;
 end;


Fri, 22 Nov 2002 03:00:00 GMT  
 how to designate standard input from another file

Quote:
Jean-Pierre Rosen writes:
> Do it the other way round, i.e. read from Standard_Input, and redirect it if necessary:
>  declare
>     File : Ada.Text_IO.File_Type;
>  begin
>     if File_Name /= "-" then
>        Ada.Text_IO.Open (File, In_File, File_Name);
>        Ada.Text_IO.Set_Input (File);
>     end if;
>  end;

Yes, I know this will work; I've done it.

But, what if there are two files for which you want to
allow the user to specify standard input for either file?
The code above won't work unless you require that only
one file be standard input.  And, if the files have some
sort of precedence (as in diff, compare, or merge),
you've lost that unless you keep flags indicating which
one is default input and which is an opened file.

--
Terry J. Westley, Software Systems Engineering Supervisor
Veridian Engineering, Calspan Operations

-------------------------------------------------------
Author of TASH, an Ada binding to Tcl/Tk.
Visit the TASH web site at http://www.adatcl.com.
-------------------------------------------------------



Fri, 22 Nov 2002 03:00:00 GMT  
 how to designate standard input from another file
One way to get around the limits of limited types is to use access types. I
would try the following:

    declare
        File : Ada.Text_IO.File_Type;
        File_Acc : Ada.Text_IO.File_Access;
    begin
        if File_Name = "-" then
            File_Acc := Ada.Text_IO.Standard_Input;
        else
            Ada.Text_IO.Open (File, In_File, File_Name);
            File_Acc := File'Unchecked_Access;
        end if;

        -- Read from File_Acc.all ...

        -- Clean up
        if Ada.Text_IO.Is_Open (File) then
            Ada.Text_IO.Close (File);
        end if;
    end;



Sat, 23 Nov 2002 03:00:00 GMT  
 how to designate standard input from another file


Quote:
> One way to get around the limits of limited types is to use access types. I
> would try the following:

>     declare
>         File : Ada.Text_IO.File_Type;
>         File_Acc : Ada.Text_IO.File_Access;
>     begin
>         if File_Name = "-" then
>             File_Acc := Ada.Text_IO.Standard_Input;
>         else
>             Ada.Text_IO.Open (File, In_File, File_Name);
>             File_Acc := File'Unchecked_Access;
>         end if;

>         -- Read from File_Acc.all ...

>         -- Clean up
>         if Ada.Text_IO.Is_Open (File) then
>             Ada.Text_IO.Close (File);
>         end if;
>     end;

But make sure you don't close Standard_Input!

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

Visit Adalog's web site at http://pro.wanadoo.fr/adalog



Sat, 23 Nov 2002 03:00:00 GMT  
 how to designate standard input from another file


Quote:




Quote:
> > One way to get around the limits of limited types is to use access
types. I
> > would try the following:

> >     declare
> >         File : Ada.Text_IO.File_Type;
> >         File_Acc : Ada.Text_IO.File_Access;
> >     begin
> >         if File_Name = "-" then
> >             File_Acc := Ada.Text_IO.Standard_Input;
> >         else
> >             Ada.Text_IO.Open (File, In_File, File_Name);
> >             File_Acc := File'Unchecked_Access;
> >         end if;

> >         -- Read from File_Acc.all ...

> >         -- Clean up
> >         if Ada.Text_IO.Is_Open (File) then
> >             Ada.Text_IO.Close (File);
> >         end if;
> >     end;

> But make sure you don't close Standard_Input!

This code segment will NOT close Standard_Input. You were probably confusing
the objects File_Acc and File. If File_Acc points to Standard_Input, then
File_Acc does not point to File and File is never opened. In the case where
Standard_Input is used, the clean up code does not close anything.

- Show quoted text -

Quote:
> --
> ---------------------------------------------------------

> Visit Adalog's web site at http://pro.wanadoo.fr/adalog



Sat, 23 Nov 2002 03:00:00 GMT  
 how to designate standard input from another file

Quote:

> But make sure you don't close Standard_Input!

Closing redirected standard output (and then having an exception
occur, which tried to print on standard output ..) is also a bad
thing.


Sat, 23 Nov 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. processing standard-input in a batch file

2. processing standard-input in a batch file

3. REXX BEAUTIES: delete files entered by standard input

4. using standard input as other than a text file

5. using standard input as a non text file

6. Designating the right file

7. standard input and output question

8. VSE - Redirect Standard input/output

9. Standard Input Stream

10. RFI non-standard input sources

11. Reading Standard Input in binary mode

12. read from standard input ....

 

 
Powered by phpBB® Forum Software