-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·114 lines (98 loc) · 2.75 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·114 lines (98 loc) · 2.75 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
#include <QApplication>
#include <QFile>
#include <QTextStream>
#include <QSettings>
#include <QDebug>
#include "scicalc.h"
#include "scanner.h"
#include "parser.h"
#include "variables.h"
namespace
{
int runCli(QString fileName)
{
QFile file(fileName);
if(!file.open(QFile::ReadOnly | QFile::Text))
{
QTextStream err(stderr);
err << "ERROR: could not open file: " << fileName << "\n";
return 1;
}
QTextStream in(&file);
in.setCodec("UTF-8");
QTextStream out(stdout);
out.setCodec("UTF-8");
scicalc::app()->resetTemporarySettings();
Variables::init();
bool accountingMode=scicalc::app()->getAccountingModeSetting();
bool previousResultAvailable=false;
while(!in.atEnd())
{
QString expression=in.readLine();
if(expression.startsWith("\t"))
{
continue;
}
QString evaluatedExpression=expression;
if(accountingMode && previousResultAvailable)
{
int firstCharIndex=0;
while(firstCharIndex<evaluatedExpression.size() && evaluatedExpression.at(firstCharIndex).isSpace())
{
firstCharIndex++;
}
if(firstCharIndex<evaluatedExpression.size())
{
QChar firstChar=evaluatedExpression.at(firstCharIndex);
bool isDivisionComment=(firstChar=='/' && firstCharIndex+1<evaluatedExpression.size() && evaluatedExpression.at(firstCharIndex+1)=='/');
if(!isDivisionComment &&
(firstChar=='+' || firstChar=='-' || firstChar=='*' || firstChar=='/' || firstChar=='^'))
{
evaluatedExpression.insert(firstCharIndex, QChar('$'));
}
}
}
Scanner::init(evaluatedExpression);
QString output=Parser::parse();
out << expression << "\n";
if(!output.isEmpty())
{
QStringList outputLines=output.split("\n");
for(int i=0; i<outputLines.size(); i++)
{
out << "\t\t\t\t" << outputLines.at(i) << "\n";
}
}
QString trimmedInput=expression.trimmed();
bool isCommentLine=(trimmedInput.startsWith("//") || trimmedInput.startsWith("%"));
bool hasExpression=!trimmedInput.isEmpty() && !isCommentLine;
bool parseOk=!output.startsWith("ERROR");
if(hasExpression)
{
previousResultAvailable=accountingMode && parseOk;
}
accountingMode=scicalc::app()->getAccountingModeSetting();
}
return 0;
}
}
int main(int argc, char *argv[])
{
if(argc==2 && qgetenv("QT_QPA_PLATFORM").isEmpty())
{
qputenv("QT_QPA_PLATFORM", "offscreen");
}
QApplication a(argc, argv);
QCoreApplication::setOrganizationName("scicalc");
QCoreApplication::setOrganizationDomain("https://github.com/ElektronikNode/scicalc");
QCoreApplication::setApplicationName("scicalc");
if(QApplication::arguments().size()==2)
{
a.setProperty("scicalcCliMode", true);
scicalc app;
return runCli(QApplication::arguments().at(1));
}
scicalc w;
w.show();
return a.exec();
}