|
[Next][Prev] [Right] [Left] [Up] [Index] [Root]
Magma provides a special file type for the reading and writing of
external files. Most of the standard C library functions can be applied
to such files to manipulate them.
Subsections
Given a filename (string) S, together with a type indicator T,
open the file named by S and return a Magma file object associated
with it. Tilde expansion is performed on S.
The standard C library function fopen() is used, so
the possible characters allowed in T are the same as those allowed
for that function in the current operating system, and have the
same interpretation. Thus one should give the value "r" for T
to open the file for reading, and give the value "w" for T to
open the file for writing, etc. (Note that in the PC version of Magma,
the character "b" should also be included in T if the file is
desired to be opened in binary mode.) Once a file object is created,
various I/O operations can be performed on it --- see below. A file
is closed by deleting it (i.e. by use of the delete statement or
by reassigning the variable associated with the file); there is no
Fclose function. This ensures that the file is not closed while
there are still multiple references to it. (The function is called
Open instead of Fopen to follow Perl-style conventions. The
following functions also follow such conventions where possible.)
Given a file F, flush the buffer of F.
Given a file F, return the offset in bytes of the file pointer within F.
Perform fseek(F, o, p); i.e. move the file pointer of F to
offset o (relative to p: 0 means beginning, 1 means current, 2 means end).
Perform rewind(F); i.e. move the file pointer of F to the
beginning.
Put (write) the characters of the string S to the file F.
Put (write) the characters of the string S, followed by a newline
character, to the file F.
Given a file F, get and return one more character from file F as a string.
If F is at end of file, a special EOF marker string is returned;
the function IsEof should be applied to the character to test for end
of file. (Thus the only way to loop over a file character by character is to
get each character and test whether it is the EOF marker before processing it.)
Given a file F, get and return one more line from file F as a string.
The newline character is removed before the string is returned.
If F is at end of file, a special EOF marker string is returned; the
function IsEof should be applied to the string to test for end
of file.
Given a string S, return whether S is the special EOF marker.
Given a character (length one string) C, together with a file F, perform
ungetc(C, F); i.e. push the character C back into the input buffer
of F.
We write a function to count the number of lines in a file.
Note the method of looping over the characters of the file: we must get
the line and then test whether it is the special EOF marker.
> function LineCount(F)
> FP := Open(F, "r");
> c := 0;
> while true do
> s := Gets(FP);
> if IsEof(s) then
> break;
> end if;
> c +:= 1;
> end while;
> return c;
> end function;
> LineCount("/etc/passwd");
59
Function that returns the contents of the text-file with name
indicated by the string F. Here F may be an expression returning
a string.
Function that returns the contents of the text-file with name
indicated by the string F as a binary string.
In this example we show how Read can be used to import the complete
output
from a separate C program into a Magma session. We assume
that a file mystery.c (of which the contents are shown below)
is present in the current directory. We first compile it, from within
Magma, and then use it to produce output for the
Magma version of our mystery function.
> Read("mystery.c");
#include <stdio.h>
main(argc, argv)
int argc;
char **argv;
{
int n, i;
n = atoi(argv[1]);
for (i = 1; i <= n; i++)
printf("%d\n", i * i);
return 0;
}
> System("cc mystery.c -o mystery");
> mysteryMagma := function(n)
> System("./mystery " cat IntegerToString(n) cat " >outfile");
> output := Read("outfile");
> return StringToIntegerSequence(output);
> end function;
> mysteryMagma(5);
[ 1, 4, 9, 16, 25 ]
[Next][Prev] [Right] [Left] [Up] [Index] [Root]
|