// IfsSwapUtil.bcpl -- misc swappable utility routines
// Copyright Xerox Corporation 1979, 1980, 1981

// Last modified November 14, 1981  2:22 PM by Taft

get "Ifs.decl"
get "CmdScan.decl"

external
[
// outgoing procedures
Wss; Ws; Wns; ExpandTemplate; DefaultTemplate; AbortCmd; RetryPhrase

// incoming procedures
Puts; PutTemplate; PutNum
CreateStringStream; ExtractSubstring; Closes
CmdError; BackupPhrase; BeginDefaultPhrase; EndDefaultPhrase
Errors; DefaultArgs; SysAllocate; SysFree; CallWithArgVec

// incoming statics
dsp
]

//----------------------------------------------------------------------------
let Wss(s, str) be
//----------------------------------------------------------------------------
 for i=1 to str>>String.length do Puts(s, str>>String.char↑i)

//----------------------------------------------------------------------------
and Ws(str) be Wss(dsp, str)
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
and Wns(s, num, wid, rdx; numargs na) be
//----------------------------------------------------------------------------
[
DefaultArgs(lv na, -2, 1, -10)

// Use unadvertised procedure in Template package.
// *** Beware:  the following 5 variables must be declared in this order.
let radix = (rdx gr 0? rdx, -rdx)
let width = wid
let signed = rdx ls 0
let double = false
let fill = $*s

PutNum(s, num, lv radix)
]

//----------------------------------------------------------------------------
and ExpandTemplate(template, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8,
 arg9, arg10) = valof
//----------------------------------------------------------------------------
// Calls PutTemplate on these arguments, and returns result as a String
// allocated from sysZone.
// template must expand to at most 255 characters, else a "fast stream
// error, tried to read or write beyond end of file" error will occur.
[
let tempString = SysAllocate(128)
let ss = CreateStringStream(tempString, 255)
PutTemplate(ss, template, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8,
 arg9, arg10)
Closes(ss)
let resultString = ExtractSubstring(tempString)
SysFree(tempString)
resultis resultString
]

//---------------------------------------------------------------------------
and DefaultTemplate(cs, string, arg1, nil, nil, nil, nil, nil, nil, nil,
    nil, nil; numargs na) be
//---------------------------------------------------------------------------
[
BeginDefaultPhrase(cs)
CallWithArgVec(PutTemplate, lv cs, na)
EndDefaultPhrase(cs)
]

//----------------------------------------------------------------------------
and AbortCmd(cs, string; numargs na) be
//----------------------------------------------------------------------------
   [ CmdError(cs, (na gr 1? string, 0)); Errors(cs, ecCmdDestroy) ]

//----------------------------------------------------------------------------
and RetryPhrase(cs, string; numargs na) be
//----------------------------------------------------------------------------
   [ CmdError(cs, (na gr 1? string, 0)); BackupPhrase(cs) ]