Skip to content

Commit

Permalink
Keep tournament's PGN output file open
Browse files Browse the repository at this point in the history
Opening and closing the PGN output file for each game was causing
problems on Windows with fast time controls. So let's just open the file
once and stream the PGN output there. This is also a lot more efficient.
The file is opened (created) when the first game is ready to be written
to disk.
  • Loading branch information
ilaripih committed Aug 30, 2015
1 parent f4d7e56 commit 9794b4d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
12 changes: 5 additions & 7 deletions projects/lib/src/pgngame.cpp
Expand Up @@ -253,10 +253,10 @@ static void writeTag(QTextStream& out, const QString& tag, const QString& value)
out << "[" << tag << " \"?\"]\n";
}

void PgnGame::write(QTextStream& out, PgnMode mode) const
bool PgnGame::write(QTextStream& out, PgnMode mode) const
{
if (m_tags.isEmpty())
return;
return false;

const QList< QPair<QString, QString> > tags = this->tags();
int maxTags = (mode == Verbose) ? tags.size() : 7;
Expand Down Expand Up @@ -307,20 +307,18 @@ void PgnGame::write(QTextStream& out, PgnMode mode) const
out << "\n" << str << "\n\n";
else
out << " " << str << "\n\n";

return true;
}

bool PgnGame::write(const QString& filename, PgnMode mode) const
{
if (m_tags.isEmpty())
return false;

QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Append))
return false;

QTextStream out(&file);
write(out, mode);
return true;
return write(out, mode);
}

bool PgnGame::isStandard() const
Expand Down
8 changes: 6 additions & 2 deletions projects/lib/src/pgngame.h
Expand Up @@ -107,8 +107,12 @@ class LIB_EXPORT PgnGame
* Returns true if any tags and/or moves were read.
*/
bool read(PgnStream& in, int maxMoves = INT_MAX - 1);
/*! Writes the game to a text stream. */
void write(QTextStream& out, PgnMode mode = Verbose) const;
/*!
* Writes the game to a text stream.
*
* Returns true if successfull.
*/
bool write(QTextStream& out, PgnMode mode = Verbose) const;
/*!
* Writes the game to a file.
* If the file already exists, the game will be appended
Expand Down
46 changes: 35 additions & 11 deletions projects/lib/src/tournament.cpp
Expand Up @@ -200,7 +200,11 @@ void Tournament::setOpeningDepth(int plies)

void Tournament::setPgnOutput(const QString& fileName, PgnGame::PgnMode mode)
{
m_pgnout = fileName;
if (fileName != m_pgnFile.fileName())
{
m_pgnFile.close();
m_pgnFile.setFileName(fileName);
}
m_pgnOutMode = mode;
}

Expand Down Expand Up @@ -302,6 +306,35 @@ void Tournament::startNextGame()
GameManager::ReusePlayers);
}

bool Tournament::writePgn(PgnGame* pgn, int gameNumber)
{
Q_ASSERT(pgn != 0);
Q_ASSERT(gameNumber > 0);

if (m_pgnFile.fileName().isEmpty())
return true;

if (!m_pgnFile.isOpen())
{
if (!m_pgnFile.open(QIODevice::WriteOnly | QIODevice::Append))
{
qWarning("Could not open PGN file %s",
qPrintable(m_pgnFile.fileName()));
return false;
}
m_pgnOut.setDevice(&m_pgnFile);
}

m_pgnGames[gameNumber] = *pgn;
while (m_pgnGames.contains(m_savedGameCount + 1))
{
PgnGame tmp = m_pgnGames.take(++m_savedGameCount);
tmp.write(m_pgnOut, m_pgnOutMode);
}

return true;
}

void Tournament::onGameStarted(ChessGame* game)
{
Q_ASSERT(game != 0);
Expand Down Expand Up @@ -350,16 +383,7 @@ void Tournament::onGameFinished(ChessGame* game)
break;
}

if (!m_pgnout.isEmpty())
{
m_pgnGames[gameNumber] = *pgn;
while (m_pgnGames.contains(m_savedGameCount + 1))
{
PgnGame tmp = m_pgnGames.take(++m_savedGameCount);
if (!tmp.write(m_pgnout, m_pgnOutMode))
qWarning("Can't write to PGN file %s", qPrintable(m_pgnout));
}
}
writePgn(pgn, gameNumber);
if (m_pgnCleanup)
delete pgn;

Expand Down
6 changes: 5 additions & 1 deletion projects/lib/src/tournament.h
Expand Up @@ -24,6 +24,8 @@
#include <QList>
#include <QVector>
#include <QMap>
#include <QFile>
#include <QTextStream>
#include "board/move.h"
#include "timecontrol.h"
#include "pgngame.h"
Expand Down Expand Up @@ -295,6 +297,7 @@ class LIB_EXPORT Tournament : public QObject

private slots:
void startNextGame();
bool writePgn(PgnGame* pgn, int gameNumber);
void onGameStarted(ChessGame* game);
void onGameFinished(ChessGame* game);
void onGameDestroyed(ChessGame* game);
Expand Down Expand Up @@ -331,7 +334,8 @@ class LIB_EXPORT Tournament : public QObject
GameAdjudicator m_adjudicator;
OpeningSuite* m_openingSuite;
Sprt* m_sprt;
QString m_pgnout;
QFile m_pgnFile;
QTextStream m_pgnOut;
QString m_startFen;
PgnGame::PgnMode m_pgnOutMode;
QPair<int, int> m_pair;
Expand Down

0 comments on commit 9794b4d

Please sign in to comment.