예제 #1
0
bool OssnFileFormat::save(const OstProto::SessionContent &session,
        const QString fileName, QString &error)
{
    OstProto::FileContent content;

    if (!session.IsInitialized())
        goto _session_not_init;

    content.mutable_matter()->mutable_session()->CopyFrom(session);
    Q_ASSERT(content.IsInitialized());

    return NativeFileFormat::save(OstProto::kSessionFileType, content,
                                  fileName, error);

_session_not_init:
    error = QString(tr("Internal Error: Session not initialized\n%1\n%2"))
                .arg(QString().fromStdString(
                            session.InitializationErrorString()))
                .arg(QString().fromStdString(session.DebugString()));
    goto _fail;
_fail:
    qDebug("%s", error.toAscii().constData());
    return false;
}
예제 #2
0
bool FileFormat::saveStreams(const OstProto::StreamConfigList streams,
                             const QString fileName, QString &error)
{
    OstProto::FileMagic magic;
    OstProto::FileMeta meta;
    OstProto::FileContent content;
    OstProto::FileChecksum cksum;
    QFile file(fileName);
    int metaSize, contentSize;
    int contentOffset, cksumOffset;
    QByteArray buf;
    quint32 calcCksum;

    magic.set_value(kFileMagicValue);
    Q_ASSERT(magic.IsInitialized());

    cksum.set_value(0);
    Q_ASSERT(cksum.IsInitialized());

    initFileMetaData(*(meta.mutable_data()));
    meta.mutable_data()->set_file_type(OstProto::kStreamsFileType);
    Q_ASSERT(meta.IsInitialized());

    if (!streams.IsInitialized())
        goto _stream_not_init;

    content.mutable_matter()->mutable_streams()->CopyFrom(streams);
    Q_ASSERT(content.IsInitialized());

    metaSize = meta.ByteSize();
    contentSize = content.ByteSize();
    contentOffset = kFileMetaDataOffset + metaSize;
    cksumOffset = contentOffset + contentSize;

    Q_ASSERT(magic.ByteSize() == kFileMagicSize);
    Q_ASSERT(cksum.ByteSize() == kFileChecksumSize);
    buf.resize(kFileMagicSize + metaSize + contentSize + kFileChecksumSize);

    // Serialize everything
    if (!magic.SerializeToArray((void*) (buf.data() + kFileMagicOffset),
                                kFileMagicSize))
    {
        goto _magic_serialize_fail;
    }

    if (!meta.SerializeToArray((void*) (buf.data() + kFileMetaDataOffset),
                               metaSize))
    {
        goto _meta_serialize_fail;
    }

    if (!content.SerializeToArray((void*) (buf.data() + contentOffset),
                                  contentSize))
    {
        goto _content_serialize_fail;
    }

    if (!cksum.SerializeToArray((void*) (buf.data() + cksumOffset),
                                kFileChecksumSize))
    {
        goto _zero_cksum_serialize_fail;
    }

    emit status("Calculating checksum...");

    // Calculate and write checksum
    calcCksum = checksumCrc32C((quint8*)buf.constData(), buf.size());
    cksum.set_value(calcCksum);
    if (!cksum.SerializeToArray(
                (void*) (buf.data() + cksumOffset),
                kFileChecksumSize))
    {
        goto _cksum_serialize_fail;
    }

    qDebug("Writing %d bytes", buf.size());
    //qDebug("%s", QString(buf.toHex()).toAscii().constData());

    emit status("Writing to disk...");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
        goto _open_fail;

    if (file.write(buf) < 0)
        goto _write_fail;

    file.close();

    return true;

_write_fail:
    error = QString(tr("Error writing to %1")).arg(fileName);
    goto _fail;
_open_fail:
    error = QString(tr("Error opening %1 (Error Code = %2)"))
            .arg(fileName)
            .arg(file.error());
    goto _fail;
_cksum_serialize_fail:
    error = QString(tr("Internal Error: Checksum Serialize failed\n%1\n%2"))
            .arg(QString().fromStdString(
                     cksum.InitializationErrorString()))
            .arg(QString().fromStdString(cksum.DebugString()));
    goto _fail;
_zero_cksum_serialize_fail:
    error = QString(tr("Internal Eror: Zero Checksum Serialize failed\n%1\n%2"))
            .arg(QString().fromStdString(
                     cksum.InitializationErrorString()))
            .arg(QString().fromStdString(cksum.DebugString()));
    goto _fail;
_content_serialize_fail:
    error = QString(tr("Internal Error: Content Serialize failed\n%1\n%2"))
            .arg(QString().fromStdString(
                     content.InitializationErrorString()))
            .arg(QString().fromStdString(content.DebugString()));
    goto _fail;
_meta_serialize_fail:
    error = QString(tr("Internal Error: Meta Data Serialize failed\n%1\n%2"))
            .arg(QString().fromStdString(
                     meta.InitializationErrorString()))
            .arg(QString().fromStdString(meta.DebugString()));
    goto _fail;
_magic_serialize_fail:
    error = QString(tr("Internal Error: Magic Serialize failed\n%1\n%2"))
            .arg(QString().fromStdString(
                     magic.InitializationErrorString()))
            .arg(QString().fromStdString(magic.DebugString()));
    goto _fail;
_stream_not_init:
    error = QString(tr("Internal Error: Streams not initialized\n%1\n%2"))
            .arg(QString().fromStdString(
                     streams.InitializationErrorString()))
            .arg(QString().fromStdString(streams.DebugString()));
    goto _fail;
_fail:
    qDebug("%s", error.toAscii().constData());
    return false;
}