12 Directory

Directory handling is very similar to file handling in the Mjølner System. Files and directories have similar properties like name, path, etc. Files are special since the contents typically are characters that can be read and written, directories are special since the contents are files and directories. These similar and special properties are both modeled in the file and directory libraries.

When using the directory library, a directory is simply declared as

ORIGIN '~beta/basiclib/directory';
...
  d: @directory;
...

A directory can be given a name:

'myDir' -> d.name;

And it can be tested for existence, content, entries, and be scanned:

The following program shows a simple use of directory: The directory with the path given as argument is scanned, and the names of all the entries are printed.

Program 15: ListDir.bet

ORIGIN '~beta/basiclib/directory';
---program: descriptor---
(# d: @directory;
do (if noOfArguments <> 2//true then
       'Usage: '->puttext; 1->arguments->puttext; ' path'->putline;
       stop;
   if);
   (* set name of directory *)
   2->arguments->d.name;
   (* print name of directory *)
   newline;
   d.name->puttext;
   (* test for content *)
   (if d.empty//true then ' is empty.'->putline;
    else ' is not empty.'->putline;
   if);
   'It contains the following '->puttext;
   d.noOfEntries->putint;
   ' entries: '->putline;
   (* scan the entries *)
   d.scanEntries
   (# (* found refers to the current entry *)
   do found.path->putline;
   #);
   newline;
#)

This program also checks for the number of arguments. If the number of arguments is not 2, then an error message is printed, and stop is called. stop is defined in the basic environment betaenv, and when called, terminates the execution.

Below we show the output of ListDir on the current working directory:

nil% ListDir .

. is not empty.
It contains the following 17 entries: 
.
..
CountChar.bet
CountChar1.bet
CountChar2.bet
ExploreTypes.bet
HelloWorld.bet
ListDir
ListDir.ast
ListDir.bet
MultipleAssignment.bet
Multiplication1.bet
Multiplication2.bet
Multiplication3.bet
SimpleTypes.bet
SquareRoot.bet
sun4s


Libraries Tutorial
© 1994-2004 Mjølner Informatics
[Modified: Thursday January 16th 2003 at 10:23]