Merge branch 'release'
[engrid.git] / src / main.cpp
blobe50e42a3a73163d0a49a706ccdf7d9f38108e332
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008-2010 enGits GmbH +
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 comment = "// ";
58 QString buffer = "";
59 bool script = false;
60 if (filename.right(3) == ".sh") script = true;
61 if (filename.right(3) == ".py") script = true;
62 if (filename.right(5) == ".bash") script = true;
63 QFile file(filename);
64 file.open(QIODevice::ReadOnly);
65 QTextStream f(&file);
66 if (script) {
67 comment = "# ";
68 QString line = f.readLine();
69 buffer += line + "\n";
71 buffer += comment + "\n";
72 buffer += comment + "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
73 buffer += comment + "+ +\n";
74 buffer += comment + "+ This file is part of enGrid. +\n";
75 buffer += comment + "+ +\n";
76 buffer += comment + "+ Copyright " + year_text + " enGits GmbH" + year_end_text;
77 buffer += comment + "+ +\n";
78 buffer += comment + "+ enGrid is free software: you can redistribute it and/or modify +\n";
79 buffer += comment + "+ it under the terms of the GNU General Public License as published by +\n";
80 buffer += comment + "+ the Free Software Foundation, either version 3 of the License, or +\n";
81 buffer += comment + "+ (at your option) any later version. +\n";
82 buffer += comment + "+ +\n";
83 buffer += comment + "+ enGrid is distributed in the hope that it will be useful, +\n";
84 buffer += comment + "+ but WITHOUT ANY WARRANTY; without even the implied warranty of +\n";
85 buffer += comment + "+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +\n";
86 buffer += comment + "+ GNU General Public License for more details. +\n";
87 buffer += comment + "+ +\n";
88 buffer += comment + "+ You should have received a copy of the GNU General Public License +\n";
89 buffer += comment + "+ along with enGrid. If not, see <http://www.gnu.org/licenses/>. +\n";
90 buffer += comment + "+ +\n";
91 buffer += comment + "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
92 buffer += comment + "\n";
94 bool src_code = false;
95 while (!f.atEnd()) {
96 QString line = f.readLine();
97 if (!src_code) {
98 if (line.size() >= comment.length()-1) {
99 if (line.left(comment.length()-1) != comment.left(comment.length()-1)) {
100 src_code = true;
102 } else {
103 src_code = true;
106 if (src_code) {
107 buffer += line + "\n";
112 QFile file(filename);
113 file.open(QIODevice::WriteOnly);
114 QTextStream f(&file);
115 f << buffer;
120 ///\todo replace with shellscript?
121 void makeDistribution()
123 system ("ldd ./engrid > ldd.out");
124 system ("mkdir enGrid");
125 system ("cp engrid enGrid");
127 QFile file("ldd.out");
128 file.open(QIODevice::ReadOnly);
129 QTextStream f(&file);
130 while (!f.atEnd()) {
131 QString line = f.readLine();
132 QTextStream l(&line, QIODevice::ReadOnly);
133 QString word;
134 l >> word;
135 if (word.left(1) != "/") {
136 l >> word;
137 l >> word;
139 QString cmd = "cp " + word + " enGrid";
140 system(qPrintable(cmd));
141 cout << qPrintable(cmd) << endl;
144 system ("tar czf enGrid_bin.tar.gz enGrid/*");
145 system ("rm -rf enGrid");
146 system ("rm ldd.out");
149 /** Message handler to allow output of Qt objects in the terminal (stderr) or the logfile/output window of engrid (stdout)
150 qDebug() is used for writing custom debug output.
151 qWarning() is used to report warnings and recoverable errors in your application.
152 qCritical() is used for writing critical error mesages and reporting system errors.
153 qFatal() is used for writing fatal error messages shortly before exiting.
155 void engridMessageHandler(QtMsgType type, const char *msg)
157 switch (type) {
158 case QtDebugMsg:
159 // fprintf(stdout, "%s", msg);
160 cout<<msg<<endl;
161 break;
162 case QtWarningMsg:
163 // fprintf(stderr, "%s", msg);
164 cerr<<msg<<endl;
165 break;
166 case QtCriticalMsg:
167 fprintf(stderr, "Critical: %s\n", msg);
168 break;
169 case QtFatalMsg:
170 fprintf(stderr, "Fatal: %s\n", msg);
171 abort();
175 double getNumber(QString text)
177 double myNumber = 0;
178 while (true) {
179 qDebug() << text;
180 string input = "";
181 getline(cin, input);
183 // This code converts from string to number safely.
184 stringstream myStream(input);
185 if (myStream >> myNumber)
186 break;
187 qDebug() << "Invalid number, please try again" << endl;
189 return myNumber;
192 int main( int argc, char ** argv )
195 double a,b,c;
196 double x[3];
197 int N;
199 // x^3 + a x^2 + b x + c = 0
200 a = getNumber("a=");
201 b = getNumber("b=");
202 c = getNumber("c=");
203 N = poly_solve_cubic( a, b, c, &(x[0]), &(x[1]), &(x[2]));
204 qDebug()<<"x^3 + "<<a<<" *x^2 + "<<b<<" *x + "<<c<<" = 0";
205 for(int i=0;i<N;i++) qDebug()<<"x["<<i<<"]="<<x[i];
207 // a x^2 + b x + c = 0
208 a = getNumber("a=");
209 b = getNumber("b=");
210 c = getNumber("c=");
211 N = poly_solve_quadratic( a, b, c, &(x[0]), &(x[1]));
212 qDebug()<<a<<" *x^2 + "<<b<<" *x + "<<c<<" = 0";
213 for(int i=0;i<N;i++) qDebug()<<"x["<<i<<"]="<<x[i];
214 exit(0);
217 qInstallMsgHandler(engridMessageHandler);
218 Q_INIT_RESOURCE(engrid);
220 ///\todo use gnu getopt ? Check windows/mac compatibility.
221 if (argc > 1) {
222 if (QString(argv[1]) == QString("-h")) {
223 QFileInfo file_info(argv[0]);
224 cout<<"Usage:"<<endl;
225 cout<<qPrintable(file_info.fileName())<<" : start engrid"<<endl;
226 cout<<qPrintable(file_info.fileName())<<" -f FILE : start engrid and open FILE"<<endl;
227 cout<<qPrintable(file_info.fileName())<<" -h : Display usage instructions"<<endl;
228 cout<<qPrintable(file_info.fileName())<<" -appendlic FILE1 FILE2 ...: Append license to files"<<endl;
229 cout<<qPrintable(file_info.fileName())<<" -distbin : Create binary distribution"<<endl;
230 exit(0);
232 if (QString(argv[1]) == QString("-appendlic")) {
233 appendLicense(argc, argv);
235 if (QString(argv[1]) == QString("-distbin")) {
236 makeDistribution();
238 if (QString(argv[1]) == QString("-f") && argc == 3) {
239 QApplication a( argc, argv );
240 QString filename = QString(argv[2]);
241 GuiMainWindow w(filename);
242 w.show();
243 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
244 a.exec();
246 } else {
247 QApplication a( argc, argv );
248 GuiMainWindow w;
249 w.show();
250 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
251 a.exec();