comp.lang.ada
 help / color / mirror / Atom feed
* File list on Windows and Debian
@ 2006-06-19  8:09 Anders Wirzenius
  2006-06-20  8:06 ` Ludovic Brenta
  0 siblings, 1 reply; 5+ messages in thread
From: Anders Wirzenius @ 2006-06-19  8:09 UTC (permalink / raw)


I am trying to process files in a directory. They have to be processed
in alphabetical order. With following code I get a nice sorted list on
Windows XP but not sorted at all on Linux Debian. Have I missed
something that switches the sorting on on Debian (Sarge)?

with Gnat.Directory_Operations;


with Ada.Command_Line;
with Ada.Exceptions;
with Ada.Text_IO;
with POSIX;
with POSIX_Files;
with Ada.Strings.Fixed;
procedure Ordning is

   use Ada;
   use Ada.Command_Line;
   use Ada.Text_IO;
   use POSIX;
   use POSIX_Files;

   procedure Display (Dirent : in     Directory_Entry;
                      Quit   : in out Boolean) is
      File_Name : String := To_String (Filename_Of (Dirent));
   begin
      Put_Line (File_Name);
   end Display;

   procedure Display_Directory is
      new For_Every_Directory_Entry (Action => Display);

begin
   declare
      use Gnat.Directory_Operations;
      This_Dir : Dir_Type;
      This_File: Dir_Name_Str (1..50);
      Last     : Natural;
   begin

      --
      --  First using Gnat.Directory_Operations
      --
      Put_Line ("First using Gnat.Directory_Operations");
      Put_Line ("-------------------------------------");
      Open (This_Dir, Get_Current_Dir);
      loop
         Read (This_Dir, This_File, Last);
         exit when Last=0;
         Put_Line (This_File (1..Last));
      end loop;
      Close (This_Dir);
      Put_Line ("-------------------------------------");
   end;
      --
      --  next using POSIX
      --
      Put_Line ("next using POSIX");
      Put_Line ("-------------------------------------");
   Display_Directory (To_POSIX_String ("."));
exception
   when E : others =>
      Put_Line (Exceptions.Exception_Name (E) & " - " &
                Exceptions.Exception_Message (E) & " : " &
                POSIX.Image (POSIX.Get_Error_Code));
end Ordning;


TIA
-- 
Anders



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: File list on Windows and Debian
  2006-06-19  8:09 File list on Windows and Debian Anders Wirzenius
@ 2006-06-20  8:06 ` Ludovic Brenta
  2006-06-20 10:23   ` Anders Wirzenius
  2006-06-21  6:13   ` Anders Wirzenius
  0 siblings, 2 replies; 5+ messages in thread
From: Ludovic Brenta @ 2006-06-20  8:06 UTC (permalink / raw)


Anders Wirzenius writes :
> I am trying to process files in a directory. They have to be processed
> in alphabetical order. With following code I get a nice sorted list on
> Windows XP but not sorted at all on Linux Debian. Have I missed
> something that switches the sorting on on Debian (Sarge)?

I tried your program at home, and had to tweak it a little bit for it
to compile. Then I also ghot unsorted directory entries, like I did
with another program I wrote.

I think this is by design on GNU/Linux systems. They don't impose the
overhead of sorting potentially large numbers of directory entries
unless you really need to sort them.

If you insist on sorting, you'd have to do that yourself: first, store
the directory entries (perhaps as GNAT.OS_Lib.String_Access values) in
a container and sort it. I would suggest either GNAT.Dynamic_Tables and
GNAT.Heap_Sort_G, or Charles.Vectors.Unbounded.Generic_Sort in the
libcharles0-dev package. Beware however of unbounded memory usage.

HTH

-- 
Ludovic Brenta.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: File list on Windows and Debian
  2006-06-20  8:06 ` Ludovic Brenta
@ 2006-06-20 10:23   ` Anders Wirzenius
  2006-06-20 11:59     ` M E Leypold
  2006-06-21  6:13   ` Anders Wirzenius
  1 sibling, 1 reply; 5+ messages in thread
From: Anders Wirzenius @ 2006-06-20 10:23 UTC (permalink / raw)


"Ludovic Brenta" <ludovic@ludovic-brenta.org> writes:

> Anders Wirzenius writes :
> > I am trying to process files in a directory. They have to be processed
> > in alphabetical order. With following code I get a nice sorted list on
> > Windows XP but not sorted at all on Linux Debian. Have I missed
> > something that switches the sorting on on Debian (Sarge)?
> 
> I tried your program at home, and had to tweak it a little bit for it
> to compile. Then I also ghot unsorted directory entries, like I did
> with another program I wrote.
> 
> I think this is by design on GNU/Linux systems. They don't impose the
> overhead of sorting potentially large numbers of directory entries
> unless you really need to sort them.
> 

Thanks for your time and effort. 

> If you insist on sorting, you'd have to do that yourself: first, store
> the directory entries (perhaps as GNAT.OS_Lib.String_Access values) in
> a container and sort it. I would suggest either GNAT.Dynamic_Tables and
> GNAT.Heap_Sort_G, or Charles.Vectors.Unbounded.Generic_Sort in the
> libcharles0-dev package. Beware however of unbounded memory usage.
> 

I'll look at them. 

> HTH

Yes it helped, it clarified the status quo.

-- 
Anders



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: File list on Windows and Debian
  2006-06-20 10:23   ` Anders Wirzenius
@ 2006-06-20 11:59     ` M E Leypold
  0 siblings, 0 replies; 5+ messages in thread
From: M E Leypold @ 2006-06-20 11:59 UTC (permalink / raw)



Anders Wirzenius <anders@no.email.thanks.invalid> writes:

> "Ludovic Brenta" <ludovic@ludovic-brenta.org> writes:
> 
> > Anders Wirzenius writes :
> > > I am trying to process files in a directory. They have to be processed
> > > in alphabetical order. With following code I get a nice sorted list on
> > > Windows XP but not sorted at all on Linux Debian. Have I missed
> > > something that switches the sorting on on Debian (Sarge)?
> > 
> > I tried your program at home, and had to tweak it a little bit for it
> > to compile. Then I also ghot unsorted directory entries, like I did
> > with another program I wrote.
> > 
> > I think this is by design on GNU/Linux systems. They don't impose the
> > overhead of sorting potentially large numbers of directory entries
> > unless you really need to sort them.

That is correct. I suppose that the Gnat directory functions just
interface to readdir(3) and with readdir() the order of entries is
dependend on the underlying filesystem. In ext2fs this is AFAIS the
order in which they are written (a directory is just a file with
dirent records, actually), with xfs (which uses btrees internally)
entries get retrieved mostly sorted but AFAIR "." and ".." are not the
first two and so on. I'd expect different results for reiser fs also.

Regards -- Markus






^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: File list on Windows and Debian
  2006-06-20  8:06 ` Ludovic Brenta
  2006-06-20 10:23   ` Anders Wirzenius
@ 2006-06-21  6:13   ` Anders Wirzenius
  1 sibling, 0 replies; 5+ messages in thread
From: Anders Wirzenius @ 2006-06-21  6:13 UTC (permalink / raw)


"Ludovic Brenta" <ludovic@ludovic-brenta.org> writes:

> Anders Wirzenius writes :
> > I am trying to process files in a directory. They have to be processed
> > in alphabetical order. With following code I get a nice sorted list on
> > Windows XP but not sorted at all on Linux Debian. Have I missed
> > something that switches the sorting on on Debian (Sarge)?
> 
> I tried your program at home, and had to tweak it a little bit for it
> to compile. Then I also ghot unsorted directory entries, like I did
> with another program I wrote.
> 
> I think this is by design on GNU/Linux systems. They don't impose the
> overhead of sorting potentially large numbers of directory entries
> unless you really need to sort them.
> 
> If you insist on sorting, you'd have to do that yourself: first, store
> the directory entries (perhaps as GNAT.OS_Lib.String_Access values) in
> a container and sort it. I would suggest either GNAT.Dynamic_Tables and
> GNAT.Heap_Sort_G, or Charles.Vectors.Unbounded.Generic_Sort in the
> libcharles0-dev package. Beware however of unbounded memory usage.
> 
> HTH
> 
> -- 
> Ludovic Brenta.
> 

I found the package OS_Services which does the job. It uses
GNAT.Heap_Sort_G.

Many thanks to J-P Rosen for a very useful package.

-- 
Anders



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-06-21  6:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-19  8:09 File list on Windows and Debian Anders Wirzenius
2006-06-20  8:06 ` Ludovic Brenta
2006-06-20 10:23   ` Anders Wirzenius
2006-06-20 11:59     ` M E Leypold
2006-06-21  6:13   ` Anders Wirzenius

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