Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Reid G.C.Thinking in PostScript.1990.pdf
Скачиваний:
17
Добавлен:
23.08.2013
Размер:
846.44 Кб
Скачать

Example 14.5: Renaming a File

%emulate “renamefile” if it doesn’t exist (copy the old file to the new and

%then try to delete the old file if possible)...

/renamefile where { pop }{ %ifelse

/renamefile % (oldname) (newname) renamefile - { %def

(w) file /new exch def dup (r) file /old exch def /buff 256 string def

{ % loop

old buff readstring { %ifelse new exch writestring

}{ %else

new exch writestring

old closefile new closefile exit

} ifelse

} loop

/deletefile where { pop deletefile } if } bind def

} ifelse

(/user/glenn/encoding.ps) (/user/glenn/StandardEncoding.ps) renamefile

WRITING FORMATTED DATA TO FILES

If you need to write out a data file or create a file with formatted data in it, you’ll need to become very familiar with the whole family of file writing operators, as well as the data conversion operators (discussed in Chapter 13). Let’s look at some useful techniques.

Writing Out Various Data Types

There are many different PostScript data types. Some of them are easy to write to a file; others present some challenges. To overgeneralize, simple data types (including integer, real, boolean, and mark) are relatively easy to convert into strings and write to a file, whereas composite objects such as dictionaries and arrays require some extra steps.

Let’s look at a few procedures that will help you write out some data types to a file. Example 14.6 and Example 14.7 provide some code that you can draw from. The basic idea is to turn your data into a string, then write that string to the output file with the writestring operator. Example

Chapter 14: USING FILES AND INPUT/OUTPUT TECHNIQUES

175

14.6 shows you how to write numbers and names to a file. You should be careful about white space and newline characters to keep the syntax of the numbers and names correct.

Example 14.6: Writing Numbers and Names to a File

/fd (outputfile.ps) (w) file def /scratch 1024 string def

/Wnum

% num Wnum -

{ %def

 

scratch cvs fd exch writestring } bind def

/Wname % /name Wname - { %def

dup type /nametype ne { %ifelse

fd (% invalid name\n) writestring pop }{ %else

dup xcheck not { fd (/) writestring } if scratch cvs fd exch writestring

}ifelse

}bind def

FontDirectory { %forall pop Wname

fd (\n) writestring

} forall

fd closefile

Notice that the Wnum procedure in Example 14.6 doesn’t pay much attention to the type of its operand, so you could use it for either an integer or a real number. The beauty of the cvs operator (used inside the Wnum procedure to convert the number to a string) is that it is polymorphic; it doesn’t matter what type of object you present to it, as long as there is a reasonable string equivalent. The Wname procedure is very much the same as the Wnum procedure, except that it prints a leading slash if the name presented to it is a literal name. Note that the slash is not part of the name itself. The slash syntax helps you to create a literal name when your program is parsed for the first time, but it just sets the literal flag on the name object, and does not otherwise differ from an executable name.

176

Chapter 14: USING FILES AND INPUT/OUTPUT TECHNIQUES

Spaces, Tabs, Returns, and Special Characters

If you intend to do any serious formatting of the output file, you will need to create white space, and you may need to write out some special characters. For the most part, the backslash syntax used for special characters will do this for you (see Table 14.4). To get them into your output file, just put them into a string body and write that string to the file with the writestring operator. You may find it convenient to use some procedures to do this for you, such as those found in Example 14.7.

Example 14.7: White Space and Special Characters

/fd (outputfile.ps) (w) file def /scratch 1024 string def

% these procedures all depend on “fd” being a valid output file descriptor /Wname % /name Wname -

{ %def

dup type /nametype ne { %ifelse

fd (% invalid name\n) writestring pop }{ %else

dup xcheck not { fd (/) writestring } if scratch cvs fd exch writestring

}ifelse

}bind def

/Wstring { fd exch writestring } bind def /Wline % (string) Wline - { %def

fd exch writestring return } bind def

/space % - space - { %def

fd ( ) writestring } bind def

/return % - return - { %def

fd (\n) writestring } bind def

/tab % - tab - { %def

fd (\t) writestring } bind def

Chapter 14: USING FILES AND INPUT/OUTPUT TECHNIQUES

177