1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtSCriptTools module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qscriptscriptdata_p.h" |
41 | |
42 | #include <QtCore/qdatastream.h> |
43 | #include <QtCore/qstring.h> |
44 | #include <QtCore/qstringlist.h> |
45 | #include <QtCore/qshareddata.h> |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | /*! |
50 | \since 4.5 |
51 | \class QScriptScriptData |
52 | \internal |
53 | |
54 | \brief The QScriptScriptData class holds data associated with a script. |
55 | */ |
56 | |
57 | class QScriptScriptDataPrivate : public QSharedData |
58 | { |
59 | public: |
60 | QScriptScriptDataPrivate(); |
61 | ~QScriptScriptDataPrivate(); |
62 | |
63 | QString contents; |
64 | QString fileName; |
65 | int baseLineNumber; |
66 | QDateTime timeStamp; |
67 | }; |
68 | |
69 | QScriptScriptDataPrivate::QScriptScriptDataPrivate() |
70 | { |
71 | } |
72 | |
73 | QScriptScriptDataPrivate::~QScriptScriptDataPrivate() |
74 | { |
75 | } |
76 | |
77 | QScriptScriptData::QScriptScriptData() |
78 | : d_ptr(0) |
79 | { |
80 | } |
81 | |
82 | QScriptScriptData::QScriptScriptData(const QString &contents, const QString &fileName, |
83 | int baseLineNumber, const QDateTime &timeStamp) |
84 | : d_ptr(new QScriptScriptDataPrivate) |
85 | { |
86 | d_ptr->contents = contents; |
87 | d_ptr->fileName = fileName; |
88 | d_ptr->baseLineNumber = baseLineNumber; |
89 | if (timeStamp.isValid()) |
90 | d_ptr->timeStamp = timeStamp; |
91 | else |
92 | d_ptr->timeStamp = QDateTime::currentDateTime(); |
93 | d_ptr->ref.ref(); |
94 | } |
95 | |
96 | QScriptScriptData::QScriptScriptData(const QScriptScriptData &other) |
97 | : d_ptr(other.d_ptr.data()) |
98 | { |
99 | if (d_ptr) |
100 | d_ptr->ref.ref(); |
101 | } |
102 | |
103 | QScriptScriptData::~QScriptScriptData() |
104 | { |
105 | } |
106 | |
107 | QScriptScriptData &QScriptScriptData::operator=(const QScriptScriptData &other) |
108 | { |
109 | d_ptr.assign(other: other.d_ptr.data()); |
110 | return *this; |
111 | } |
112 | |
113 | QString QScriptScriptData::contents() const |
114 | { |
115 | Q_D(const QScriptScriptData); |
116 | if (!d) |
117 | return QString(); |
118 | return d->contents; |
119 | } |
120 | |
121 | QStringList QScriptScriptData::lines(int startLineNumber, int count) const |
122 | { |
123 | Q_D(const QScriptScriptData); |
124 | if (!d) |
125 | return QStringList(); |
126 | QStringList allLines = d->contents.split(sep: QLatin1Char('\n')); |
127 | return allLines.mid(pos: qMax(a: 0, b: startLineNumber - d->baseLineNumber), alength: count); |
128 | } |
129 | |
130 | QString QScriptScriptData::fileName() const |
131 | { |
132 | Q_D(const QScriptScriptData); |
133 | if (!d) |
134 | return QString(); |
135 | return d->fileName; |
136 | } |
137 | |
138 | int QScriptScriptData::baseLineNumber() const |
139 | { |
140 | Q_D(const QScriptScriptData); |
141 | if (!d) |
142 | return -1; |
143 | return d->baseLineNumber; |
144 | } |
145 | |
146 | QDateTime QScriptScriptData::timeStamp() const |
147 | { |
148 | Q_D(const QScriptScriptData); |
149 | if (!d) |
150 | return QDateTime(); |
151 | return d->timeStamp; |
152 | } |
153 | |
154 | bool QScriptScriptData::isValid() const |
155 | { |
156 | Q_D(const QScriptScriptData); |
157 | return (d != 0); |
158 | } |
159 | |
160 | bool QScriptScriptData::operator==(const QScriptScriptData &other) const |
161 | { |
162 | Q_D(const QScriptScriptData); |
163 | const QScriptScriptDataPrivate *od = other.d_func(); |
164 | if (d == od) |
165 | return true; |
166 | if (!d || !od) |
167 | return false; |
168 | return ((d->contents == od->contents) |
169 | && (d->fileName == od->fileName) |
170 | && (d->baseLineNumber == od->baseLineNumber)); |
171 | } |
172 | |
173 | bool QScriptScriptData::operator!=(const QScriptScriptData &other) const |
174 | { |
175 | return !(*this == other); |
176 | } |
177 | |
178 | QDataStream &operator<<(QDataStream &out, const QScriptScriptData &data) |
179 | { |
180 | const QScriptScriptDataPrivate *d = data.d_ptr.data(); |
181 | if (d) { |
182 | out << d->contents; |
183 | out << d->fileName; |
184 | out << qint32(d->baseLineNumber); |
185 | } else { |
186 | out << QString(); |
187 | out << QString(); |
188 | out << qint32(0); |
189 | } |
190 | return out; |
191 | } |
192 | |
193 | QDataStream &operator>>(QDataStream &in, QScriptScriptData &data) |
194 | { |
195 | if (!data.d_ptr) { |
196 | data.d_ptr.reset(other: new QScriptScriptDataPrivate()); |
197 | data.d_ptr->ref.ref(); |
198 | } |
199 | QScriptScriptDataPrivate *d = data.d_ptr.data(); |
200 | in >> d->contents; |
201 | in >> d->fileName; |
202 | qint32 ln; |
203 | in >> ln; |
204 | d->baseLineNumber = ln; |
205 | return in; |
206 | } |
207 | |
208 | QT_END_NAMESPACE |
209 | |