1 /*
2     SPDX-FileCopyrightText: 2006 Ian Wadham <iandw.au@gmail.com>
3     SPDX-FileCopyrightText: 2009 Ian Wadham <iandw.au@gmail.com>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #ifndef KGRGAMEIO_H
9 #define KGRGAMEIO_H
10 
11 #include "kgrglobals.h"
12 
13 #include <QFile>
14 
15 class QWidget;
16 
17 /// Return values from I/O operations.
18 enum IOStatus {OK, NotFound, NoRead, NoWrite, UnexpectedEOF};
19 
20 /**
21  * The KGrGameIO class handles I/O for text-files containing KGoldrunner games
22  * and levels.  The games and levels that are released with KGoldrunner are
23  * installed in a "System" directory (e.g. .../share/apps/kgoldrunner/system).
24  * Those that the user composes or edits are stored in a "User" directory
25  * (e.g. $HOME/.kde/share/apps/kgoldrunner/user).
26  *
27  * The class handles files in either KGoldrunner 2 format or KGoldrunner 3
28  * format.  In KGoldrunner 2 format, the data for games is in file "games.dat"
29  * and the data for levels is in multiple files "levels/<prefix><nnn>.grl".
30  * Each level-file has at least one line containing codes for the level's layout
31  * and can have optional extra lines containing a level name and a hint.  In
32  * KGoldrunner 3 format, each game is in one file called "game_<prefix>.txt",
33  * containing the game-data and the data for all levels.  The data formats are
34  * the same as for KGoldrunner 2, except that the first character of each line
35  * indicates what kind of data is in the rest of the line.  "G" = game data,
36  * "L" = start of level data, with the level-number following the "L", and
37  * " " = level data, with line 1 being the layout codes, line 2 (optional) the
38  * level name and lines >2 (optional) the hint.
39  *
40  * This class is used by the game and its editor and is also used by a utility
41  * program that finds game names, level names and hints and rewrites them in
42  * a format suitable for extracting strings that KDE translators can use.
43  *
44  * @short   KGoldrunner Game-File IO
45  */
46 
47 class KGrGameIO : public QObject
48 {
49     Q_OBJECT
50 public:
51     /**
52      * Default constructor.
53      *
54      * @param pView    The view or widget used as a parent for error messages.
55      */
56     explicit KGrGameIO (QWidget * pView);
57 
58     /**
59      * Find and read data for games, into a list of KGrGameData structures.
60      */
61     IOStatus fetchGameListData (const Owner o, const QString & dir,
62                                 QList<KGrGameData *> & gameList,
63                                 QString & filePath);
64 
65     /**
66      * Find and read data for a level of a game.  Can display error messages.
67      */
68     bool readLevelData (const QString & dir, const QString & prefix,
69                         const int levelNo, KGrLevelData & d);
70 
71     /**
72      * Find and read data for a level of a game, into a KGrLevelData structure.
73      * Returns an OK or error status, but does not display error messages.
74      */
75     IOStatus fetchLevelData    (const QString & dir, const QString & prefix,
76                                 const int level, KGrLevelData & d,
77                                 QString & filePath);
78 
79     /*
80      * Rename a file, first removing any existing file that has the target name.
81      */
82     static bool safeRename (QWidget * theView, const QString & oldName,
83                             const QString & newName);
84 
85 private:
86     QWidget *           view;
87 
88     QFile		openFile;
89 
90     QString		getFilePath (const QString & dir,
91                                 const QString & prefix, const int level);
92     char		getALine (const bool kgr3, QByteArray & line);
93     QByteArray		removeNewline (const QByteArray & line);
94     KGrGameData *	initGameData (Owner o);
95 };
96 
97 #endif // KGRGAMEIO_H
98