[Next] [Prev] [Right] [Left] [Up] [Index] [Root]
External Files

External Files

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

Opening Files

Open(S, T) : MonStgElt, MonStgElt -> File
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.)
POpen(S, T) : MonStgElt, MonStgElt -> File
Given a shell command line S, together with a type indicator T, open a pipe between the Magma process and the command to be executed. The standard C library function popen() 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 so that Magma can read the output from the command, and give the value "w" for T so that Magma can write into the input of the command. See the Pipe intrinsic for a method for sending input to, and receiving output from, a single command.

Operations on File Objects

Flush(F) : File ->
Given a file F, flush the buffer of F.
Tell(F) : File -> RngIntElt
Given a file F, return ftell(F), i.e. the position of the current offset in bytes within F.
Seek(F, o, p) : File, RngIntElt, RngIntElt ->
(Procedure.) 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).
Rewind(F) : File ->
(Procedure.) Perform rewind(F); i.e. move the file pointer of F to the beginning.
Put(F, S) : File, MonStgElt ->
(Procedure.) Put (write) the characters of the string S to the file F.
Puts(F, S) : File, MonStgElt ->
(Procedure.) Put (write) the characters of the string S, followed by a newline character, to the file F.
Getc(F) : File -> MonStgElt
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.)
Gets(F) : File -> MonStgElt
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.
IsEof(S) : MonStgElt -> BoolElt
Given a string S, return whether S is the special EOF marker.
Ungetc(F, c) : MonStgElt, File -> MonStgElt
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.

Example IO_LineCount (H3E9)

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

Example IO_GetTime (H3E10)

We write a function which returns the current time as 3 values: hour, minutes, seconds. The function opens a pipe to the UNIX command "date" and applies regular expression matching to the output to extract the relevant fields.

> function GetTime()
>     D := POpen("date", "r");
>     date := Gets(D);
>     _, _, f := Regexp("([0-9][0-9]):([0-9][0-9]):([0-9][0-9])", date);
>     h, m, s := Explode(f);
>     return h, m, s;
> end function;
> h, m, s := GetTime();
> h, m, s;
14 30 01
> h, m, s := GetTime();
> h, m, s;
14 30 04

Reading a Complete File

Read(F) : MonStgElt -> MonStgElt
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.

Example IO_Read (H3E11)

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]