comp.lang.ada
 help / color / mirror / Atom feed
From: "David Botton" <David@Botton.com>
To: <comp.lang.ada@ada.eu.org>
Subject: Re: How to rename a file?
Date: Thu, 27 Sep 2001 23:41:32 -0400
Date: 2001-09-27T23:41:32-04:00	[thread overview]
Message-ID: <mailman.1001648485.22108.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: Y3Rs7.46826$QK.32812951@news1.sttln1.wa.home.com

On Win32 if you wish to be cross compiler (this also demonstrates why thin
bindings are mostly a waste of time for Ada):

First the ugly method:

   function Rename_File (Old_File, New_File : String) return Boolean
   is
      Old_C : Interfaces.C.Char_Array := Interfaces.C.To_C (Old_File);
      New_C : Interfaces.C.Char_Array := Interfaces.C.To_C (New_File);

      function MoveFile
        (Old_Name : Interfaces.C.Char_Array := Old_C;
         New_Name : Interfaces.C.Char_Array := New_C)
        return Boolean;
      pragma Import (StdCall, MoveFile, "MoveFileA");
   begin
      return MoveFile;
   end Rename_File;

Now the cool method:

   function Rename_File2 (Old_File, New_File : String) return Boolean
   is
      function MoveFile
        (Old_Name : String := Old_File & Character'Val (0);
         New_Name : String := New_File & Character'Val (0))
        return Boolean;
      pragma Import (StdCall, MoveFile, "MoveFileA");
   begin
      return MoveFile;
   end Rename_File2;

Ada arrays are passed as C arrays do to the import so the use of String is
valid, we just need to Zero terminate. Ada record types are also passed as
pointers, which makes life a lot easier. For many more examples look through
the bodies of GWindows code http://www.adapower.com/gwindows


BTW, if you don't care about the result:

   procedure Rename_File3 (Old_File, New_File : String)
   is
      procedure MoveFile
        (Old_Name : String := Old_File & Character'Val (0);
         New_Name : String := New_File & Character'Val (0));
      pragma Import (StdCall, MoveFile, "MoveFileA");
   begin
      MoveFile;
   end Rename_File3;


If you are using a compiler other then GNAT, you may need to insure that
WinBase.lib is linked in (as you would using a Win32 thin binding).

David Botton




----- Original Message -----
From: "DuckE" <nospam_steved94@home.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Thursday, September 27, 2001 10:16 PM
Subject: Re: How to rename a file?


> Here's how I make the OS call on WindowsNT/W2K using ObjectAda or GNAT:
>
> WITH Win32;
> WITH Win32.WinBase;
> WITH Interfaces.C;
>  ...
>   PACKAGE WinBase RENAMES Win32.WinBase;
>   PACKAGE C RENAMES Interfaces.C;
>
>   USE TYPE Win32.BOOL;
>   USE TYPE Win32.DWORD;
> ...
>   PROCEDURE RenameFileDFU( sourceFileNameDFU, destFileNameDFU : STRING;
>                            renamedDFU : out BOOLEAN ) IS
>     result     : Win32.BOOL;
>     sourceName : ALIASED C.Char_Array := C.To_C( sourceFileNameDFU );
>     destName   : ALIASED C.Char_Array := C.To_C( destFileNameDFU );
>   BEGIN
>     result := WinBase.MoveFile( sourceName(0)'UNCHECKED_ACCESS,
>                                 destName(0)'UNCHECKED_ACCESS );
>     renamedDFU := result /= Win32.FALSE;
>   END RenameFileDFU;
>
> I hope this helps,
> SteveD
>
> "Pi" <pi3_1415926536@yahoo.ca> wrote in message
> news:MYKs7.11405$%r.2963162@news20.bellglobal.com...
> > It may sound stupid, but I didn't find anything.
> >
> > Is there a way to rename files in Ada?
> >
> > Other than :
> > create a new file,
> > copy every byte into the new one,
> > delete the old one.
> >
> > --
> > 3,14159265359
> >
>
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>




  parent reply	other threads:[~2001-09-28  3:41 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-27 19:43 How to rename a file? Pi
2001-09-27 23:09 ` tmoran
2001-09-27 23:36   ` Larry Kilgallen
2001-09-28  2:16 ` DuckE
2001-09-28  2:54   ` Pi
2001-09-28  3:41   ` David Botton [this message]
2001-09-28  7:39     ` Lutz Donnerhacke
2001-09-28 17:34     ` Pascal Obry
2001-09-28 18:39       ` Larry Kilgallen
2001-09-30  1:51       ` David Botton
2001-09-30  8:53         ` Pascal Obry
2001-09-30 10:21           ` Florian Weimer
2001-09-30 14:53             ` Pascal Obry
2001-09-29  5:02     ` DuckE
2001-09-28  3:16 ` David Botton
2001-09-28 16:03   ` Jeffrey Carter
replies disabled

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