File indexing completed on 2024-12-08 06:46:01

0001 /*
0002     SPDX-FileCopyrightText: 2006 Ian Wadham <iandw.au@gmail.com>
0003     SPDX-FileCopyrightText: 2009 Ian Wadham <iandw.au@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef KGRGAMEIO_H
0009 #define KGRGAMEIO_H
0010 
0011 #include "kgrglobals.h"
0012 
0013 #include <QFile>
0014 
0015 class QWidget;
0016 
0017 /// Return values from I/O operations.
0018 enum IOStatus {OK, NotFound, NoRead, NoWrite, UnexpectedEOF};
0019 
0020 /**
0021  * The KGrGameIO class handles I/O for text-files containing KGoldrunner games
0022  * and levels.  The games and levels that are released with KGoldrunner are
0023  * installed in a "System" directory (e.g. .../share/apps/kgoldrunner/system).
0024  * Those that the user composes or edits are stored in a "User" directory
0025  * (e.g. $HOME/.kde/share/apps/kgoldrunner/user).
0026  *
0027  * The class handles files in either KGoldrunner 2 format or KGoldrunner 3
0028  * format.  In KGoldrunner 2 format, the data for games is in file "games.dat"
0029  * and the data for levels is in multiple files "levels/<prefix><nnn>.grl".
0030  * Each level-file has at least one line containing codes for the level's layout
0031  * and can have optional extra lines containing a level name and a hint.  In
0032  * KGoldrunner 3 format, each game is in one file called "game_<prefix>.txt",
0033  * containing the game-data and the data for all levels.  The data formats are
0034  * the same as for KGoldrunner 2, except that the first character of each line
0035  * indicates what kind of data is in the rest of the line.  "G" = game data,
0036  * "L" = start of level data, with the level-number following the "L", and
0037  * " " = level data, with line 1 being the layout codes, line 2 (optional) the
0038  * level name and lines >2 (optional) the hint. 
0039  * 
0040  * This class is used by the game and its editor and is also used by a utility
0041  * program that finds game names, level names and hints and rewrites them in
0042  * a format suitable for extracting strings that KDE translators can use.
0043  *
0044  * @short   KGoldrunner Game-File IO
0045  */
0046 
0047 class KGrGameIO : public QObject
0048 {
0049     Q_OBJECT
0050 public:
0051     /**
0052      * Default constructor.
0053      *
0054      * @param pView    The view or widget used as a parent for error messages.
0055      */
0056     explicit KGrGameIO (QWidget * pView);
0057 
0058     /**
0059      * Find and read data for games, into a list of KGrGameData structures.
0060      */
0061     IOStatus fetchGameListData (const Owner o, const QString & dir,
0062                                 QList<KGrGameData *> & gameList,
0063                                 QString & filePath);
0064 
0065     /**
0066      * Find and read data for a level of a game.  Can display error messages.
0067      */
0068     bool readLevelData (const QString & dir, const QString & prefix,
0069                         const int levelNo, KGrLevelData & d);
0070 
0071     /**
0072      * Find and read data for a level of a game, into a KGrLevelData structure.
0073      * Returns an OK or error status, but does not display error messages.
0074      */
0075     IOStatus fetchLevelData    (const QString & dir, const QString & prefix,
0076                                 const int level, KGrLevelData & d,
0077                                 QString & filePath);
0078 
0079     /*
0080      * Rename a file, first removing any existing file that has the target name.
0081      */
0082     static bool safeRename (QWidget * theView, const QString & oldName,
0083                             const QString & newName);
0084 
0085 private:
0086     QWidget *           view;
0087 
0088     QFile       openFile;
0089 
0090     QString     getFilePath (const QString & dir,
0091                                 const QString & prefix, const int level);
0092     char        getALine (const bool kgr3, QByteArray & line);
0093     QByteArray      removeNewline (const QByteArray & line);
0094     KGrGameData *   initGameData (Owner o);
0095 };
0096 
0097 #endif // KGRGAMEIO_H