///////////////////////////////////////////////////////////////////////////////
// WriteObject -- template class that writes an object to disk (ie -- report,
//      db, etc) either encrypted or unencrypted.
//      The only requrement on the object is that it is typed serializable.
//      errorMsg is a number that can be passed to iUserString to indicate an
//      appropriate error message if the object fails to load
//
// 10/30 -- this function has been expanded to take two objects -- a header and
//      an object. Both are typed serializable, but the header can be NULL if
//      none is desired.
///////////////////////////////////////////////////////////////////////////////
static void WriteObject(const TCHAR*                 filename,
                        const iTypedSerializable*    pObjHeader,
                        const iTypedSerializable&    obj,
                        cFileHeader&                 fileHeader,
                        bool                         bEncrypt,
                        const cElGamalSigPrivateKey* pPrivateKey)
{
    cDebug d("WriteObject");
    d.TraceDebug(_T("Writing %s to file %s\n"), obj.GetType().AsString(), filename);

    ASSERT(pPrivateKey || (!bEncrypt));

    cFileArchive arch;

    if (!cFileUtil::IsRegularFile(filename) && cFileUtil::FileExists(filename))
        throw eArchiveNotRegularFile(filename);

    try
    {
        arch.OpenReadWrite(filename);
    }
    catch (eArchive&)
    {
        // probably better to rethrow this as a write failed exception
        throw eArchiveWrite(filename, iFSServices::GetInstance()->GetErrString());
    }

    WriteObjectToArchive(arch, filename, pObjHeader, obj, fileHeader, bEncrypt, pPrivateKey);

    arch.Close();
}
/// General

TSS_REGISTER_ERROR( eErrorGeneral(),    _T("General Error") );
TSS_REGISTER_ERROR( eOpen(),            _T("File could not be opened.") );
TSS_REGISTER_ERROR( eOpenRead(),        _T("File could not be opened for reading.") );
TSS_REGISTER_ERROR( eOpenWrite(),       _T("File could not be opened for writing.") );
TSS_REGISTER_ERROR( eBadModeSwitch(),   _T("Unknown mode specified.") );
TSS_REGISTER_ERROR( eBadCmdLine(),      _T("Command line error.") );


/// Archive 

TSS_REGISTER_ERROR( eArchive(),         _T("Archive error.") )
TSS_REGISTER_ERROR( eArchiveOpen(),     _T("File could not be opened.") )
TSS_REGISTER_ERROR( eArchiveWrite(),    _T("File could not be written.") )
TSS_REGISTER_ERROR( eArchiveRead(),     _T("File could not be read.") )
TSS_REGISTER_ERROR( eArchiveEOF(),      _T("End of file reached.") )
TSS_REGISTER_ERROR( eArchiveSeek(),     _T("File seek failed.") )
TSS_REGISTER_ERROR( eArchiveMemmap(),   _T("Memory mapped archive file invalid.") )
TSS_REGISTER_ERROR( eArchiveOutOfMem(), _T("Archive ran out of memory.") )
TSS_REGISTER_ERROR( eArchiveInvalidOp(),_T("Archive logic error.") )
TSS_REGISTER_ERROR( eArchiveFormat(),   _T("Archive file format invalid.") )
TSS_REGISTER_ERROR( eArchiveNotRegularFile(), _T("File is not a regular file.") )
TSS_REGISTER_ERROR( eArchiveCrypto(),   _T("File could not be decrypted.") )
TSS_REGISTER_ERROR( eArchiveStringTooLong(),    _T("String was too long.") )


/// File

TSS_REGISTER_ERROR( eFile(),            _T("File error.") )