A multi-file text-styles stripper -
While developing in Oberon, occasionally the need may arise to process many files by a single 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 IDE on the Mac or PC, together with Oberon.
Also, conversion to plaintext is a necessary first step before translating to LF or CRLF line ends for export..
Processing many files within a single user command execution 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 file resources.
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.
Directory.Convertare put forward:
- it will rewrite Oberon Text files into the plain text form, without the rich text information but keeping CR line ends.
- text will be written to a file with the same name, thus accepting loss of the old file with styles information.
- it shall accept any number of specified files, but not require more than 20-some files to be open at one time.
- it will output the name of each translated file in the System Log
- it should accept a "wildcard" files specification argument, a-la the
- it shall have measures to protect against inadvertent destructive results when applied to non-Text files; it also shall not touch files that are already plaintext
- the module should be suitable to be extended to other multi-file operations, (search, export, compile, snaphot, backup...)
- The Oberon Tasks facility will be used to put actual processing in the background
- Task management should not significantly load the memory (heap), whether the module is active or just loaded
- The program should work on any Project Oberon system (no SYSTEM import)
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.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.
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).
Note: This 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). Klingons don't take prisoners!
Typical usage would be more like "
Directory.Convert *.Mod" to strip only source code texts.
The "Directory" module is available for download in case you don't want to program it yourself.
If used on another Project Oberon system (i.e. not Oberon Workstation) the import item "
FileDir := FileDirOSX" should be replaced by just "
FileDir", assuming a standard (Wirth) Oberon implementation.