34 (
const Owner o,
const QString & dir, QList<KGrGameData *> & gameList,
40 QStringList files = directory.entryList (pattern, QDir::Files, QDir::Name);
44 bool kgr3Format = (files.count() > 0);
50 foreach (
const QString &filename, files) {
51 if (filename ==
"game_ende.txt") {
55 filePath = dir + filename;
60 openFile.setFileName (filePath);
63 if (! openFile.exists()) {
68 if (! openFile.open (QIODevice::ReadOnly)) {
77 c = getALine (kgr3Format, textLine);
79 while ((c !=
'G') && (c !=
'\0')) {
80 c = getALine (kgr3Format, textLine);
90 if (kgr3Format && (c ==
'L')) {
94 QList<QByteArray> fields = textLine.split (
' ');
95 g->
nLevels = fields.at (0).toInt();
96 g->
rules = fields.at (1).at (0);
103 g->
skill = fields.at (3).at (0);
104 c = getALine (kgr3Format, textLine);
106 gameName = removeNewline (textLine);
107 g->
name = i18n (gameName.constData());
114 n = textLine.indexOf (
' ', n) + 1;
115 n = textLine.indexOf (
' ', n) + 1;
116 n = textLine.indexOf (
' ', n) + 1;
117 gameName = removeNewline (textLine.right (textLine.size() - n));
118 g->
name = i18n (gameName.constData());
125 c = getALine (kgr3Format, textLine);
127 (kgr3Format && (c ==
'L')) ||
129 (textLine.at (0) >=
'0') && (textLine.at (0) <=
'9'))) {
132 g->
about.append (textLine);
137 if ((! kgr3Format) && (c !=
'\0')) {
138 filePath = dir + filename;
139 g = initGameData (o);
152 const QString & prefix,
155 kDebug() <<
"dir" << dir <<
"Level" << prefix << levelNo;
158 (dir, prefix, levelNo, d, filePath);
162 i18n (
"Cannot find file '%1'.", filePath));
167 i18n (
"Cannot open file '%1' for read-only.", filePath));
171 i18n (
"Reached end of file '%1' without finding level data.",
182 (
const QString & dir,
const QString & prefix,
185 filePath = getFilePath (dir, prefix, level);
194 openFile.setFileName (filePath);
197 if (! openFile.exists()) {
202 if (! openFile.open (QIODevice::ReadOnly)) {
211 bool kgr3Format = (filePath.endsWith (
".txt"));
215 while ((c = getALine (kgr3Format, textLine)) !=
'\0') {
216 if ((c ==
'L') && (textLine.left (3).toInt() == level)) {
227 if ((c = getALine (kgr3Format, textLine)) ==
' ') {
229 d.
layout = removeNewline (textLine);
232 if ((c = getALine (kgr3Format, textLine)) ==
' ') {
233 d.
name = removeNewline (textLine);
236 while ((c = getALine (kgr3Format, textLine)) ==
' ') {
237 d.
hint.append (textLine);
251 QString KGrGameIO::getFilePath
252 (
const QString & dir,
const QString & prefix,
const int level)
254 QString filePath = ((level == 0) ?
"ende" : prefix);
255 filePath = dir +
"game_" + filePath +
".txt";
256 QFile test (filePath);
266 filePath = dir +
"levels/level000.grl";
269 QString num = QString::number (level).rightJustified (3,
'0');
270 filePath = dir +
"levels/" + prefix + num +
".grl";
276 char KGrGameIO::getALine (
const bool kgr3, QByteArray & line)
280 while (openFile.getChar (&c)) {
281 line = line.append (c);
288 if (line.size() <= 0) {
294 if (line.startsWith (
"// ")) {
295 line = line.right (line.size() - 3);
299 if (line.startsWith (
" i18n(\"")) {
300 line =
' ' + line.right (line.size() - 7);
302 else if (line.startsWith (
" NOTi18n(\"")) {
303 line =
' ' + line.right (line.size() - 10);
305 else if (line.startsWith (
" \"")) {
306 line =
' ' + line.right (line.size() - 2);
308 if (line.endsWith (
"\");\n")) {
309 line = line.left (line.size() - 4) +
'\n';
311 else if (line.endsWith (
"\\n\"\n")) {
312 line = line.left (line.size() - 4) +
'\n';
314 else if (line.endsWith (
"\"\n")) {
315 line = line.left (line.size() - 2);
321 line = line.right (line.size() - 1);
330 QByteArray KGrGameIO::removeNewline (
const QByteArray & line)
332 int len = line.size();
333 if ((len > 0) && (line.endsWith (
'\n'))) {
334 return (line.left (len -1));
357 const QString & newName)
359 QFile newFile (newName);
360 if (newFile.exists()) {
365 if (! newFile.remove()) {
367 i18n (
"Cannot delete previous version of file '%1'.", newName));
371 QFile oldFile (oldName);
372 if (! oldFile.rename (newName)) {
374 i18n (
"Cannot rename file '%1' to '%2'.", oldName, newName));
380 #include "kgrgameio.moc"
QByteArray hint
Level hint (optional).
int height
Height of grid, in cells.
int height
Height of grid, in cells.
QString prefix
Game's filename prefix.
KGrGameIO(QWidget *pView)
Default constructor.
Owner owner
Owner of the game: "System" or "User".
KGrGameData structure: contains attributes of a KGoldrunner game.
IOStatus fetchGameListData(const Owner o, const QString &dir, QList< KGrGameData * > &gameList, QString &filePath)
Find and read data for games, into a list of KGrGameData structures.
int width
Width of grid, in cells.
QByteArray layout
Codes for the level layout (mandatory).
IOStatus fetchLevelData(const QString &dir, const QString &prefix, const int level, KGrLevelData &d, QString &filePath)
Find and read data for a level of a game, into a KGrLevelData structure.
char rules
Game's rules: KGoldrunner or Traditional.
bool readLevelData(const QString &dir, const QString &prefix, const int levelNo, KGrLevelData &d)
Find and read data for a level of a game.
QByteArray about
Optional info about game (untranslated).
static void information(QWidget *parent, const QString &caption, const QString &text, const QString &dontShowAgain=QString())
KGrLevelData structure: contains attributes of a KGoldrunner level.
int nLevels
Number of levels in the game.
IOStatus
Return values from I/O operations.
int width
Width of grid, in cells.
QString name
Name of game (translated, if System game).
char skill
Game's skill: Tutorial, Normal or Champion.
static bool safeRename(QWidget *theView, const QString &oldName, const QString &newName)
QByteArray name
Level name (optional).