-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathmain.cpp
More file actions
130 lines (104 loc) · 3.84 KB
/
Copy pathmain.cpp
File metadata and controls
130 lines (104 loc) · 3.84 KB
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
/*
* QHexEdit is a Hex Editor Widget for the Qt Framework
* Copyright (C) 2010-2025 Winfried Simon
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* https://www.gnu.org/licenses/
*/
#include <optional>
#include <QApplication>
#include <QCommandLineParser>
#include <QFile>
#include <QIcon>
#include <QLibraryInfo>
#include <QLocale>
#include <QTranslator>
#include "mainwindow.h"
struct ParsedOptions
{
bool hasFile = false;
QString file = "";
};
struct CommandLineParseResult
{
enum class Status { Ok, Error, VersionRequested, HelpRequested };
Status statusCode = Status::Ok;
std::optional<QString> errorString = std::nullopt;
};
CommandLineParseResult parseCommandLine(QCommandLineParser &parser, ParsedOptions *query)
{
using Status = CommandLineParseResult::Status;
const QCommandLineOption helpOption = parser.addHelpOption();
const QCommandLineOption versionOption = parser.addVersionOption();
parser.addPositionalArgument("file", "File to open");
if (!parser.parse(QCoreApplication::arguments()))
return {Status::Error, parser.errorText()};
if (parser.isSet(versionOption))
return {Status::VersionRequested};
if (parser.isSet(helpOption))
return {Status::HelpRequested};
const QStringList positionalArguments = parser.positionalArguments();
if (!positionalArguments.isEmpty()) {
if (positionalArguments.size() > 1)
return {Status::Error, "Several 'name' arguments specified."};
query->hasFile = true;
query->file = positionalArguments.first();
}
return {Status::Ok};
}
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(qhexedit);
QApplication app(argc, argv);
QCoreApplication::setApplicationName("QHexEdit");
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
app.setOrganizationName("QHexEdit");
app.setWindowIcon(QIcon(":images/qhexedit.ico"));
QTranslator translator_app;
if (translator_app.load(QLocale(), "qhexedit", "_", ":/translations"))
QCoreApplication::installTranslator(&translator_app);
QTranslator translator_qt;
#if QT_VERSION < 0x060000
QString path = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
#else
QString path = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
#endif
if (translator_qt.load(QLocale(), "qt", "_", path))
QCoreApplication::installTranslator(&translator_qt);
QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("QHexEdit", "A hex editor application"));
MainWindow *mainWin = new MainWindow;
ParsedOptions query;
using Status = CommandLineParseResult::Status;
CommandLineParseResult parseResult = parseCommandLine(parser, &query);
switch (parseResult.statusCode) {
case Status::Ok:
break;
case Status::Error:
std::fputs(qPrintable(parseResult.errorString.value_or("Unknown error occurred")), stderr);
std::fputs("\n\n", stderr);
std::fputs(qPrintable(parser.helpText()), stderr);
return 1;
case Status::VersionRequested:
parser.showVersion();
return 0;
case Status::HelpRequested:
parser.showHelp();
return 0;
}
if (query.hasFile)
mainWin->loadFile(query.file);
mainWin->show();
return app.exec();
}