qcross/qcross/chighscore.cpp

90 lines
2.9 KiB
C++

/***************************************************************************
* Copyright (C) 2008 by Oliver Groß *
* z.o.gross@gmx.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "chighscore.h"
#include "constants.h"
#include <libqnono/ccrosspackage.h>
#include <QDir>
#include <QFile>
#include <QDataStream>
namespace qcross {
CHighscore::CHighscore(int initialSize) : QList<quint32>() {
for (int i = 0; i < initialSize; i++)
append(0);
}
void CHighscore::setPackageIdentifier(QString PackageIdentifier) {
m_PackageIdentifier = libqnono::CCrossPackage::identifierFromPath(PackageIdentifier);
}
bool CHighscore::open() {
if (m_PackageIdentifier.isEmpty())
return false;
QString fileName = (QCROSS_STRING_DATAPATH + QDir::separator() + m_PackageIdentifier + ".hsc");
qDebug("reading highscore file: %s", fileName.toAscii().data());
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
return false;
clear();
QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_0);
quint32 intBuffer = 0;
in >> intBuffer;
for (quint32 i = 0; i < intBuffer; i++) {
append(0);
in >> last();
}
return true;
}
bool CHighscore::save() {
if (m_PackageIdentifier.isEmpty())
return false;
QString fileName = (QCROSS_STRING_DATAPATH + QDir::separator() + m_PackageIdentifier + ".hsc");
qDebug("saving highcore file: %s", fileName.toAscii().data());
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
return false;
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_0);
out << size();
foreach (quint32 i, *this)
out << i;
return true;
}
}