[Visualoberon-win32] Re: Initial cut of directory scanning module
Status: Beta
Brought to you by:
tteuling
From: Michael v. A. <mi...@de...> - 2001-06-03 21:12:46
|
Michael Griebling <mgr...@sy...> writes: > Hi all, > > Here's the initial definition of the directory scanning interface. > Please make any comments and let me know what changes you > think would make this module better. I'm sure Michael will have > lots to say :-) Michael, nice to hear from you. I'm not quite sure why you think I have "lots to say" on this matter, but hey, I'll whip up an opinion in no time ;-) 1) I would suggest to split enumerating the names in a directory and getting data on the returned names. The most important reason for this is that enumerating the names is quite portable, while obtaining file stats isn't. Getting file info should be a different module, say "OS:Stat". This module should provide a function to get a info object for a file name, and this info object should have methods (_not_ attributes) to access all relevant information. In particular, I strongly suggest to drop the "is*" flags and replace each of them with a predicate method for portability and efficieny reasons. This methods would map to the C-level "S_IS*" macros. 2) The enumeration of the files should _not_ report the entries "." and ".." to the caller. Reason: They are always there, even if some readdir()s don't report them, and most of the time the user must filter them out anyway. 3) Don't ignore error messages with Open/Next/Close. 4) The iterator pattern for this module should be as simple as possible. The simplest one I can come up with is VAR d: Directory; res: Msg.Msg; ... d := Open(name); WHILE d.Next() DO (* ... do something with current entry *) END; Close(d); IF (d. res # done) THEN (* oops, something went wrong in one of the preceding fct calls *) END 5) Document that the reported order of files is pretty much random. 6) I would consider skipping Rewind(). Would anybody miss the Unix functions rewinddir(), telldir(), and seekdir()? Probably not. And when all is done, one could write yet another module that selects all files of a directory matching a regular expression and return them sorted according to a given relation. The regexp matcher I justed checked in under libadt might come in handy there :-) So, is this "lots" enough? -- mva > > MODULE Directories; > > IMPORT sc := SysClock; > > CONST > isDir * = 0; (* set when the entry is a directory *) > isRead * = 1; (* set when the entry is readable *) > isWrite * = 2; (* set when the entry is writeable *) > isExec * = 3; (* set when the entry is executable *) > isArch * = 4; (* set when the entry is archived *) > isHidden * = 5; (* set when the entry is hidden *) > isComp * = 6; (* set when the entry is compressed *) > isSys * = 7; (* set when the entry is a system file *) > isLink * = 8; (* set when the entry is a link to another file *) > > TYPE > Directory* = POINTER TO DirDesc; > DirDesc* = RECORD > ok- : BOOLEAN; (* true if last operation succeeded *) > size- : LONGINT; (* current size in bytes *) > date- : sc.DateTime; (* date attached to file *) > flags- : SET; (* set of attributes for the active entry > *) > END ; > > (* Open the directory named "name". A NIL pointer is returned > if the directory cannot be opened. *) > PROCEDURE Open * (name : ARRAY OF CHAR) : Directory; > > (* Returns the name of the active directory entry. An empty string > is returned at the last directory entry. *) > PROCEDURE (d : Directory) EntryIs * (VAR ename : ARRAY OF CHAR); > > (* Steps to the next entry in the current directory. "ok" is updated > to reflect the status of this operation. *) > PROCEDURE (d : Directory) Next * (); > > (* Restarts the directory scan from the beginning. *) > PROCEDURE (d : Directory) Rewind * (); > > (* Closes the directory "d". *) > PROCEDURE Close * (VAR d : Directory); > > END Directories. |