Stripping styles from multiple files - Directory.Convert

While developing in Oberon, occasionally the need may arise to process many files by a single Oberon command. In this particular case we want to strip the styles and format information from e.g. all .Mod module text files.

The resulting plaintext files, even with CR line-endings, are often easier to handle during development when you use other software, like a version control program or a text editor like BBEdit. Also, conversion to plaintext is a necessary first step before translating to LF or CRLF line ends for export..

Too many open files

Processing many files by a single user command however puts a heavy load on Oberon, because each opened file remains open until the command terminates and the garbage collector has a chance to reclaim open file resources.

Therefore Directory.Convert will execute small batches in the background, in this case not to create a "continuous" task, but rather to limit peak resource usage and allow the garbage collector to reclaim file structures every once in a while.

Requirements for "Directory.Convert" :

Implementation notes

The implementation in Oberon is straightforward, especially since the entire task can easily be split into small portions that can be executed by the Tasks mechanism.

We will borrow code from System.Directory, and System.List: they implement a good mechanism to interpret wildcard file spec arguments. However the code needs to be copied into our program, because we don't want to alter System.Mod. Note that System.Directory itself does not have to deal with overloading Oberon file resources, because it does not actually need to open files in order to list them.

Since the number of files is potentially large, we will keep a queue of to-be-processed files as a (unregistered, anonymous) task file.

Each activation of the Directory.Convert command results in depositing the names of the wanted files to the tail of the queue, tracked by taskW, i.e. they are appended to the task file. The activation also causes the Oberon Task to be switched on.

Each time the task is executed by Oberon, max 10 filenames (set by the constant taskLoad) are read from the head of the queue, which is tracked by the taskR rider. After each task execution, the garbage collector is prompted to do its magic asap (Oberon.Collect(0)).

When all files in the queue are processed, the head (taskR) and tail (taskW) riders are reset to the start of a fresh task file and the Oberon task is stopped. New user commands can safely be issued before Convert is ready (if you are in a hurry).

Using the Module

The utility should be used with care, since e.g. "Directory.Convert *" will remove the styles/formats from all of your Oberon Text files! It does not create .Bak files but strips them too (if matching the pattern).

Safer usage would be more like "Directory.Convert *.Mod" e.g. to strip only source code texts.

The "Directory" module is a free download, available as a (zipped to preserve line-ends) archive.

Other Oberon-07 systems

If used on another Project Oberon system (i.e. not Oberon Workstation/Core) the import item "FileDir := FileDirOSX" should be replaced by just "FileDir", assuming a standard (Wirth) Oberon implementation.