From 3568c8ecfb6dad0919a222e42c8c34fa339dca72 Mon Sep 17 00:00:00 2001 From: Hoffmann Gerhard Date: Mon, 11 Mar 2024 12:14:28 +0100 Subject: [PATCH] first steps --- ATBTariffCalculator.pro | 2 +- Calculator/Calculator.pro | 91 +++------------ Calculator/main.cpp | 51 ++++++++- Calculator/message_handler.cpp | 107 ++++++++++++++++++ Calculator/message_handler.h | 23 ++++ CalculatorCInterface/CalculatorCInterface.pro | 14 +-- .../calculator_c_interface_lib.cpp | 13 ++- .../calculator_c_interface_lib.h | 11 +- .../calculator_c_interface_lib_global.h | 6 +- ...ariff_editor.cpp => tariff_calculator.cpp} | 21 +++- .../{tariff_editor.h => tariff_calculator.h} | 11 +- Utilities/Utilities.pro | 82 -------------- 12 files changed, 239 insertions(+), 193 deletions(-) create mode 100644 Calculator/message_handler.cpp create mode 100644 Calculator/message_handler.h rename CalculatorCInterface/{tariff_editor.cpp => tariff_calculator.cpp} (97%) rename CalculatorCInterface/{tariff_editor.h => tariff_calculator.h} (59%) diff --git a/ATBTariffCalculator.pro b/ATBTariffCalculator.pro index baac1bb..4252756 100644 --- a/ATBTariffCalculator.pro +++ b/ATBTariffCalculator.pro @@ -1,3 +1,3 @@ TEMPLATE = subdirs CONFIG += ordered -SUBDIRS = Calculator Utilities CalculatorCInterface +SUBDIRS = Calculator CalculatorCInterface diff --git a/Calculator/Calculator.pro b/Calculator/Calculator.pro index e1ce5c6..c647e4e 100644 --- a/Calculator/Calculator.pro +++ b/Calculator/Calculator.pro @@ -1,84 +1,23 @@ -QT += core +QT+=core -TARGET = calc +CONFIG+=c++20 console -VERSION="0.1.0" +INCLUDEPATH+="../CalculatorCInterface" -win32 { - BUILD_DATE=$$system("date /t") - BUILD_TIME=$$system("time /t") -} else { - BUILD_DATE=$$system("date +%d-%m-%y") - BUILD_TIME=$$system("date +%H:%M:%S") -} +#LIBS += "C:\build-ATBTariffCalculator-Desktop_Qt_6_5_0_MinGW_64_bit-Debug\CalculatorCInterface\debug\libCalculatorCInterface.a" +#LIBS += "C:\build-ATBTariffCalculator-Desktop_Qt_6_5_0_MinGW_64_bit-Debug\CalculatorCInterface\debug\CalculatorCInterface.dll" -GIT_COMMIT=$$system("git log -1 --format=oneline | cut -d' ' -f1") - -EXTENDED_VERSION="$${VERSION}-$${GIT_COMMIT}" - -CONFIG += c++17 - -DEFINES+=APP_VERSION=\\\"$$VERSION\\\" -DEFINES+=APP_BUILD_DATE=\\\"$$BUILD_DATE\\\" -DEFINES+=APP_BUILD_TIME=\\\"$$BUILD_TIME\\\" -DEFINES+=APP_EXTENDED_VERSION=\\\"$$EXTENDED_VERSION\\\" - -# keep comments, as /* fall through */ -QMAKE_CXXFLAGS += -C -QMAKE_CXXFLAGS += -g -QMAKE_CXXFLAGS += -Wno-deprecated-copy -O - -INCLUDEPATH+=$$_PRO_FILE_PWD_/../CalculatorCInterface/ - -LIB_BUILD_DIR="/opt/ptu5/opt/build-ATBTariffCalculator-Desktop_Qt_5_12_12_GCC_64bit-Debug/CalculatorCInterface" - -LIBS += -L$$LIB_BUILD_DIR -lCalculatorCInterface - - -contains( CONFIG, PTU5 ) { - greaterThan(QT_MAJOR_VERSION, 4): QT += serialport - CONFIG += link_pkgconfig - lessThan(QT_MAJOR_VERSION, 5): PKGCONFIG += qextserialport - QMAKE_CXXFLAGS += -O2 -std=c++17 # for GCC >= 4.7 - # QMAKE_CXXFLAGS += -Wno-deprecated-copy - ARCH = PTU5 - DEFINES+=PTU5 -} -contains( CONFIG, PTU5_YOCTO ) { - greaterThan(QT_MAJOR_VERSION, 4): QT += serialport - QMAKE_CXXFLAGS += -std=c++17 # for GCC >= 4.7 - # QMAKE_CXXFLAGS += -Wno-deprecated-copy - PTU5BASEPATH = /opt/devel/ptu5 - ARCH = PTU5 - DEFINES+=PTU5 - - # add qmqtt lib - #LIBS += -lQt5Qmqtt -} -contains( CONFIG, DesktopLinux ) { - greaterThan(QT_MAJOR_VERSION, 4): QT += serialport - lessThan(QT_MAJOR_VERSION, 5): CONFIG += extserialport - # QMAKE_CC = ccache $$QMAKE_CC - # QMAKE_CXX = ccache $$QMAKE_CXX - QMAKE_CXXFLAGS += -std=c++17 - # QMAKE_CXXFLAGS += -Wno-deprecated-copy - linux-clang { QMAKE_CXXFLAGS += -Qunused-arguments } - ARCH = DesktopLinux - DEFINES+=DesktopLinux -} +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ - main.cpp + main.cpp \ + message_handler.cpp -# HEADERS += \ -# tariff_json_template.h \ -# tariff_editor.h +HEADERS += message_handler.h - -########################################################################################## -# for running program on target through QtCreator -contains( CONFIG, PTU5 ) { - qnx: target.path = /tmp/$${TARGET}/bin - else: unix:!android: target.path = /opt/app/tools/atbupdate/ - !isEmpty(target.path): INSTALLS += target -} +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/Calculator/main.cpp b/Calculator/main.cpp index afa649b..cb967aa 100644 --- a/Calculator/main.cpp +++ b/Calculator/main.cpp @@ -1,10 +1,55 @@ +#include +#include #include +#include + +#include +#include + +#include "message_handler.h" #include "calculator_c_interface_lib.h" -int main() { +//#include "tariff_calculator.h" - qCritical() << "initTariffEditor" << initTariffEditor(); +typedef TariffCalculator *TariffCalculatorHandle; + +typedef TariffCalculatorHandle (*NewTariffCalculatorFunc)(); +typedef void (*DeleteTariffCalculatorFunc)(TariffCalculatorHandle handle); + +//extern "C" { +//#include "calculator_c_interface_lib.h" +//bool __declspec(dllimport) initTariffEditor(); +//} +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + if (!messageHandlerInstalled()) { + atbInstallMessageHandler(atbDebugOutput); + //setDebugLevel(LOG_NOTICE); + } + + QLibrary library("C:\\build-ATBTariffCalculator-Desktop_Qt_6_5_0_MinGW_64_bit-Release\\CalculatorCInterface\\release\\CalculatorCInterface.dll"); + if (library.load()) { + qCritical() << "loaded"; + NewTariffCalculatorFunc f = (NewTariffCalculatorFunc) library.resolve("NewTariffCalculator"); + TariffCalculatorHandle handle = 0; + if (f) { + qCritical() << "resolved"; + handle = f(); + } + DeleteTariffCalculatorFunc d = (DeleteTariffCalculatorFunc) library.resolve("DeleteTariffCalculator"); + if (d) { + qCritical() << "resolved"; + d(handle); + } + } + + + //initTariffEditor(); + + + return 0; // a.exec(); - return 0; } diff --git a/Calculator/message_handler.cpp b/Calculator/message_handler.cpp new file mode 100644 index 0000000..164aab9 --- /dev/null +++ b/Calculator/message_handler.cpp @@ -0,0 +1,107 @@ +#include "message_handler.h" + +#include +#include +#include +#include +#include + + + +#define LOG_EMERG 0 +#define LOG_ALERT 1 +#define LOG_CRIT 2 +#define LOG_ERR 3 +#define LOG_WARNING 4 +#define LOG_NOTICE 5 +#define LOG_INFO 6 +#define LOG_DEBUG 7 + +static char const *DBG_NAME[] = { "DBG ", "WARN ", "CRIT ", "FATAL", "INFO " }; +static bool installedMsgHandler = false; +static int debugLevel = LOG_NOTICE; + +int getDebugLevel() { return debugLevel; } +void setDebugLevel(int newDebugLevel) { + debugLevel = newDebugLevel; +} + +bool messageHandlerInstalled() { + return installedMsgHandler; +} + +QtMessageHandler atbInstallMessageHandler(QtMessageHandler handler) { + installedMsgHandler = (handler != 0); + static QtMessageHandler prevHandler = nullptr; + if (handler) { + prevHandler = qInstallMessageHandler(handler); + return prevHandler; + } else { + return qInstallMessageHandler(prevHandler); + } +} + +/// +/// \brief Print message according to given debug level. +/// +/// \note Install this function using qInstallMsgHandler(). +/// +/// int main(int argc, char **argv) { +/// installMsgHandler(atbDebugOutput); +/// QApplication app(argc, argv); +/// ... +/// return app.exec(); +/// } +/// +//#if (QT_VERSION > QT_VERSION_CHECK(5, 0, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) +void atbDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { + Q_UNUSED(context); + QString const localMsg = QString(DBG_NAME[type]) + msg.toLocal8Bit(); + + switch (debugLevel) { + case LOG_DEBUG: { // debug-level message + fprintf(stderr, "LOG_DEBUG %s\n", localMsg.toStdString().c_str()); + } break; + case LOG_INFO: { // informational message + if (type != QtDebugMsg) { + fprintf(stderr, "LOG_INFO %s\n", localMsg.toStdString().c_str()); + } + } break; + case LOG_NOTICE: { // normal, but significant, condition + if (type != QtDebugMsg) { + fprintf(stderr, "LOG_NOTICE %s\n", localMsg.toStdString().c_str()); + } + } break; + case LOG_WARNING: { // warning conditions + if (type != QtInfoMsg && type != QtDebugMsg) { + fprintf(stderr, "LOG_WARN %s\n", localMsg.toStdString().c_str()); + } + } break; + case LOG_ERR: { // error conditions + if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) { + fprintf(stderr, "LOG_ERR %s\n", localMsg.toStdString().c_str()); + } + } break; + case LOG_CRIT: { // critical conditions + if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) { + fprintf(stderr, "LOG_CRIT %s\n", localMsg.toStdString().c_str()); + } + } break; + case LOG_ALERT: { // action must be taken immediately + if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) { + fprintf(stderr, "LOG_ALERT %s\n", localMsg.toStdString().c_str()); + } + } break; + case LOG_EMERG: { // system is unusable + if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) { + fprintf(stderr, "LOG_EMERG %s\n", localMsg.toStdString().c_str()); + } + } break; + default: { + //fprintf(stderr, "%s No ErrorLevel defined! %s\n", + // datetime.toStdString().c_str(), msg.toStdString().c_str()); + } + } +} +//#endif + diff --git a/Calculator/message_handler.h b/Calculator/message_handler.h new file mode 100644 index 0000000..1c54be4 --- /dev/null +++ b/Calculator/message_handler.h @@ -0,0 +1,23 @@ +#ifndef MESSAGE_HANDLER_H_INCLUDED +#define MESSAGE_HANDLER_H_INCLUDED + +#include +#ifdef __linux__ +#include +#endif + +int getDebugLevel(); +void setDebugLevel(int newDebugLevel); + +bool messageHandlerInstalled(); +QtMessageHandler atbInstallMessageHandler(QtMessageHandler handler); + +//#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) +// typedef void (*QtMessageHandler)(QtMsgType, const char *); +//void atbDebugOutput(QtMsgType type, const char *msg); +//#elif QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +// typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &); +void atbDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg); +//#endif + +#endif // MESSAGE_HANDLER_H_INCLUDED diff --git a/CalculatorCInterface/CalculatorCInterface.pro b/CalculatorCInterface/CalculatorCInterface.pro index c7943c0..d8702ef 100644 --- a/CalculatorCInterface/CalculatorCInterface.pro +++ b/CalculatorCInterface/CalculatorCInterface.pro @@ -1,13 +1,13 @@ -QT -= gui +QT+=core TEMPLATE = lib -DEFINES += CACULATOR_C_INTERFACE_LIBRARY +DEFINES += CALCULATOR_C_INTERFACE_LIBRARY # INCLUDEPATH+=$$_PRO_FILE_PWD_/../Utilities/ -CONFIG += c++17 -QMAKE_CXX=x86_64-w64-mingw32-g++ +CONFIG += c++20 console +#QMAKE_CXX=C:\Qt\Tools\mingw1120_64\g++.exe # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. @@ -15,12 +15,12 @@ QMAKE_CXX=x86_64-w64-mingw32-g++ SOURCES += \ calculator_c_interface_lib.cpp \ - tariff_editor.cpp + tariff_calculator.cpp HEADERS += \ calculator_c_interface_lib.h \ - calculator_c_interface_lib_global.h - tariff_editor.h + calculator_c_interface_lib_global.h \ + tariff_calculator.h # Default rules for deployment. unix { diff --git a/CalculatorCInterface/calculator_c_interface_lib.cpp b/CalculatorCInterface/calculator_c_interface_lib.cpp index ad4ca3e..d3fa02c 100644 --- a/CalculatorCInterface/calculator_c_interface_lib.cpp +++ b/CalculatorCInterface/calculator_c_interface_lib.cpp @@ -1,18 +1,19 @@ #include "calculator_c_interface_lib.h" -#include "tariff_editor.h" - #ifdef __cplusplus extern "C" { #endif - -bool CACULATOR_C_INTERFACE_LIB_EXPORT initTariffEditor() { - TariffEditor editor; - return true; +TariffCalculatorHandle CALCULATOR_C_INTERFACE_LIB_EXPORT NewTariffCalculator(void) { + return new TariffCalculator(); } +void CALCULATOR_C_INTERFACE_LIB_EXPORT DeleteTariffCalculator(TariffCalculatorHandle handle) { + delete handle; +} + + #ifdef __cplusplus } #endif diff --git a/CalculatorCInterface/calculator_c_interface_lib.h b/CalculatorCInterface/calculator_c_interface_lib.h index 85fd4e7..f9ba63a 100644 --- a/CalculatorCInterface/calculator_c_interface_lib.h +++ b/CalculatorCInterface/calculator_c_interface_lib.h @@ -2,19 +2,20 @@ #define CALCULATOR_C_INTERFACE_LIB_H_INCLUDED #include "calculator_c_interface_lib_global.h" +#include "tariff_calculator.h" + +typedef TariffCalculator *TariffCalculatorHandle; #ifdef __cplusplus extern "C" { #endif -bool CACULATOR_C_INTERFACE_LIB_EXPORT initTariffEditor(); +TariffCalculatorHandle CALCULATOR_C_INTERFACE_LIB_EXPORT NewTariffCalculator(void); +void CALCULATOR_C_INTERFACE_LIB_EXPORT DeleteTariffCalculator(TariffCalculatorHandle handle); #ifdef __cplusplus } #endif -//{ -//public: -// Testlib(); -//}; + #endif // CALCULATOR_C_INTERFACE_LIB_H_INCLUDED diff --git a/CalculatorCInterface/calculator_c_interface_lib_global.h b/CalculatorCInterface/calculator_c_interface_lib_global.h index c5150b7..beed995 100644 --- a/CalculatorCInterface/calculator_c_interface_lib_global.h +++ b/CalculatorCInterface/calculator_c_interface_lib_global.h @@ -3,10 +3,10 @@ #include -#if defined(CACULATOR_C_INTERFACE_LIBRARY) - #define CACULATOR_C_INTERFACE_LIB_EXPORT Q_DECL_EXPORT +#if defined(CALCULATOR_C_INTERFACE_LIBRARY) + #define CALCULATOR_C_INTERFACE_LIB_EXPORT Q_DECL_EXPORT __stdcall #else - #define CACULATOR_C_INTERFACE_LIB_EXPORT Q_DECL_IMPORT + #define CALCULATOR_C_INTERFACE_LIB_EXPORT Q_DECL_IMPORT __stdcall #endif #endif // CALCULATOR_C_INTERFACE_GLOBAL_H diff --git a/CalculatorCInterface/tariff_editor.cpp b/CalculatorCInterface/tariff_calculator.cpp similarity index 97% rename from CalculatorCInterface/tariff_editor.cpp rename to CalculatorCInterface/tariff_calculator.cpp index c996eeb..a244d81 100644 --- a/CalculatorCInterface/tariff_editor.cpp +++ b/CalculatorCInterface/tariff_calculator.cpp @@ -1,4 +1,4 @@ -#include "tariff_editor.h" +#include "tariff_calculator.h" #include #include @@ -10,7 +10,8 @@ #include #include -QByteArray TariffEditor::readJson(QString const &filename) { +#if 0 +QByteArray TariffCalculator::readJson(QString const &filename) { QFile f(filename); if (f.exists()) { if (f.open(QFile::ReadOnly | QFile::Text)) { @@ -25,10 +26,15 @@ QByteArray TariffEditor::readJson(QString const &filename) { return QByteArray(); } +#endif +TariffCalculator::~TariffCalculator() { + qCritical() << __func__; +} -TariffEditor::TariffEditor() { - +TariffCalculator::TariffCalculator() { + qCritical() << __func__; + return; // QByteArray ba = readJson("/opt/ptu5/opt/ATBTariffCalculator/Utilities/tariff.template.json"); //QJsonParseError pe; @@ -41,6 +47,7 @@ TariffEditor::TariffEditor() { //} m_a = QJsonArray(); + createJsonValue("Project", "Szeged"); createJsonValue("Version", "1.0.0"); createJsonValue("Date", "01.01.1970"); @@ -56,11 +63,14 @@ TariffEditor::TariffEditor() { createJsonValue("DayConfigs"); createJsonValue("Days"); + + m_jsonDoc.setArray(m_a); qCritical().noquote() << m_jsonDoc.toJson(QJsonDocument::JsonFormat::Indented); + } -void TariffEditor::createJsonValue(QString const &key, QString const &value) { +void TariffCalculator::createJsonValue(QString const &key, QString const &value) { if ((key.compare("Project", Qt::CaseInsensitive) == 0) || (key.compare("Version", Qt::CaseInsensitive) == 0) @@ -421,3 +431,4 @@ void TariffEditor::createJsonValue(QString const &key, QString const &value) { #endif } + diff --git a/CalculatorCInterface/tariff_editor.h b/CalculatorCInterface/tariff_calculator.h similarity index 59% rename from CalculatorCInterface/tariff_editor.h rename to CalculatorCInterface/tariff_calculator.h index 02c909f..6bd1b6c 100644 --- a/CalculatorCInterface/tariff_editor.h +++ b/CalculatorCInterface/tariff_calculator.h @@ -1,5 +1,5 @@ -#ifndef TARIFF_EDITOR_H_INCLUDED -#define TARIFF_EDITOR_H_INCLUDED +#ifndef TARIFF_CALCULATOR_H_INCLUDED +#define TARIFF_CALCULATOR_H_INCLUDED #include #include @@ -7,14 +7,15 @@ #include #include -class TariffEditor { +class TariffCalculator { QJsonArray m_a; QJsonDocument m_jsonDoc; void createJsonValue(QString const &key, QString const &value = ""); public: - explicit TariffEditor(); + explicit TariffCalculator(); + ~TariffCalculator(); static QByteArray readJson(QString const &filename); }; -#endif // TARIFF_EDITOR_H_INCLUDED +#endif // TARIFF_CALCULATOR_H_INCLUDED diff --git a/Utilities/Utilities.pro b/Utilities/Utilities.pro index 363e5ff..e69de29 100644 --- a/Utilities/Utilities.pro +++ b/Utilities/Utilities.pro @@ -1,82 +0,0 @@ -QT += core - -TARGET = util - -VERSION="0.1.0" -win32 { - BUILD_DATE=$$system("date /t") - BUILD_TIME=$$system("time /t") -} else { - BUILD_DATE=$$system("date +%d-%m-%y") - BUILD_TIME=$$system("date +%H:%M:%S") -} - -GIT_COMMIT=$$system("git log -1 --format=oneline | cut -d' ' -f1") - -EXTENDED_VERSION="$${VERSION}-$${GIT_COMMIT}" - -CONFIG += c++17 - -DEFINES+=APP_VERSION=\\\"$$VERSION\\\" -DEFINES+=APP_BUILD_DATE=\\\"$$BUILD_DATE\\\" -DEFINES+=APP_BUILD_TIME=\\\"$$BUILD_TIME\\\" -DEFINES+=APP_EXTENDED_VERSION=\\\"$$EXTENDED_VERSION\\\" - -# keep comments, as /* fall through */ -QMAKE_CXXFLAGS += -C -QMAKE_CXXFLAGS += -g -QMAKE_CXXFLAGS += -Wno-deprecated-copy -O - -contains( CONFIG, PTU5 ) { - greaterThan(QT_MAJOR_VERSION, 4): QT += serialport - CONFIG += link_pkgconfig - lessThan(QT_MAJOR_VERSION, 5): PKGCONFIG += qextserialport - QMAKE_CXXFLAGS += -O2 -std=c++17 # for GCC >= 4.7 - # QMAKE_CXXFLAGS += -Wno-deprecated-copy - ARCH = PTU5 - DEFINES+=PTU5 -} -contains( CONFIG, PTU5_YOCTO ) { - greaterThan(QT_MAJOR_VERSION, 4): QT += serialport - QMAKE_CXXFLAGS += -std=c++17 # for GCC >= 4.7 - # QMAKE_CXXFLAGS += -Wno-deprecated-copy - PTU5BASEPATH = /opt/devel/ptu5 - ARCH = PTU5 - DEFINES+=PTU5 - - # add qmqtt lib - #LIBS += -lQt5Qmqtt -} -contains( CONFIG, DesktopLinux ) { - greaterThan(QT_MAJOR_VERSION, 4): QT += serialport - lessThan(QT_MAJOR_VERSION, 5): CONFIG += extserialport - # QMAKE_CC = ccache $$QMAKE_CC - # QMAKE_CXX = ccache $$QMAKE_CXX - QMAKE_CXXFLAGS += -std=c++17 - # QMAKE_CXXFLAGS += -Wno-deprecated-copy - linux-clang { QMAKE_CXXFLAGS += -Qunused-arguments } - ARCH = DesktopLinux - DEFINES+=DesktopLinux -} - -SOURCES += \ - main.cpp \ - tariff_json_template.cpp \ - tariff_editor.cpp - -HEADERS += \ - tariff_json_template.h \ - tariff_editor.h - -OTHER_FILES += \ - tariff.json \ - tariff.template.json - - -########################################################################################## -# for running program on target through QtCreator -contains( CONFIG, PTU5 ) { - qnx: target.path = /tmp/$${TARGET}/bin - else: unix:!android: target.path = /opt/app/tools/atbupdate/ - !isEmpty(target.path): INSTALLS += target -}