-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
129 lines (120 loc) · 3.87 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
#include <QtGui/QApplication>
#include <QTextCodec>
#include "mainwindow.h"
#include <stdio.h>
#include <string.h>
FILE *logFile = 0;
void logMessageOutput(QtMsgType type, const char *msg)
{
switch (type) {
case QtDebugMsg:
fprintf(logFile, "Debug: %s\n", msg);
break;
case QtWarningMsg:
fprintf(logFile, "Warning: %s\n", msg);
break;
case QtCriticalMsg:
fprintf(logFile, "Critical: %s\n", msg);
break;
case QtFatalMsg:
fprintf(logFile, "Fatal: %s\n", msg);
abort();
}
}
void showHelp() {
printf("instead-launcher [-insteadpath <dir>] [-gamespath <dir>] [-log <logfile>] [-help]\n");
printf(" -insteadpath set instead dir\n");
printf(" -gamespath set games path dir\n");
printf(" -default-insteadpath set default instead dir\n");
printf(" -default-gamespath set default games path dir\n");
printf(" -help show this help message\n");
printf(" -log redirect debug messages to <logfile>\n\n");
}
bool enableLog(char *fileName) {
logFile = fopen(fileName, "at");
if (!logFile) {
printf("Can't open file %s for writing\n", fileName);
return false;
}
fprintf(logFile, "Log started\n");
qInstallMsgHandler(logMessageOutput);
return true;
}
int qtMain( const ArgMap &argMap, int argc, char *argv[] ) {
QApplication a(argc, argv);
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
MainWindow w( argMap );
w.show();
return a.exec();
}
int main( int argc, char *argv[] )
{
ArgMap argMap;
for (int i=1; i<argc; i++) {
if ( !strcmp(argv[i], "-help") || !strcmp(argv[i], "--help") ) {
showHelp();
return 1;
} else if ( !strcmp(argv[i], "-log") || !strcmp(argv[i], "--log") ) {
i++;
if (i < argc) {
if (!enableLog(argv[i])) return 1;
} else {
showHelp();
return 1;
}
} else if ( !strcmp(argv[i], "-gamespath") || !strcmp(argv[i], "--gamespath") ) {
i++;
if (i < argc) {
argMap["gamespath"]=QString::fromLocal8Bit( argv[i] );
} else {
showHelp();
return 1;
}
} else if ( !strcmp(argv[i], "-insteadpath") || !strcmp(argv[i], "--insteadpath") ) {
i++;
if (i < argc) {
if ( QFileInfo( QString::fromLocal8Bit( argv[i] ) ).exists() ) {
argMap["insteadpath"]=QString::fromLocal8Bit( argv[i] );
} else {
showHelp();
return 1;
}
} else {
showHelp();
return 1;
}
} else if ( !strcmp(argv[i], "-default-gamespath") || !strcmp(argv[i], "--default-gamespath") ) {
i++;
if (i < argc) {
argMap["default-gamespath"]=QString::fromLocal8Bit( argv[i] );
} else {
showHelp();
return 1;
}
} else if ( !strcmp(argv[i], "-default-insteadpath") || !strcmp(argv[i], "--default-insteadpath") ) {
i++;
if (i < argc) {
if ( QFileInfo( QString::fromLocal8Bit( argv[i] ) ).exists() ) {
argMap["default-insteadpath"]=QString::fromLocal8Bit( argv[i] );
} else {
showHelp();
return 1;
}
} else {
showHelp();
return 1;
}
} else {
fprintf(stderr,"Unrecognized option: %s\n\n", argv[i]);
showHelp();
return 1;
}
}
int result = qtMain( argMap, argc, argv );
if ( logFile ) {
fprintf( logFile, "Log finished\n\n");
fclose( logFile );
}
return result;
}