[fpc-pascal]directory tree in memory

Gabor DEAK JAHN djg at tramontana.co.hu
Mon Oct 15 05:43:10 CEST 2001


At 10/15/01 01:29 AM, you wrote:

Ivan,

 > pMyDirsItem = ^mydirsitem;
 > myDirsItem =RECORD
 >   info :tFileInfo; //name, size, time/date ...
 >   sub :tList;
 > end;

A slight modification would yield a more general solution:

type
   PDirNode = ^TDirNode;
   TDirNode = record
     info : ...
     Next, Sub : PDirNode;
var
   Root : PDirNode;

For each node, you can store the file information bits, as you described. If
it is a directory, the Sub will point to the node of the subdirectory, for a
simple file Sub = nil. In addition, Next points to the next node (directory
or file residing in the same directory), for the last node in the list, Next
= nil;

To go through the whole structure (to find a file or for any other reason),
you have to walk this tree--the actual order might differ but I guess
post-order would be the most suited for the usual tasks you can do with file
system structures. Thus, you first process the info field (eg. when looking
for a file, compare the name or attributes to the one you are looking for).
Then, if Sub <> nil, you proceed to the node pointed by Sub recursively.
When you return, if Next <> nil, you proceed to the node pointed by it. If
Next = nil, you return back to the caller. Basically:

Walk (p : PDirNode);
begin
   repeat
     ... p^.info ...
     if Sub <> nil then Walk (Sub);
     p := p^.Next;
   until p = nil;
end;


Regards,

   Gabor DEAK JAHN

-------------------------------------------------------------------
Gabor DEAK JAHN -- Budapest, Hungary.
WWW: www.tramontana.co.hu
E-mail: djg at tramontana.co.hu





More information about the fpc-pascal mailing list