Files
z-bar-qt/Plugins/ZShell/qalculator.cpp
T
AramJonghu 728bdc85bc
Lint & Format (JS/TS) / lint-format (pull_request) Successful in 9s
Python / lint-format (pull_request) Successful in 26s
Python / test (pull_request) Successful in 47s
Lint & Format (Rust) / lint-format (pull_request) Successful in 1m54s
C++ / build (pull_request) Successful in 3m25s
fix(clang-tidy/analyzer): resolving false positives
2026-06-30 03:17:52 +02:00

57 lines
1.2 KiB
C++

#include "qalculator.hpp"
#include <libqalculate/qalculate.h>
namespace ZShell {
Qalculator::Qalculator(QObject* parent) : QObject(parent) {
if (!CALCULATOR) {
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
new Calculator();
CALCULATOR->loadExchangeRates();
CALCULATOR->loadGlobalDefinitions();
CALCULATOR->loadLocalDefinitions();
}
}
QString Qalculator::eval(const QString& expr, bool printExpr) const {
if (expr.isEmpty()) {
return QString();
}
EvaluationOptions eo;
PrintOptions po;
std::string parsed;
std::string result = CALCULATOR->calculateAndPrint(
CALCULATOR->unlocalizeExpression(expr.toStdString(), eo.parse_options),
100,
eo,
po,
&parsed);
std::string error;
while (CALCULATOR->message()) {
if (!CALCULATOR->message()->message().empty()) {
if (CALCULATOR->message()->type() == MESSAGE_ERROR) {
error += "error: ";
} else if (CALCULATOR->message()->type() == MESSAGE_WARNING) {
error += "warning: ";
}
error += CALCULATOR->message()->message();
}
CALCULATOR->nextMessage();
}
if (!error.empty()) {
return QString::fromStdString(error);
}
if (printExpr) {
return QString("%1 = %2").arg(parsed).arg(result);
}
return QString::fromStdString(result);
}
} // namespace ZShell