Compare commits
2 Commits
90853294da
...
73340cbb7a
Author | SHA1 | Date | |
---|---|---|---|
73340cbb7a | |||
65f72eca79 |
@ -31,11 +31,13 @@ int main(int argc, char *argv[])
|
||||
//TariffCalculatorHandle handle = NewTariffCalculator();
|
||||
//DeleteTariffCalculator(handle);
|
||||
|
||||
if (InitGitLibrary() > 0) {
|
||||
qCritical() << CloneRepository("https://git.mimbach49.de/GerhardHoffmann/customer_999.git", "C:\\tmp\\customer_999");
|
||||
qCritical() << CheckoutLocalBranch("C:\\tmp\\customer_999", "master");
|
||||
ShutdownGitLibrary();
|
||||
}
|
||||
//if (InitGitLibrary() > 0) {
|
||||
// qCritical() << CloneRepository("https://git.mimbach49.de/GerhardHoffmann/customer_999.git", "C:\\tmp\\customer_999");
|
||||
// qCritical() << CheckoutLocalBranch("C:\\tmp\\customer_999", "master");
|
||||
// ShutdownGitLibrary();
|
||||
// }
|
||||
|
||||
qCritical() << GetFileMenuSize("customer_999");
|
||||
#else
|
||||
QLibrary library("C:\\build-ATBTariffCalculator-Desktop_Qt_6_5_0_MinGW_64_bit-Release\\CalculatorCInterface\\release\\CalculatorCInterface.dll");
|
||||
if (library.load()) {
|
||||
|
@ -28,12 +28,14 @@ CONFIG += c++20 console
|
||||
|
||||
SOURCES += \
|
||||
calculator_c_interface_lib.cpp \
|
||||
tariff_calculator.cpp
|
||||
tariff_calculator.cpp \
|
||||
local_git_repository.cpp
|
||||
|
||||
HEADERS += \
|
||||
calculator_c_interface_lib.h \
|
||||
calculator_c_interface_lib_global.h \
|
||||
tariff_calculator.h
|
||||
tariff_calculator.h \
|
||||
local_git_repository.h
|
||||
|
||||
# Default rules for deployment.
|
||||
unix {
|
||||
|
@ -5,12 +5,17 @@
|
||||
#include <QMap>
|
||||
#include <QString>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QByteArray>
|
||||
#include <QRegularExpression>
|
||||
#include <QRegularExpressionMatch>
|
||||
#include <mutex>
|
||||
|
||||
|
||||
#include <git2.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <algorithm>
|
||||
|
||||
static QMap<QString, git_repository *> customerRepoMap;
|
||||
static std::mutex m;
|
||||
@ -589,7 +594,67 @@ int PushLocalRepository(char const *local_path, char const *branch_name, char co
|
||||
return error;
|
||||
}
|
||||
|
||||
#include <local_git_repository.h>
|
||||
|
||||
static QMap<QString, LocalGitRepository *> localGitRepos;
|
||||
|
||||
int32_t GetFileMenuSize(char const *localGitRepo) {
|
||||
if (localGitRepos.count(localGitRepo) == 0) {
|
||||
localGitRepos.insert(localGitRepo,
|
||||
new LocalGitRepository(localGitRepo));
|
||||
}
|
||||
return localGitRepos[localGitRepo]->GetFileMenuSize();
|
||||
}
|
||||
|
||||
char const *GetFileMenu(const char *localGitRepo) {
|
||||
static char j[] = "{}";
|
||||
char *json = j;
|
||||
int const len = GetFileMenuSize(localGitRepo);
|
||||
if (len > 0) {
|
||||
json = new char [len+1];
|
||||
// fprintf(stderr, "allocate pointer %p\n", json);
|
||||
memset(json, 0x00, len+1);
|
||||
QByteArray const &a = localGitRepos[localGitRepo]->GetFileMenu().constData();
|
||||
if (a.isValidUtf8()) {
|
||||
memcpy(json, a.constData(), std::min(len, (int)a.size()));
|
||||
}
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
char const *GetFileName(char const *localGitRepo, char const *fileId) {
|
||||
QByteArray const &a = localGitRepos[localGitRepo]->GetFileName(fileId);
|
||||
if (a.isValidUtf8()) {
|
||||
char *c = new char[a.size() + 1];
|
||||
memset(c, 0x00, a.size() + 1);
|
||||
memcpy(c, a.constData(), a.size());
|
||||
return c;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int32_t GetFileSize(char const *localGitRepo, char const *fileId) {
|
||||
return localGitRepos[localGitRepo]->GetFileSize(fileId);
|
||||
}
|
||||
|
||||
char const *GetFile(char const *localGitRepo, char const *fileId) {
|
||||
QByteArray const &a = localGitRepos[localGitRepo]->GetFile(fileId);
|
||||
if (a.isValidUtf8()) {
|
||||
char *c = new char[a.size() + 1];
|
||||
memset(c, 0x00, a.size() + 1);
|
||||
memcpy(c, a.constData(), a.size());
|
||||
return c;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void DeleteMem(char *p) {
|
||||
if (p) {
|
||||
// fprintf(stderr, "delete pointer %p\n", p);
|
||||
delete p;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
#ifndef CALCULATOR_C_INTERFACE_LIB_H_INCLUDED
|
||||
#define CALCULATOR_C_INTERFACE_LIB_H_INCLUDED
|
||||
|
||||
#include "calculator_c_interface_lib_global.h"
|
||||
#include "tariff_calculator.h"
|
||||
|
||||
@ -10,6 +9,16 @@ typedef TariffCalculator *TariffCalculatorHandle;
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void DeleteMem(char *p) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
|
||||
// interface for menu of webpage
|
||||
char const *GetFileMenu(char const *localRepo) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int32_t GetFileMenuSize(char const *localRepo) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
char const *GetFileName(char const *localRepo, char const *fileId) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
int32_t GetFileSize(char const *localRepo, char const *fileId) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
char const *GetFile(char const *localRepo, char const *fileId) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
bool SetFile(char const *localRepo, char const *fileId, char const *json) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
|
||||
TariffCalculatorHandle NewTariffCalculator(void) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
void DeleteTariffCalculator(TariffCalculatorHandle handle) CALCULATOR_C_INTERFACE_LIB_EXPORT;
|
||||
|
||||
|
113
CalculatorCInterface/local_git_repository.cpp
Normal file
113
CalculatorCInterface/local_git_repository.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
#include "local_git_repository.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QJsonDocument>
|
||||
#include <QByteArray>
|
||||
#include <QRegularExpression>
|
||||
#include <QRegularExpressionMatch>
|
||||
#include <QFile>
|
||||
|
||||
LocalGitRepository::LocalGitRepository(QString const &localRepository)
|
||||
: m_localRepository(localRepository)
|
||||
, m_fileMenu("{}")
|
||||
, m_fileMenuSize(-1) {
|
||||
}
|
||||
|
||||
int32_t LocalGitRepository::GetFileMenuSize() const {
|
||||
if (m_fileMenuSize == -1) {
|
||||
GetFileMenu();
|
||||
}
|
||||
return m_fileMenuSize;
|
||||
}
|
||||
|
||||
QByteArray const &LocalGitRepository::GetFileMenu() const {
|
||||
if (m_fileMenu.toStdString() == "{}") {
|
||||
QFile f(QDir::cleanPath(QString(baseDir) + m_localRepository + QDir::separator()
|
||||
+ "etc/psa_webinterface/menu_config.json"));
|
||||
if (f.exists()) {
|
||||
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
m_fileMenu = QTextStream(&f).readAll().toUtf8();
|
||||
|
||||
if (m_fileMenu.isValidUtf8()) {
|
||||
QString const &s = QString::fromUtf8(m_fileMenu);
|
||||
m_fileMenuSize = s.toLocal8Bit().size();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return m_fileMenu;
|
||||
}
|
||||
|
||||
QByteArray LocalGitRepository::GetFileName(QString const &fId) {
|
||||
QRegularExpressionMatch match;
|
||||
static const QRegularExpression re("(master|[0-9]+)");
|
||||
if (fId.lastIndexOf(re, -1, &match) != -1) {
|
||||
int idx = fId.indexOf("/");
|
||||
if (idx != -1) {
|
||||
QString path = fId.mid(idx);
|
||||
idx = path.indexOf(":");
|
||||
if (idx != -1) {
|
||||
path = path.mid(0, idx);
|
||||
QString s = match.captured(match.lastCapturedIndex());
|
||||
if (s != "master") {
|
||||
if (fId.contains("psa_tariff")) {
|
||||
QString fn(QDir::cleanPath(
|
||||
QString(baseDir) + QDir::separator() +
|
||||
QString(m_localRepository) + QDir::separator()
|
||||
+ path + QDir::separator()
|
||||
+ QString("tariff%1.json").arg(s.toUInt(), 2, 10, QChar('0'))));
|
||||
return fn.toUtf8();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
QByteArray LocalGitRepository::GetFile(QString const &fId) {
|
||||
QByteArray a = GetFileName(fId);
|
||||
if (a.isValidUtf8()) {
|
||||
QFile fn(a);
|
||||
if (fn.exists()) {
|
||||
if (fn.open(QIODevice::ReadOnly)) {
|
||||
return fn.readAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QByteArray("{}");
|
||||
}
|
||||
|
||||
int32_t LocalGitRepository::GetFileSize(QString const &fId) {
|
||||
QByteArray a = GetFileName(fId);
|
||||
if (a.isValidUtf8()) {
|
||||
QFile fn(a);
|
||||
if (fn.exists()) {
|
||||
return fn.size();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool LocalGitRepository::SetFile(QString const &fId, QByteArray const &json) {
|
||||
QByteArray a = GetFileName(fId);
|
||||
if (a.isValidUtf8()) {
|
||||
QFile fn(a);
|
||||
if (fn.exists()) {
|
||||
if (fn.open(QIODevice::WriteOnly)) {
|
||||
quint64 bytesWritten = 0;
|
||||
qint64 bytesToWrite = a.size();
|
||||
while (bytesToWrite > 0 &&
|
||||
(bytesWritten = fn.write(json.constData(), bytesToWrite)) != -1) {
|
||||
bytesToWrite -= bytesWritten;
|
||||
}
|
||||
return (bytesToWrite == 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
30
CalculatorCInterface/local_git_repository.h
Normal file
30
CalculatorCInterface/local_git_repository.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef LOCAL_GIT_REPOSITORY_H_INCLUDED
|
||||
#define LOCAL_GIT_REPOSITORY_H_INCLUDED
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
|
||||
class LocalGitRepository {
|
||||
QString m_localRepository;
|
||||
mutable QByteArray m_fileMenu;
|
||||
mutable int32_t m_fileMenuSize;
|
||||
public:
|
||||
LocalGitRepository(QString const &localRepository);
|
||||
|
||||
static char const constexpr *baseDir = "H:\\";
|
||||
QString gitRepository() const { return m_localRepository; }
|
||||
|
||||
QByteArray const &GetFileMenu() const;
|
||||
int32_t GetFileMenuSize() const;
|
||||
QByteArray GetFileName(QString const &fileId);
|
||||
int32_t GetFileSize(QString const &fileId);
|
||||
QByteArray GetFile(QString const &fileId);
|
||||
|
||||
bool SetFile(QString const &fileId, QByteArray const &json);
|
||||
};
|
||||
|
||||
#endif // LOCAL_GIT_REPOSITORY_H_INCLUDED
|
Loading…
x
Reference in New Issue
Block a user