File Types and Modes
|
Description
|
|
•
|
The Maple I/O library makes use of a number of different concepts relating to kinds, modes, and types of files. For most file I/O operations, this can be safely ignored, but sometimes this information is important.
|
•
|
Maple also makes use of files for saving and restoring Maple objects. For more information on these, see file.
|
|
|
Maple I/O Library
|
|
|
The Maple I/O Library recognizes several files types.
|
STREAM
|
a buffered file as is typically provided by the C standard I/O library (eg. fopen, etc.).
|
RAW
|
an unbuffered file as is typically provided by the UNIX (and most other platforms) system call library (eg. open), etc.).
|
PIPE
|
a double-ended pipe as is provided by the UNIX system call library (eg. pipe, etc.). The PIPE type is supported only in UNIX implementations.
|
PROCESS
|
a pipe that has one end attached to another process, specified by a command line, as is provided by the UNIX functions popen and pclose.
|
DIRECT
|
direct access to Maple's current (default) or top-level (terminal) input or output stream (ie. where you type and see results when you run Maple interactively).
|
|
|
|
|
Access Modes
|
|
•
|
Files are open in either READ or WRITE modes. The mode in which a file is opened is determined when it is opened explicitly by one of the functions fopen, open, pipe, or popen, or when it is opened implicitly by one of the other I/O functions.
|
•
|
If a STREAM, RAW, or DIRECT file is open in READ mode, and an attempt is made to write to it, it is automatically closed and reopened in WRITE mode, at the same position. The reverse is also true (although that isn't exactly how it is implemented). This allows one to create code that scans through a file and begins writing at a specified position.
|
•
|
Other kinds of files, which are always opened explicitly, cannot change modes after they are open.
|
|
|
File Types (or Line Ending Conventions)
|
|
•
|
UNIX files are typeless; they are just streams of bytes. Unfortunately, this is not always true in other operating systems. Many systems distinguish between text files (a stream of characters) and binary files (a stream of bytes). This distinction is generally limited to the question of which sequence of characters represents a newline ("\n"). Under DOS and Windows, newline is represented in a file as two characters ("\r\n"). On Macintosh, newline is represented as a carriage return character ("\r"). Under UNIX, newline is just a linefeed character ("\n").
|
•
|
Therefore, Maple provides a means of specifying whether a STREAM is TEXT or BINARY. If the STREAM is opened explicitly, the file type can be specified at that time. If it is opened implicitly, the file type is determined by context (for example, a STREAM that is opened by a call to fprintf will be opened with type TEXT).
|
•
|
A STREAM opened with type TEXT will have "\n" translated appropriately on output, and vice versa on input. Any file opened with type BINARY will see the contents of the file as-is.
|
•
|
Non-STREAM files, which are always opened explicitly, are automatically opened with type BINARY. The only exception is DIRECT files, which are always opened with type TEXT.
|
•
|
Under UNIX, which makes no distinction between text and binary files, specifying a file type is allowed, but superfluous.
|
|
|
Default and Terminal Files
|
|
•
|
The two special filenames "default" and "terminal" refer to DIRECT files.
|
•
|
The file "default" refers to the default input or output stream of a Maple session. When Maple is run interactively, these refer to the keyboard and screen. The "default" stream is roughly equivalent to "stdin" and "stdout" under UNIX.
|
•
|
The file "terminal" refers to the top-level input or output stream of a Maple session. When no read is in effect, "terminal" as an input file is equivalent to "default". Similarly, when no write is in effect, "terminal" as an output file is equivalent to "default".
|
|
|