// IfsPrintError.bcpl
// Copyright Xerox Corporation 1979, 1981

// Last modified October 4, 1981  3:57 PM by Taft

get "Ifs.decl"
get "IfsSystemInfo.decl"

external
[
// outgoing procedures
IFSPrintError; LookupErrorCode; ErrorCodeToString
ErrCompareKey; ErrEntryLength

// incoming procedures
Usc; Allocate; Free; FreePointer; ReadRecLE
Wss; Closes; PutTemplate; CreateStringStream; ExtractSubstring

// incoming statics
errorTree; sysZone
]

//----------------------------------------------------------------------------
let IFSPrintError(stream, ec) be
//----------------------------------------------------------------------------
[
let errRec = LookupErrorCode(ec)
Wss(stream, lv errRec>>ErrRec.errorString)
FreePointer(lv errRec)
]

//----------------------------------------------------------------------------
and ErrorCodeToString(ec) = valof
//----------------------------------------------------------------------------
// Returns an error string which the caller must free.
[
let errRec = LookupErrorCode(ec)
let result = ExtractSubstring(lv errRec>>ErrRec.errorString)
Free(sysZone, errRec)
resultis result
]

//----------------------------------------------------------------------------
and LookupErrorCode(ec) = valof
//----------------------------------------------------------------------------
// Returns an ErrRec which the caller must free.
[
let errRec = 0
if errorTree ne 0 then errRec = ReadRecLE(errorTree, ec)
if errRec ne 0 then
   unless errRec>>ErrRec.ifsEc eq ec do
      FreePointer(lv errRec)
if errRec eq 0 then
   [  // Not found. Construct an ErrRec with an appropriate message.
   manifest lenErrRec = lenErrRecHeader + 20  // message < 40 chars long
   errRec = Allocate(sysZone, lenErrRec)
   errRec>>ErrRec.length = lenErrRec
   errRec>>ErrRec.ifsEc = ec
   errRec>>ErrRec.ftpEc = 0
   let ss = CreateStringStream(lv errRec>>ErrRec.errorString, 39)
   PutTemplate(ss, "Error $D (can't find error string)", ec)
   Closes(ss)
   ]
resultis errRec
]

//----------------------------------------------------------------------------
and ErrCompareKey(key, errRec) = Usc(key, errRec>>ErrRec.ifsEc)
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
and ErrEntryLength(errRec) = errRec>>ErrRec.length
//----------------------------------------------------------------------------