ergetgetagt

Page 57

Chapter 6: Programming

52

void texpreamble(string s); The TEX environment can be reset to its initial state, clearing all macro definitions, with the function void texreset(); The routine void usepackage(string s, string options=""); provides a convenient abbreviation for texpreamble("\usepackage["+options+"]{"+s+"}"); that can be used for importing LaTeX packages.

6.6 Files Asymptote can read and write text files (including comma-separated value) files and portable XDR (External Data Representation) binary files. An input file must first be opened with input(string name, bool check=true, string comment="#"); reading is then done by assignment: file fin=input("test.txt"); real a=fin; If the optional boolean argument check is false, no check will be made that the file exists. If the file does not exist or is not readable, the function bool error(file) will return true. The first character of the string comment specifies a comment character. If this character is encountered in a data file, the remainder of the line is ignored. When reading strings, a comment character followed immediately by another comment character is treated as a single literal comment character. One can change the current working directory for read operations to the contents of the string s with the function string cd(string s), which returns the new working directory. If string s is empty, the path is reset to the value it had at program startup. When reading pairs, the enclosing parenthesis are optional. Strings are also read by assignment, by reading characters up to but not including a newline. In addition, Asymptote provides the function string getc(file) to read the next character (treating the comment character as an ordinary character) and return it as a string. A file named name can be open for output with file output(string name, bool update=false); If update=false, any existing data in the file will be erased and only write operations can be used on the file. If update=true, any existing data will be preserved, the position will be set to the end-of-file, and both reading and writing operations will be enabled. For security reasons, writing to files in directories other than the current directory is allowed only if the -globalwrite (or -nosafe) command-line option is specified. The function string mktemp(string s) may be used to create and return the name of a unique temporary file in the current directory based on the string s. There are two special files: stdin, which reads from the keyboard, and stdout, which writes to the terminal. The implicit initializer for files is null. Data of a built-in type T can be written to an output file by calling one of the functions


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.