In order to support the Oakwood Guildlines, OOC provides a set of basic library modules that comply with the Oakwood specification. (Note that all Oakwood modules may not be available.) All Oakwood compliant modules begin with the prefix "Oak".
Module `OakStrings' provides a set of operations on strings (i.e., on
string constants and character arrays, both of which contain the character
0X
as a terminator). All positions in strings start at 0
.
(The Oakwood Guildlines remark that string assignments and string comparisons are already supported by the language Oberon-2.)
(s: ARRAY OF CHAR): INTEGER
0X
.
(src: ARRAY OF CHAR; pos: INTEGER; VAR dst: ARRAY OF CHAR)
0<=pos<=Length(dst)
). If
pos=Length(dst)
, src is appended to dst. If
the size of dst is not large enough to hold the result of the
operation, the result is truncated so that dst is always terminated
with a 0X
.
(s: ARRAY OF CHAR; VAR dst: ARRAY OF CHAR)
Insert(s, Length(dst), dst)
.
(VAR s: ARRAY OF CHAR; pos, n: INTEGER)
0<=pos<=Length(s)
). If
n>Length(s)-pos
, the new length of s is
pos.
(src: ARRAY OF CHAR; pos: INTEGER; VAR dst: ARRAY OF CHAR)
Delete(dst, pos,
Length(src))
followed by
Insert(src, pos, dst)
.
(src: ARRAY OF CHAR; pos, n: INTEGER; VAR dst: ARRAY OF CHAR)
0<=pos<= Length(src)
) in src. If
n>Length(src)-pos
, dst is only the part of
src from pos to the end of src, i.e. Length(src)-1
.
If the size of dst is not large enough to hold the result of the
operation, the result is truncated so that dst is always terminated
with a 0X
.
(pat, s: ARRAY OF CHAR; pos: INTEGER): INTEGER
(VAR s: ARRAY OF CHAR)
Module `OakFiles' provides operations on files and the file directory.
The Oakwood Guildlines define the type File
as representing a
stream of bytes ususally stored on an external medium. A File
has a
certain length as well as the date and time of its last modification.
A file directory is a mapping from file names to files. A file that is not registered in the directory is considered temporary.
The type Rider
holds a read/write position in a file (positions start
with 0). There may be multiple riders set to the same file. The field
eof
is set to TRUE
if an attempt was made to read beyond the
end of the file. The field res
reports the success of
ReadBytes
and WriteBytes
operations. Writing data overwrites
old data at the rider position. When data is written beyond the end of the
file, the file length increases.
In general, all operations must use the following format for external representation:
SHORTINT
1 byte, INTEGER
2 bytes, LONGINT
4
bytes
FALSE
= 0, TRUE
= 1
REAL
4 bytes, LONGREAL
8 bytes
0X
Example:
VAR f: Files.File; r: Files.Rider; ch: CHAR;
Reading from an existing file:
f := Files.Old ("xxx"); IF f # NIL THEN Files.Set (r, f, 0); Files.Read (r, ch); WHILE ~ r.eof DO Files.Read (r, ch) END END
Writing to a new file yyy:
f := Files.New ("yyy"); Files.Set (r, f, 0); Files.WriteInt (r, 8); Files.WriteString (r, " bytes"); Files.Register (f)
Please note: This module implements virtual file descriptors; that is, an unlimited number of files can be open at the same time. These files share the limited number of file descriptors provided by the operating system.
The Oakwood Guildlines provide the following specifications:
WriteNum
and ReadNum
, should use the following encoding
algorithms for conversion to and from external format:
PROCEDURE WriteNum (VAR r: Rider; x: LONGINT); BEGIN WHILE (x < - 64) OR (x > 63) DO Write(r, CHR(x MOD 128 + 128)); x := x DIV 128 END; Write(r, CHR(x MOD 128)) END WriteNum; PROCEDURE ReadNum (VAR r: Rider; VAR x: LONGINT); VAR s: SHORTINT; ch: CHAR; n: LONGINT; BEGIN s := 0; n := 0; Read(r, ch); WHILE ORD(ch) >= 128 DO INC(n, ASH(ORD(ch) - 128, s) ); INC(s, 7); Read(r, ch) END; x := n + ASH(ORD(ch) MOD 64 - ORD(ch) DIV 64 * 64, s) END ReadNum;
The reason for the specification of the file name in the operation
New
is to allow allocation of the file on the correct medium from the
beginning (if the operating system supports multiple media).
The operations Read
, Write
, ReadBytes
and
WriteBytes
require the existence of a type SYSTEM.BYTE
with
the following characteristics:
SYSTEM.BYTE
, the corresponding
actual parameter may be of type CHAR
, SHORTINT
, or
SYSTEM.BYTE
.
ARRAY OF SYSTEM.BYTE
, the
corresponding actual parameter may be of any type. Note that this feature
is dangerous and inherently unportable. Its use should therefore be
restricted to system-level modules.
BOOLEAN
TRUE
if an attempt was made to read beyond the end of the
file.
INTEGER
ReadBytes
and WriteBytes
below for possible values of
res
.
(name: ARRAY OF CHAR): File
Old(name)
searches for the name in the directory and
returns the corresponding file. If the name is not found, it returns
NIL
.
(name: ARRAY OF CHAR): File
New(name)
creates and returns a new file. The name is
remembered for the later use of the operation Register
. The file is
only entered into the directory when Register
is called.
(f: File)
Register(f)
enters the file f into the directory together
with the name provided in the operation New
that created f.
The file buffers are written back. Any existing mapping of this name to
another file is overwritten.
(VAR f: File)
Close(f)
writes back the file buffers of f. The file is
still accessible by its handle f and the riders positioned on it. If
a file is not modified, it is not necessary to close it.
Please note: The above holds only for
permanentClose=FALSE
. Otherwise, the buffers are flushed and the file handle is deallocated (and f is set toNIL
); at this time, all riders on this file become invalid. This behaviour, and the variablepermanentClose
, are not part of The Oakwood Guidelines.
(f: File)
Purge(f)
resets the length of file f to 0
.
(name: ARRAY OF CHAR; VAR res: INTEGER)
Delete(name, res)
removes the directory entry for the
file name
without deleting the file. If res=0
the file has
been successfully deleted. If there are variables referring to the file
while Delete
is called, they can still be used.
(old, new: ARRAY OF CHAR; VAR res: INTEGER)
Rename(old, new, res)
renames the directory entry
old to new. If res=0
, the file has been successfully
renamed. If there are variables referring to the file while Rename
is called, they can still be used.
(f: File): LONGINT
Length(f)
returns the number of bytes in file f.
(f: File; VAR t, d: LONGINT)
GetDate(f, t, d)
returns the time t and date
d of the last modification of file f.
The encoding is as follows:
hour = t DIV 4096; minute = t DIV 64 MOD 64; second = t MOD 64; year = d DIV 512; month = d DIV 32 MOD 16; day = d MOD 32.
(VAR r: Rider; f: File; pos: LONGINT)
Set(r, f, pos)
sets the rider r to position
pos in file f. The field r.eof
is set to FALSE. The
operation requires that 0 <= pos <= Length(f)
.
(VAR r: Rider): LONGINT
Pos(r)
returns the position of the rider r.
(VAR r: Rider): File
Base(r)
returns the file to which the rider r has been
set.
(VAR r: Rider; VAR x: SYSTEM.BYTE)
Read(r, x)
reads the next byte x from rider r
and advances r accordingly.
(VAR r: Rider; VAR i: INTEGER)
ReadInt(r, i)
reads a integer number i from rider
r and advances r accordingly.
(VAR r: Rider; VAR i: LONGINT)
ReadLInt(r, i)
reads a long integer number i from
rider r and advances r accordingly.
(VAR r: Rider; VAR x: REAL)
ReadReal(r, x)
reads a real number x from rider
r and advances r accordingly.
(VAR r: Rider; VAR x: LONGREAL)
ReadLReal(r, x)
reads a long real number x from
rider r and advances r accordingly.
(VAR r: Rider; VAR i: LONGINT)
ReadNum(r, i
reads an integer number i from rider
r and advances r accordingly. The number i is compactly
encoded (see the "Remarks" section above).
(VAR r: Rider; VAR s: ARRAY OF CHAR)
ReadString(r, s)
reads a sequence of characters
(including the terminating 0X
) from rider r and returns it in
s
. The rider is advanced accordingly. The actual parameter
corresponding to s must be long enough to hold the character sequence
plus the terminating 0X
.
(VAR r: Rider; VAR s: SET)
ReadSet(r, s)
reads a set s from rider r and
advances r accordingly.
(VAR r: Rider; VAR b: BOOLEAN)
ReadBool(r, b)
reads a Boolean value b from rider
r and advances r accordingly.
(VAR r: Rider; VAR buf: ARRAY OF SYSTEM.BYTE; n: LONGINT)
ReadBytes(r, buf, n)
reads n bytes into
buffer buf starting at the rider position r. The rider is
advanced accordingly. If less than n bytes could be read,
r.res
contains the number of requested but unread bytes.
(VAR r: Rider; x: SYSTEM.BYTE)
Write(r, x)
writes the byte x to rider r and
advances r accordingly.
(VAR r: Rider; i: INTEGER)
WriteInt(r, i)
writes the integer number i to rider
r and advances r accordingly.
(VAR r: Rider; i: LONGINT)
WriteLInt(r, i)
writes the long integer number i to
rider r and advances r accordingly.
(VAR r: Rider; x: REAL)
WriteReal(r, x)
writes the real number x to rider
r and advances r accordingly.
(VAR r: Rider; x: LONGREAL)
WriteLReal(r, x)
write the long real number x to
rider r and advance r accordingly.
(VAR r: Rider; i: LONGINT)
WriteNum(r, i)
writes the integer number i to rider
r and advances r accordingly. The number i is compactly
encoded (see the "Remarks" section above).
(VAR r: Rider; s: ARRAY OF CHAR)
WriteString(r, s)
writes the sequence of characters
s (including the terminating 0X
) to rider r and advances
r accordingly.
(VAR r: Rider; s: SET)
WriteSet(r, s)
writes the set s to rider r
and advances r accordingly.
(VAR r: Rider; b: BOOLEAN)
WriteBool(r, b)
writes the Boolean value b to rider
r and advances r accordingly.
(VAR r: Rider; VAR buf: ARRAY OF SYSTEM.BYTE; n: LONGINT)
WriteBytes(r, buf, n)
writes the first n
bytes from buf to rider r and advances r accordingly.
r.res
contains the number of bytes that could not be written
(e.g., due to a disk full error).
Module `OakIn' provides a set of basic routines for formatted input of
characters, character sequences, numbers, and names. It assumes a standard
input stream with a current position that can be reset to the beginning of
the stream. A call to procedure Open
initializes module `OakIn'
and sets it to read from the standard input channel StdChannels.stdin
(see section Module StdChannels)
Module `OakIn' has a concept of a current position, which is the
character position in the input stream from where the next symbol is read.
Open
(re)sets it to the beginning of the input stream. After reading
a symbol, the current position is set to the position immediately after this
symbol. Before the first call to Open
, the current position is
undefined.
Done
is TRUE
after an input operation, the operation was successful and its result is
valid. An unsuccessful input operation sets Done
to FALSE
; it
remains FALSE
until the next call to Open
. In particular,
Done
is set to FALSE
if an attempt is made to read beyond the
end of the input stream.
Done
indicates if the operation was successful.
(VAR ch: CHAR)
(VAR n: LONGINT)
IntConst = digit {digit} | digit {hexDigit} "H".
(VAR n: INTEGER)
IntConst = digit {digit} | digit {hexDigit} "H".
(VAR n: LONGREAL)
LongRealConst = digit {digit} ["." {digit} [("D" | "E") ("+" | "-") digit {digit}]].
(VAR n: REAL)
RealConst = digit {digit} ["." {digit} ["E" ("+" | "-") digit {digit}]].
(VAR s: ARRAY OF CHAR)
StringConst = '"' char {char} '"'.
The string must not contain characters less than blank such as EOL
or
TAB
.
(VAR s: ARRAY OF CHAR)
Module `OakOut' provides a set of basic routines for formatted output of characters, numbers, and strings. It assumes a standard output stream to which the symbols are written.
(ch: CHAR)
(s: ARRAY OF CHAR)
0X
).
(i, n: LONGINT)
m
characters, i is
right adjusted in a field of Max(n, m)
characters padded with
blanks at the left end. A plus sign is not written.
(x: REAL; n: INTEGER)
m
characters (including a two-digit signed exponent), x is
right adjusted in a field of Max(n, m)
characters padded with blanks
at the left end. A plus sign of the mantissa is not written.
(x: LONGREAL; n: INTEGER)
m
characters (including a three-digit signed exponent), x is
right adjusted in a field of Max(n, m)
characters padded with blanks
at the left end. A plus sign of the mantissa is not written.
The Oakwood Guildlines requires the definition of the following mathematical constants (i.e., implementation-defined approximations):
(x: REAL): REAL
(x: LONGREAL): LONGREAL
sqrt(x)
returns the square root of x, where x must
be positive.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
sin(x)
returns the sine value of x, where x is in
radians.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
cos(x)
returns the cosine value of x, where x is in
radians.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
tan(x)
returns the tangent value of x, where x is
in radians.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
arcsin(x)
returns the arcsine value in radians of x,
where x is in the sine value.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
arcos(x)
returns the arcos value in radians of x, where
x is in the cosine value.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
arctan(x)
returns the arctan value in radians of x, where
x is in the tangent value.
(x, base: REAL): REAL
(x, base: LONGREAL): LONGREAL
power(x, base)
returns the x to the power
base.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
round(x)
if fraction part of x is in range 0.0
to
0.5
, then the result is the largest integer not greater than x,
otherwise the result is x rounded up to the next highest whole number.
Note that integer values cannot always be exactly represented in
LONGREAL
or REAL
format.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
ln(x)
returns the natural logarithm (base e) of x.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
exp(x)
is the exponential of x base e. x must not
be so small that this exponential underflows nor so large that it overflows.
(x, base: REAL): REAL
(x, base: LONGREAL): LONGREAL
log(x, base)
is the logarithm of x base base.
All positive arguments are allowed. The base base must be positive.
(xn, xd: REAL): REAL
(xn, xd: LONGREAL): LONGREAL
arctan2(xn,xd)
is the quadrant-correct arc tangent
`atan(xn/xd)'. If the denominator xd is zero, then
the numerator xn must not be zero. All arguments are legal except
xn = xd = 0
.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
sinh(x)
is the hyperbolic sine of x. The argument
x must not be so large that exp(|x|)
overflows.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
cosh(x)
is the hyperbolic cosine of x. The argument
x must not be so large that exp(|x|)
overflows.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
tanh(x)
is the hyperbolic tangent of x. All arguments
are legal.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
arcsinh(x)
is the arc hyperbolic sine of x. All
arguments are legal.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
arccosh(x)
is the arc hyperbolic cosine of x. All
arguments greater than or equal to 1
are legal.
(x: REAL): REAL
(x: LONGREAL): LONGREAL
arctanh(x)
is the arc hyperbolic tangent of x.
|x| < 1 - sqrt(em)
, where `em' is machine epsilon. Note
that |x|
must not be so close to 1
that the result is
less accurate than half precision.
Go to the first, previous, next, last section, table of contents.