Esempio n. 1
0
void JudgingThread::specialJudge(const QString &fileName)
{
    if (! QFileInfo(inputFile).exists()) {
        score = 0;
        result = FileError;
        message = tr("Cannot find standard input file");
        return;
    }
    
    if (! QFileInfo(fileName).exists()) {
        score = 0;
        result = FileError;
        message = tr("Cannot find contestant\'s output file");
        return;
    }
    
    if (! QFileInfo(outputFile).exists()) {
        score = 0;
        result = FileError;
        message = tr("Cannot find standard output file");
        return;
    }
    
    QProcess *judge = new QProcess(this);
    QStringList arguments;
    arguments << inputFile << fileName << outputFile << QString("%1").arg(fullScore);
    arguments << workingDirectory + "_score";
    arguments << workingDirectory + "_message";
    judge->start(Settings::dataPath() + task->getSpecialJudge(), arguments);
    if (! judge->waitForStarted(-1)) {
        score = 0;
        result = InvalidSpecialJudge;
        delete judge;
        return;
    }
    
    QElapsedTimer timer;
    timer.start();
    bool flag = false;
    while (timer.elapsed() < specialJudgeTimeLimit) {
        if (judge->state() != QProcess::Running) {
            flag = true;
            break;
        }
        QCoreApplication::processEvents();
        if (stopJudging) {
            judge->kill();
            delete judge;
            return;
        }
        msleep(10);
    }
    if (! flag) {
        judge->kill();
        score = 0;
        result = SpecialJudgeTimeLimitExceeded;
        delete judge;
        return;
    } else
        if (judge->exitCode() != 0) {
            score = 0;
            result = SpecialJudgeRunTimeError;
            delete judge;
            return;
        }
    delete judge;
    
    QFile scoreFile(workingDirectory + "_score");
    if (! scoreFile.open(QFile::ReadOnly)) {
        score = 0;
        result = InvalidSpecialJudge;
        return;
    }
    
    QTextStream scoreStream(&scoreFile);
    scoreStream >> score;
    if (scoreStream.status() == QTextStream::ReadCorruptData) {
        score = 0;
        result = InvalidSpecialJudge;
        return;
    }
    scoreFile.close();
    
    if (score < 0) {
        score = 0;
        result = InvalidSpecialJudge;
        return;
    }
    
    QFile messageFile(workingDirectory + "_message");
    if (messageFile.open(QFile::ReadOnly)) {
        QTextStream messageStream(&messageFile);
        message = messageStream.readAll();
        messageFile.close();
    }
    
    if (score == 0) result = WrongAnswer;
    if (0 < score && score < fullScore) result = PartlyCorrect;
    if (score >= fullScore) result = CorrectAnswer;
    
    scoreFile.remove();
    messageFile.remove();
}
Esempio n. 2
0
bool MailFolder::compact(unsigned level)
{
    // sync first so that you are sure the database is ok
    sync();

    if ( level ) {
        qDebug("Folder compressing isn't yet available.");
    }

    QCString mboxInfo;
    mboxInfo = "From - " + QDateTime::currentDateTime().toString() + "\r\n";

    QFile descriptorFile( getDescriptorFileName() );
    if ( !descriptorFile.open(IO_ReadOnly) )
        return false;

    QFile newDescriptorFile( getDescriptorFileName()+"new" );
    if ( !newDescriptorFile.open(IO_WriteOnly) )
        return false;

    QFile messageFile( getMessagesFileName() );
    if ( !messageFile.open(IO_ReadOnly) )
        return false;

    QFile newMessageFile( getMessagesFileName()+"new" );
    if ( !newMessageFile.open(IO_WriteOnly) )
        return false;

    IndexClass * index = 0L;
    QByteArray buffer;

    for (QDictIterator<IndexClass> it(indexCollection); it.current(); ++it) {
        index = it.current();

        if ( !index->getDescriptorLength() ||
                !index->getUniblockLength() ) {
            qDebug("The index file seems to be broken :(");
            indexCollection.remove( index->getID() );
            continue;
        }

        // read the descriptor
        buffer.resize( index->getDescriptorLength() );
        descriptorFile.at( index->getDescriptorOffset() );
        descriptorFile.readBlock(buffer.data(), buffer.size());

        // write the descriptor
        index->setDescriptorOffset( newDescriptorFile.at() );
        newDescriptorFile.writeBlock( buffer );

        // read the message
        buffer.resize( index->getUniblockLength() );
        messageFile.at( index->getUniblockOffset() );
        messageFile.readBlock(buffer.data(), buffer.size());

        // write the message
        // The MBOX line isn't right but the line isn't used too.
        // So, I decided to just store only the current date.
        newMessageFile.writeBlock((const char *)mboxInfo, mboxInfo.length());
        index->setUniblockOffset(newMessageFile.at(), true);

        newMessageFile.writeBlock( buffer );
        newMessageFile.writeBlock("\r\n", 2);
    }

    descriptorFile.close();
    newDescriptorFile.close();
    messageFile.close();
    newMessageFile.close();

    buffer.truncate(0);

    // replace the old files
    descriptorFile.remove();
    messageFile.remove();

    QDir folder( getStorageDevice() );
    folder.rename( newDescriptorFile.name(), getDescriptorFileName(), true );
    folder.rename( newMessageFile.name(), getMessagesFileName(), true );

    saveIndex();

    return true;
}