c Xerox Corporation 1980Name: WriteFormattedMaintainer: Eric Schmidt, Larry StewartDate: June 5, 1980 8:34 PMPurpose: WriteFormatted is a family of procedures to write strings on an output stream. The user mayalso define his own formats and incorporate them into WF via procedure calls.WriteFormatted is based on the UNIX printf routine for C programs, Ed McCreight's FORMATroutines and Ed Taft's TEMPLATE routines for BCPL, and the BCPL string conventions.Note: The DEFINITIONS file has been renamed "WF.bcd" and its implementor is now"WFImpl.bcd" to conform to the Pilot and Cedar naming conventions.General Format:WFn["string with escapes", arg1, ..., argn];where n (between 0 and 4) is the number of substitutions into the string by values of arg1, ...Substitution positions in the string are of the form %z, where z is an expression composed of:- An optional minus sign ('-') which specifies left-adjustment instead of the normal right adjustmentof the argument.- An optional zero ('0') which indicates a field that, if necessary, will be filled with leading zeroesrather than with leading blanks.- An optional number specifying a field width; i.e. the number of digits for a number, or themaximum number of characters to be printed from a string.- The field width number may be of the form xx.yy for use by a user supplied floating point outputroutine. (See RealDefs.)- An option letter 'l' for long (32-bit) numbers.- A character indicating the conversion to be applied to the argument.The conversion characters are (upper-case is also allowed):b - Print one word quantity as octal number (unsigned). No trailing 'B' is supplied.c - The argument is interpreted as a CHARACTER and printed.d - Treat as INTEGER, print decimal number.s - The argument is interpreted as a STRING and printed.u - The argument is interpreted as a CARDINAL and printed (unsigned).x - Print one word quantity as hexadecimal number (unsigned).If no recognizable character follows the %, and it is not a user-supplied routine (see below), then itis printed. Thus % may be printed by %%.",pqX ^e []' XU UMe SM P$rq(r O=qrq3 L5 JB GbrD7q, A U ?E <\ X : 707 6( 27& 1y9 .MF , r )q1 &sF #G; U ; + 8 nE C= +; ) >[2If the optional letter 'l' preceds one of 'b', 'd', 'r', 't', 'u', or 'x' WF treats the correspondingparameter as a POINTER TO a 32-bit quantity:lb - Treat as POINTER to 32-bit quantity, print in octal (unsigned). No leading 0's or trailing 'B'are supplied.ld - Treat as POINTER TO LONG INTEGER, print 32-bit integer.lr - Treat as POINTER to 32-bit quantity which is the difference between twoTimeDefs.PackedTime's, print in "hr:mn:sc" format.lt - Treat as POINTER to TimeDefs.PackedTime, print in TimeDefs.AppendTime[] format.lu - Treat as POINTER TO LONG CARDINAL, print 32-bit unsigned integer.lx - Treat as POINTER to 32-bit quantity, print in hexadecimal (unsigned).Special CharactersCharacters preceded by an * are special, and have the following interpretations (upper-case is alsoallowed):*n(12B,newline):Print a newline character.*b(10B,backspace):Print a backspace character.*t(11B,tab):Print a tab character.*f(14B,formfeed):Print a new page (formfeed).*nnn(nnn is precisely three digits): Print a char. whose ASCII code is Octal nnn.Unrecognizable characters are simply printed, so * may be obtained by saying **.User-Supplied ConversionsWriteFormatted maintains a table of 26 conversion routines, one for each letter of the alphabet.The string is scanned left to right and characters are output until a % is reached. The conversioncode is used to select the routine to interpret the corresponding argument. Users can set aconversion escape usingSetCode[c: CHARACTER, p: PROCEDURE[d: UNSPECIFIED, form: STRING, wp:PROCEDURE[CHARACTER]]];which sets character c to be interpreted by procedure p. d is the argument to be interpreted, formis the string of characters between the % and the character char (i.e. "-nnn"), and wp is theprocedure that should be used to output characters (i.e. the prevailing output). Default conversionsmay be overwritten, and should be reset to their original when finished byResetCode[char: CHARACTER];which resets the code of char to the default. In the case of multi-word items, the procedure mustuse a pointer to the items. The user-supplied conversion procedure may call WF for any output, aslong as it does not use the conversion it is implementing!Examples:Nfq bB$ `, ]nH[ X< U@ T2 PT MF JJ Gbr D7q?$ B ? # <\# 90 # 6# 2M /P ,r )Wq#= 'I &OM $!8   5. lF '> dJ9  S ] : r >\x3Print a simple string:WF0["a tab *t is before a carriage return*n"];Print the values of a, b, c, and a string:WF4["print a string %s, followed by three numbers %d, %d, %d*n",str,a,b,c];Use the formatting information to left justify and specify field widths:WF2["%-14s is followed by %6d*n",s,a];Use user supplied routines:SetCode['m,PrintStrange];WF2["print a few strange things %m and %m*n",@StrangeThing1,@StrangeThing2];ResetCode['m];WF0["now %m just prints an m"];Print 32-bit quantities:m: LONG INTEGER _ 50000;WF2["Try %ld, %lb",@m,@m];Other Procedures:WFC[CHARACTER]; prints a single character.WFCR[]; prints a carriage return.WFN[STRING, DESCRIPTOR FOR ARRAY OF UNSPECIFIED]; is a generalization of WF0-4that takes its arguments in an array.Formatted Output to Strings and Streams:Routines SWF0[] through SWF4[] and SWFN[] are available to put the output that would havegone to the termnal on a string. For example:str: STRING _ [20];SWF1[str,"one %d two",5];will copy the string "one 5 two" onto string str. Any previous contents of str are lost.Likewise FWF0[] through FWF4[] and FWFN[] take a StreamHandle parameter and do a stream"put" on each character. No other Stream operations are called. For example:sh: StreamDefs.StreamHandle;FWF1[sh,"one %d two",5];will write the string onto the stream sh.Nfq b^. [*XK UpHRE& OKHLE Bl ?A<8 5r 2q, /h# ,<J*% & r( "qC !Y.. ~-rqrq SI N  &rq, =[^4If you don't like WriteChar:A procedure identifier is used by WF to output each character. If P and OP are procedures with acharacter as arguments, the procedure identifier used by WF can be changed via:OP _ SetWriteProcedure[P];so that P will be called for each character (SetWriteProcedure returns the previously usedprocedure). This can be used to write to other streams or to strings.CallingOP _ WriteToString[s: STRING];will cause subsequent WFn calls to append things to string s (note the length is never reset and OPis the previously used procedure).The statement[] _ SetWriteProcedure[OP];will reset to write back to the default stream.Bugs:Files: On [ivy]writeformatted> you will find the source and object for WF and WFImpl.Nfq ^r [q$= ZCOW SQ RhF O=L H:* Gb" D7 A  =/ :sq 6sqX 5=2M`MATH  TIMESROMAN  TIMESROMAN  TIMESROMAN A fj/ͬwritEfORMATTED.BRAVO SwinehartJune 27, 1980 2:44 PM