comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic.brenta@insalien.org>
Subject: Re: File_Type
Date: 20 Apr 2004 08:08:26 GMT
Date: 2004-04-20T08:08:26+00:00	[thread overview]
Message-ID: <2004420-10826-784447b@foorum> (raw)
In-Reply-To: x7v8ygrp671.fsf@smaug.pushface.org


Jorge Suarez-Solis Rivaya asked:
> Is there anyway to use an Integer Type like a File_Type?,
> Is there anyway to asign a File_Type the value 3?

Yes, there is, on Unix and Unix-like operating systems; perhaps also
on others as well.  The way to achieve this is to write a thin binding
to the operating system's file management functions, like so:

package Low_Level_IO is
   type File_Type is new Integer;
   File_Error : exception;
   function Open (File_Name : in String) return File_Type;
   procedure Write (Into : in File_Type; What : in String);
end Low_Level_IO;

package body Low_Level_IO is
   function Open (File_Name : in String) return File_Type is
      function Internal (File_Name : in String,
                         Flags : in Integer) return File_Type;
      pragma Import (C, Internal, "open");
      Result : File_Type := Internal (File_Name & ASCII.NUL, 12);
   begin
      if Result = 0 then
         raise File_Error;
      end if;
      return Result;
   end Open;


   procedure Write (Into : in File_Type; What : in String) is
      function Internal (Into : in File_Type; What : in String;
                         Size : in Integer) return Integer;
      pragma Import (C, Internal, "write");
      Result : Integer := Internal (Into, What, What'Length);
   begin
      if Result /= What'Length then
         raise File_Error;
      end if;
   end Write;
end Low_Level_IO;


But, you have to ask yourself exactly why you are trying to use such
low-level code.  This is platform-dependent and error-prone.  I
encourage you to consider using Ada's IO libraries, or GNAT.OS_Lib,
which provide most of what you want in a portable manner.  Indeed, if
you look at the implementation of GNAT.OS_Lib, you will find something
roughly equivalent to what I wrote above.

You should try to avoid messing around with file descriptors if you
can.  It is better to use high-level abstractions; they are much
safer.

-- 
Ludovic Brenta.

-- 
Ce message a ete poste via la plateforme Web club-Internet.fr
This message has been posted by the Web platform club-Internet.fr

http://forums.club-internet.fr/



  reply	other threads:[~2004-04-20  8:08 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-18 12:17 File_Type Jorge Suarez-Solis Rivaya
2004-04-18 16:30 ` File_Type Martin Krischik
2004-04-18 20:47 ` File_Type tmoran
2004-04-18 22:02 ` File_Type sk
2004-04-20  5:30   ` File_Type Simon Wright
2004-04-20  8:08     ` Ludovic Brenta [this message]
2004-04-20 19:39       ` File_Type Keith Thompson
2004-04-20  8:23     ` File_Type sk
2004-04-18 22:11 ` File_Type Marius Amado Alves
2004-04-18 23:17   ` File_Type Georg Bauhaus
2004-04-21 17:03     ` File_Type Warren W. Gay VE3WWG
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox