1 /*
2  * scriptfile.h
3  * Copyright 2019, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
4  *
5  * This file is part of Tiled.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include <QObject>
24 
25 #include <memory>
26 
27 class QFileDevice;
28 class QTextStream;
29 
30 namespace Tiled {
31 
32 class ScriptBinaryFile : public QObject
33 {
34     Q_OBJECT
35 
36     Q_PROPERTY(QString filePath READ filePath)
37     Q_PROPERTY(bool atEof READ atEof)
38     Q_PROPERTY(qint64 size READ size)
39     Q_PROPERTY(qint64 pos READ pos)
40 
41 public:
42     enum OpenMode {
43         ReadOnly = 1,
44         WriteOnly = 2,
45         ReadWrite = ReadOnly | WriteOnly
46     };
47     Q_ENUM(OpenMode)
48 
49     Q_INVOKABLE ScriptBinaryFile(); // workaround for Qt issue
50     Q_INVOKABLE ScriptBinaryFile(const QString &filePath,
51                                  OpenMode mode = ReadOnly);
52     ~ScriptBinaryFile() override;
53 
54     QString filePath() const;
55     bool atEof() const;
56     qint64 size() const;
57     qint64 pos() const;
58 
59     Q_INVOKABLE void resize(qint64 size);
60     Q_INVOKABLE void seek(qint64 pos);
61     Q_INVOKABLE QByteArray read(qint64 size);
62     Q_INVOKABLE QByteArray readAll();
63     Q_INVOKABLE void write(const QByteArray &data);
64     Q_INVOKABLE void commit();
65     Q_INVOKABLE void close();
66 
67 private:
68     bool checkForClosed() const;
69 
70     std::unique_ptr<QFileDevice> m_file;
71 };
72 
73 class ScriptTextFile : public QObject
74 {
75     Q_OBJECT
76 
77     Q_PROPERTY(QString filePath READ filePath)
78     Q_PROPERTY(bool atEof READ atEof)
79     Q_PROPERTY(QString codec READ codec WRITE setCodec)
80 
81 public:
82     enum OpenMode {
83         ReadOnly = 1,
84         WriteOnly = 2,
85         ReadWrite = ReadOnly | WriteOnly,
86         Append = 4
87     };
88     Q_ENUM(OpenMode)
89 
90     Q_INVOKABLE ScriptTextFile();   // workaround for Qt issue
91     Q_INVOKABLE ScriptTextFile(const QString &filePath,
92                                OpenMode mode = ReadOnly);
93     ~ScriptTextFile() override;
94 
95     QString filePath() const;
96 
97     QString codec() const;
98     void setCodec(const QString &codec);
99 
100     Q_INVOKABLE QString readLine();
101     Q_INVOKABLE QString readAll();
102     bool atEof() const;
103     Q_INVOKABLE void truncate();
104     Q_INVOKABLE void write(const QString &string);
105     Q_INVOKABLE void writeLine(const QString &string);
106     Q_INVOKABLE void commit();
107     Q_INVOKABLE void close();
108 
109 private:
110     bool checkForClosed() const;
111 
112     std::unique_ptr<QFileDevice> m_file;
113     std::unique_ptr<QTextStream> m_stream;
114 };
115 
116 } // namespace Tiled
117 
118 Q_DECLARE_METATYPE(Tiled::ScriptBinaryFile*)
119 Q_DECLARE_METATYPE(Tiled::ScriptTextFile*)
120