forked from torrent-file-editor/torrent-file-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
294 lines (250 loc) · 7.2 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*
* Copyright (C) 2014-2015 Ivan Romanov <drizt72@zoho.eu>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "mainwindow.h"
#include "application.h"
#include "bencode.h"
#include <QVariant>
#include <QFile>
// Allow run Qt5 static version https://github.com/tonytheodore/mxe/commit/497669fa44356db0cd8335e2554b7bac12eb88c2
#if defined HAVE_QT5 && defined Q_OS_WIN && defined BUILD_STATIC
#include <QtPlugin>
Q_IMPORT_PLUGIN(QWindowsVistaStylePlugin)
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
Q_IMPORT_PLUGIN(QGifPlugin)
Q_IMPORT_PLUGIN(QICOPlugin)
Q_IMPORT_PLUGIN(QJpegPlugin)
#endif
#ifdef HAVE_QT5
# include <QJsonDocument>
#else
# include <qjson/serializer.h>
# include <qjson/parser.h>
#endif
#ifdef Q_OS_WIN
# include <windows.h>
HANDLE hConsole = NULL;
#endif
#ifdef Q_OS_MAC
# include "cocoainitializer.h"
# include "sparkleautoupdater.h"
#endif
#ifdef ENABLE_NVWA
# include "nvwa/debug_new.h"
#endif
#ifdef Q_OS_WIN
# ifdef HAVE_QT5
void winDebugHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
# else
void winDebugHandler(QtMsgType type, const char *msg)
# endif
{
{
QString time = QTime::currentTime().toString();
QString debugMsg = QStringLiteral("[%1] ").arg(time);
switch (type) {
case QtDebugMsg:
break;
case QtWarningMsg:
debugMsg += QLatin1String("W:");
break;
case QtCriticalMsg:
debugMsg += QLatin1String("C:");
break;
# if QT_VERSION >= 0x050500
case QtInfoMsg:
debugMsg += QLatin1String("I:");
break;
# endif
case QtFatalMsg:
debugMsg += QLatin1String("F:");
break;
abort();
}
# ifdef HAVE_QT5
debugMsg += msg;
debugMsg += QStringLiteral(" (%1:%2, %3)").arg(QString::fromUtf8(context.file), QString::number(context.line), QString::fromUtf8(context.function));
# else
debugMsg += QLatin1String(msg);
# endif
# ifdef UNICODE
OutputDebugString(debugMsg.toStdWString().c_str());
# else
OutputDebugString(debugMsg.toStdString().c_str());
# endif
if (type == QtFatalMsg)
abort();
}
}
#endif
void openWinConsole()
{
#ifdef Q_OS_WIN
BOOL b = AllocConsole();
if (!b)
return;
hConsole = GetStdHandle(STD_INPUT_HANDLE);
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
#endif
}
void closeWinConsole()
{
#ifdef Q_OS_WIN
if (!hConsole)
return;
printf("\nPress any key to close window...\n");
getchar();
FreeConsole();
#endif
}
bool toJson(const QString &source, const QString &dest)
{
QFile sourceFile(source);
if (!sourceFile.open(QIODevice::ReadOnly)) {
qDebug("Error: can't open source file");
return false;
}
QByteArray raw(sourceFile.readAll());
sourceFile.close();
Bencode *bencode = Bencode::fromRaw(raw);
QVariant json = bencode->toJson();
delete bencode;
if (!json.isValid()) {
qDebug("Error: can't parse bencode format");
return false;
}
#ifdef HAVE_QT5
QByteArray ba = QJsonDocument::fromVariant(json).toJson();
#else
QJson::Serializer serializer;
serializer.setIndentMode(QJson::IndentFull);
QByteArray ba = serializer.serialize(json);
#endif
QFile destFile(dest);
if (!destFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug("Error: can't open destination file");
return false;
}
destFile.write(ba);
destFile.close();
return true;
}
bool fromJson(const QString &source, const QString &dest)
{
QFile sourceFile(source);
if (!sourceFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
openWinConsole();
qDebug("Error: can't open source file");
return false;
}
QByteArray ba(sourceFile.readAll());
sourceFile.close();
#ifdef HAVE_QT5
QJsonParseError error;
QVariant variant = QJsonDocument::fromJson(ba, &error).toVariant();
#else
QJson::Parser parser;
bool ok;
QVariant variant = parser.parse(ba, &ok);
#endif
if (!variant.isValid()) {
qDebug("Error: can't parse json format");
return false;
}
Bencode *bencode = Bencode::fromJson(variant);
QFile destFile(dest);
if (!destFile.open(QIODevice::WriteOnly)) {
qDebug("Error: can't open destination file");
return false;
}
destFile.write(bencode->toRaw());
destFile.close();
delete bencode;
return true;
}
int main(int argc, char *argv[])
{
if (argc == 2 && !strcmp(argv[1], "--help")) {
openWinConsole();
printf("Usage: torrent-file-editor --to-json | --from-json source dest\n");
closeWinConsole();
return 0;
}
#ifdef ENABLE_NVWA
NVWA::new_progname = argv[0];
NVWA::new_autocheck_flag = false;
#endif
if (argc == 4) { // -V112 PVS-Studio
QString command = QString::fromUtf8(argv[1]);
#ifndef Q_OS_WIN
QString source = QString::fromUtf8(argv[2]);
QString dest = QString::fromUtf8(argv[3]);
#else
QString source = QString::fromLocal8Bit(argv[2]);
QString dest = QString::fromLocal8Bit(argv[3]);
#endif
if (command == QLatin1String("--to-json") || command == QLatin1String("--from-json")) {
int retCode = 0;
openWinConsole();
if (!QFile::exists(source)) {
qDebug("Error: source file is not exist!");
retCode = -1;
}
else if (command == QLatin1String("--to-json"))
retCode = toJson(source, dest) ? -1 : 0;
else
retCode = fromJson(source, dest) ? -1 : 0;
closeWinConsole();
return retCode;
}
}
#ifdef Q_OS_WIN
# ifdef HAVE_QT5
qInstallMessageHandler(winDebugHandler);
# else
qInstallMsgHandler(winDebugHandler);
# endif
#endif
int returnCode = 0;
// For nvwa purposes. Need to delete local objects before leaks checking.
{
Application a(argc, argv);
MainWindow w;
a.setMainWindow(&w);
w.show();
#ifdef Q_OS_MAC
CocoaInitializer initializer;
SparkleAutoUpdater *updater = new SparkleAutoUpdater;
updater->checkForUpdates();
#endif
if (argc == 2) {
QString filename = QString::fromLocal8Bit(argv[1]);
if (QFile::exists(filename))
w.open(filename);
}
returnCode = a.exec();
}
#ifdef ENABLE_NVWA
NVWA::check_leaks_summary();
#endif
return returnCode;
}