replaced abort() with EG_BUG
[engrid.git] / src / main.cpp
blobdc6d056dc51509992f6340fef1bd5ed8a1efdc01
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008,2009 Oliver Gloth +
7 // + +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
12 // + +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
17 // + +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include <QtCore>
24 #include <QtGui>
25 #include <QApplication>
26 #include <QFile>
27 #include <QTextStream>
28 #include <QDate>
30 #include "guimainwindow.h"
31 #include "filetemplate.h"
33 #include "geometrytools.h"
34 using namespace GeometryTools;
36 ///@@@ TODO: replace with shellscript?
37 void appendLicense(int argc, char ** argv)
39 int first_year = 2008;
40 QString first_year_text;
41 first_year_text.setNum(first_year);
42 int year = QDate::currentDate().year();
43 QString year_text;
44 year_text.setNum(year);
45 if (year-first_year == 1) {
46 year_text = first_year_text + "," + year_text;
48 if (year-first_year > 1) {
49 year_text = first_year_text + "-" + year_text;
51 QString year_end_text = " +\n";
52 if (year == first_year) {
53 year_end_text = " " + year_end_text;
55 for (int i = 2; i < argc; ++i) {
56 QString filename(argv[i]);
57 QString buffer = "//\n";
58 buffer += "// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
59 buffer += "// + +\n";
60 buffer += "// + This file is part of enGrid. +\n";
61 buffer += "// + +\n";
62 buffer += "// + Copyright " + year_text + " Oliver Gloth" + year_end_text;
63 buffer += "// + +\n";
64 buffer += "// + enGrid is free software: you can redistribute it and/or modify +\n";
65 buffer += "// + it under the terms of the GNU General Public License as published by +\n";
66 buffer += "// + the Free Software Foundation, either version 3 of the License, or +\n";
67 buffer += "// + (at your option) any later version. +\n";
68 buffer += "// + +\n";
69 buffer += "// + enGrid is distributed in the hope that it will be useful, +\n";
70 buffer += "// + but WITHOUT ANY WARRANTY; without even the implied warranty of +\n";
71 buffer += "// + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +\n";
72 buffer += "// + GNU General Public License for more details. +\n";
73 buffer += "// + +\n";
74 buffer += "// + You should have received a copy of the GNU General Public License +\n";
75 buffer += "// + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +\n";
76 buffer += "// + +\n";
77 buffer += "// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
78 buffer += "//\n";
80 bool src_code = false;
81 QFile file(filename);
82 file.open(QIODevice::ReadOnly);
83 QTextStream f(&file);
84 while (!f.atEnd()) {
85 QString line = f.readLine();
86 if (!src_code) {
87 if (line.size() >= 2) {
88 if (line.left(2) != "//") {
89 src_code = true;
91 } else {
92 src_code = true;
95 if (src_code) {
96 buffer += line + "\n";
101 QFile file(filename);
102 file.open(QIODevice::WriteOnly);
103 QTextStream f(&file);
104 f << buffer;
109 ///@@@ TODO: replace with shellscript?
110 void makeDistribution()
112 system ("ldd ./engrid > ldd.out");
113 system ("mkdir enGrid");
114 system ("cp engrid enGrid");
115 system ("cp start_engrid enGrid");
117 QFile file("ldd.out");
118 file.open(QIODevice::ReadOnly);
119 QTextStream f(&file);
120 while (!f.atEnd()) {
121 QString line = f.readLine();
122 QTextStream l(&line, QIODevice::ReadOnly);
123 QString word;
124 l >> word;
125 if (word.left(1) != "/") {
126 l >> word;
127 l >> word;
129 QString cmd = "cp " + word + " enGrid";
130 system(qPrintable(cmd));
131 cout << qPrintable(cmd) << endl;
134 system ("tar czf enGrid_bin.tar.gz enGrid/*");
135 system ("rm -rf enGrid");
136 system ("rm ldd.out");
139 /** Message handler to allow output of Qt objects in the terminal (stderr) or the logfile/output window of engrid (stdout)
140 qDebug() is used for writing custom debug output.
141 qWarning() is used to report warnings and recoverable errors in your application.
142 qCritical() is used for writing critical error mesages and reporting system errors.
143 qFatal() is used for writing fatal error messages shortly before exiting.
145 void engridMessageHandler(QtMsgType type, const char *msg)
147 switch (type) {
148 case QtDebugMsg:
149 // fprintf(stdout, "%s", msg);
150 cout<<msg<<endl;
151 break;
152 case QtWarningMsg:
153 // fprintf(stderr, "%s", msg);
154 cerr<<msg<<endl;
155 break;
156 case QtCriticalMsg:
157 fprintf(stderr, "Critical: %s\n", msg);
158 break;
159 case QtFatalMsg:
160 fprintf(stderr, "Fatal: %s\n", msg);
161 abort();
165 int main( int argc, char ** argv )
167 qInstallMsgHandler(engridMessageHandler);
168 Q_INIT_RESOURCE(engrid);
170 ///@@@ TODO: use gnu getopt ? Check windows/mac compatibility.
171 if (argc > 1) {
172 if (QString(argv[1]) == QString("-h")) {
173 QFileInfo file_info(argv[0]);
174 cout<<"Usage:"<<endl;
175 cout<<qPrintable(file_info.fileName())<<" : start engrid"<<endl;
176 cout<<qPrintable(file_info.fileName())<<" -f FILE : start engrid and open FILE"<<endl;
177 cout<<qPrintable(file_info.fileName())<<" -h : Display usage instructions"<<endl;
178 cout<<qPrintable(file_info.fileName())<<" -appendlic FILE1 FILE2 ...: Append license to files"<<endl;
179 cout<<qPrintable(file_info.fileName())<<" -distbin : Create binary distribution"<<endl;
180 exit(0);
182 if (QString(argv[1]) == QString("-appendlic")) {
183 appendLicense(argc, argv);
185 if (QString(argv[1]) == QString("-distbin")) {
186 makeDistribution();
188 if (QString(argv[1]) == QString("-f") && argc == 3) {
189 QApplication a( argc, argv );
190 GuiMainWindow w;
191 w.show();
192 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
193 GuiMainWindow::pointer()->open(QString(argv[2]));//this could aventually be done with another GuiMainWindow constructor
194 a.exec();
196 } else {
197 QApplication a( argc, argv );
198 GuiMainWindow w;
199 w.show();
200 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
201 a.exec();