Node: Interfacing to C Streams, Previous: Operations on C Streams, Up: The Implementation of Standard I/O



Interfacing to C Streams

The packages in this section permit interfacing Ada files to C Stream operations.

      with Interfaces.C_Streams;
      package Ada.Sequential_IO.C_Streams is
         function C_Stream (F : File_Type)
            return Interfaces.C_Streams.FILEs;
         procedure Open
           (File : in out File_Type;
            Mode : in File_Mode;
            C_Stream : in Interfaces.C_Streams.FILEs;
            Form : in String := "");
      end Ada.Sequential_IO.C_Streams;
     
       with Interfaces.C_Streams;
       package Ada.Direct_IO.C_Streams is
          function C_Stream (F : File_Type)
             return Interfaces.C_Streams.FILEs;
          procedure Open
            (File : in out File_Type;
             Mode : in File_Mode;
             C_Stream : in Interfaces.C_Streams.FILEs;
             Form : in String := "");
       end Ada.Direct_IO.C_Streams;
     
       with Interfaces.C_Streams;
       package Ada.Text_IO.C_Streams is
          function C_Stream (F : File_Type)
             return Interfaces.C_Streams.FILEs;
          procedure Open
            (File : in out File_Type;
             Mode : in File_Mode;
             C_Stream : in Interfaces.C_Streams.FILEs;
             Form : in String := "");
       end Ada.Text_IO.C_Streams;
     
       with Interfaces.C_Streams;
       package Ada.Wide_Text_IO.C_Streams is
          function C_Stream (F : File_Type)
             return Interfaces.C_Streams.FILEs;
          procedure Open
            (File : in out File_Type;
             Mode : in File_Mode;
             C_Stream : in Interfaces.C_Streams.FILEs;
             Form : in String := "");
      end Ada.Wide_Text_IO.C_Streams;
     
      with Interfaces.C_Streams;
      package Ada.Stream_IO.C_Streams is
         function C_Stream (F : File_Type)
            return Interfaces.C_Streams.FILEs;
         procedure Open
           (File : in out File_Type;
            Mode : in File_Mode;
            C_Stream : in Interfaces.C_Streams.FILEs;
            Form : in String := "");
      end Ada.Stream_IO.C_Streams;
     

In each of these five packages, the C_Stream function obtains the FILE pointer from a currently opened Ada file. It is then possible to use the Interfaces.C_Streams package to operate on this stream, or the stream can be passed to a C program which can operate on it directly. Of course the program is responsible for ensuring that only appropriate sequences of operations are executed.

One particular use of relevance to an Ada program is that the setvbuf function can be used to control the buffering of the stream used by an Ada file. In the absence of such a call the standard default buffering is used.

The Open procedures in these packages open a file giving an existing C Stream instead of a file name. Typically this stream is imported from a C program, allowing an Ada file to operate on an existing C file.