2022-12-11 19:47:43 +08:00
|
|
|
#include "freeprojectsetformat.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QXmlStreamReader>
|
2022-12-16 14:41:24 +08:00
|
|
|
#include <QXmlStreamWriter>
|
2022-12-11 19:47:43 +08:00
|
|
|
|
|
|
|
QList<POJProblem> importFreeProblemSet(const QString &filename)
|
|
|
|
{
|
|
|
|
QFile file(filename);
|
|
|
|
QList<POJProblem> problems;
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
return problems;
|
|
|
|
QXmlStreamReader xml;
|
|
|
|
xml.setDevice(&file);
|
|
|
|
POJProblem currentProblem;
|
|
|
|
POJProblemCase currentCase;
|
|
|
|
QString currentEleName;
|
|
|
|
while(!xml.atEnd()) {
|
|
|
|
xml.readNext();
|
|
|
|
switch (xml.tokenType()) {
|
|
|
|
case QXmlStreamReader::TokenType::StartElement:
|
|
|
|
currentEleName = xml.name().toString();
|
|
|
|
if (xml.name()=="item") {
|
|
|
|
currentProblem=std::make_shared<OJProblem>();
|
|
|
|
} else if (currentProblem &&
|
2022-12-11 21:46:51 +08:00
|
|
|
(xml.name()=="test_input")) {
|
2022-12-11 19:47:43 +08:00
|
|
|
currentCase = std::make_shared<OJProblemCase>();
|
|
|
|
foreach (const QXmlStreamAttribute& attr, xml.attributes()) {
|
|
|
|
if (attr.name() == "name") {
|
2022-12-11 21:46:51 +08:00
|
|
|
currentCase->name = attr.value().toString().trimmed();
|
2022-12-11 19:47:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currentCase->name = QObject::tr("Problem Case %1").arg(currentProblem->cases.count()+1);
|
|
|
|
} else if (currentProblem &&
|
|
|
|
xml.name()=="time_limit") {
|
2022-12-13 08:49:20 +08:00
|
|
|
currentEleName = xml.name().toString();
|
2022-12-11 19:47:43 +08:00
|
|
|
foreach (const QXmlStreamAttribute& attr, xml.attributes()) {
|
2022-12-13 08:49:20 +08:00
|
|
|
if (attr.name() == "unit") {
|
|
|
|
if (attr.value()=="ms")
|
|
|
|
currentProblem->timeLimitUnit = ProblemTimeLimitUnit::Milliseconds;
|
|
|
|
else if (attr.value()=="s")
|
|
|
|
currentProblem->timeLimitUnit = ProblemTimeLimitUnit::Seconds;
|
2022-12-11 19:47:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (currentProblem &&
|
|
|
|
xml.name()=="memory_limit") {
|
2022-12-13 08:49:20 +08:00
|
|
|
currentEleName = xml.name().toString();
|
2022-12-11 19:47:43 +08:00
|
|
|
foreach (const QXmlStreamAttribute& attr, xml.attributes()) {
|
2022-12-13 08:49:20 +08:00
|
|
|
if (attr.name() == "unit") {
|
|
|
|
if (attr.value()=="mb")
|
|
|
|
currentProblem->memoryLimitUnit = ProblemMemoryLimitUnit::MB;
|
|
|
|
else if (attr.value()=="kb")
|
|
|
|
currentProblem->memoryLimitUnit = ProblemMemoryLimitUnit::KB;
|
|
|
|
else if (attr.value()=="gb")
|
|
|
|
currentProblem->memoryLimitUnit = ProblemMemoryLimitUnit::GB;
|
2022-12-11 19:47:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QXmlStreamReader::TokenType::EndElement:
|
|
|
|
currentEleName.clear();
|
|
|
|
if (currentProblem && xml.name()=="item") {
|
|
|
|
problems.append(currentProblem);
|
|
|
|
currentProblem.reset();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QXmlStreamReader::TokenType::Characters:
|
2022-12-11 21:46:51 +08:00
|
|
|
if (currentCase && currentProblem && currentEleName=="test_input") {
|
2022-12-11 19:47:43 +08:00
|
|
|
currentCase->input = xml.text().toString();
|
2022-12-11 21:46:51 +08:00
|
|
|
} else if (currentCase && currentProblem && currentEleName=="test_output" ) {
|
2022-12-11 19:47:43 +08:00
|
|
|
currentCase->expected = xml.text().toString();
|
|
|
|
currentProblem->cases.append(currentCase);
|
|
|
|
currentCase.reset();
|
|
|
|
} else if (currentProblem && currentEleName=="description") {
|
|
|
|
currentProblem->description = xml.text().toString();
|
|
|
|
} else if (currentProblem && currentEleName=="hint") {
|
|
|
|
currentProblem->hint = xml.text().toString();
|
|
|
|
} else if (currentProblem && currentEleName=="title") {
|
2022-12-13 08:49:20 +08:00
|
|
|
currentProblem->name = xml.text().toString().trimmed().replace(" "," ");
|
2022-12-11 21:46:51 +08:00
|
|
|
} else if (currentProblem && currentEleName=="url") {
|
|
|
|
currentProblem->url = xml.text().toString().trimmed();
|
2022-12-11 19:47:43 +08:00
|
|
|
} else if (currentProblem && currentEleName=="time_limit") {
|
|
|
|
currentProblem->timeLimit = xml.text().toInt();
|
|
|
|
} else if (currentProblem && currentEleName=="memory_limit") {
|
|
|
|
currentProblem->memoryLimit = xml.text().toInt();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return problems;
|
|
|
|
}
|
2022-12-16 14:41:24 +08:00
|
|
|
|
|
|
|
void exportFreeProblemSet(const QList<POJProblem> &problems, const QString &filename)
|
|
|
|
{
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QFile::WriteOnly|QFile::Truncate))
|
|
|
|
return;
|
|
|
|
QXmlStreamWriter writer(&file);
|
|
|
|
writer.setAutoFormatting(true);
|
|
|
|
writer.writeStartDocument();
|
|
|
|
writer.writeEndDocument();
|
|
|
|
}
|