ATBTariffCalculator/CalculatorCInterface/calculator_c_interface_lib.cpp
2024-04-03 16:29:58 +02:00

151 lines
4.3 KiB
C++

#include "calculator_c_interface_lib.h"
#include "local_git_repository.h"
#include "git_library.h"
//#include <git2/common.h>
#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;
#ifndef _WIN32
# include <unistd.h>
#endif
#include <errno.h>
#ifdef __cplusplus
extern "C" {
#endif
TariffCalculatorHandle NewTariffCalculator(void) {
return new TariffCalculator();
}
void DeleteTariffCalculator(TariffCalculatorHandle handle) {
delete handle;
}
int InitGitLibraryInternal(void) {
return GitLibrary::Init();
}
int ShutdownGitLibraryInternal(void) {
LocalGitRepository::DestroyAllRepositories();
return GitLibrary::Shutdown();
}
int CloneRepositoryInternal(char const *url, char const *local_path) {
return GitLibrary::CloneRepository(url, local_path);
}
int CheckoutRepositoryInternal(char const *url, char const *localRepoName, char const *branchName) {
return GitLibrary::CheckoutRepository(url, localRepoName, branchName);
}
int CommitFileInternal(char const *localRepoName, char const *branchName,
char const *fName, char const *commitMessage) {
return GitLibrary::CommitFile(localRepoName, branchName, fName, commitMessage);
}
int PushRepositoryInternal(char const *localRepoName, char const *branchName,
char const *userName, char const *userPassword) {
return GitLibrary::PushRepository(localRepoName, branchName, userName, userPassword);
}
int PullRepositoryInternal(char const *localRepoName,
char const *branchName, char const *user,
char const *password) {
return GitLibrary::PullRepository(localRepoName, branchName, user, password);
}
#include <local_git_repository.h>
void SetReposRootDirectoryInternal(char const *p) {
LocalGitRepository::SetReposRootDirectory(QString::fromUtf8(p));
}
char const *GetReposRootDirectoryInternal() {
return (char const *)LocalGitRepository::GetReposRootDirectory().constData();
}
char const *GetLocalRepositoryPathInternal(char const *localGitRepo) {
return (char const *)LocalGitRepository::GetInstance(localGitRepo)->localRepositoryPath().constData();
}
int32_t GetFileMenuSizeInternal(char const *localGitRepo) {
return LocalGitRepository::GetInstance(localGitRepo)->GetFileMenuSizeInternal();
}
char const *GetFileMenuInternal(const char *localGitRepo) {
QByteArray const &a = LocalGitRepository::GetInstance(localGitRepo)->GetFileMenuInternal().constData();
if (a.isValidUtf8()) {
int const len = GetFileMenuSizeInternal(localGitRepo);
if (len > 0) {
char *json = new char [len+1];
// fprintf(stderr, "allocate pointer %p\n", json);
memset(json, 0x00, len+1);
memcpy(json, a.constData(), std::min(len, (int)a.size()));
return json;
}
}
return nullptr;
}
char const *GetFileNameInternal(char const *localGitRepo, char const *fileId) {
QByteArray const &a = LocalGitRepository::GetInstance(localGitRepo)->GetFileNameInternal(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 LocalGitRepository::GetInstance(localGitRepo)->GetFileSize(fileId);
}
char const *GetFileInternal(char const *localGitRepo, char const *fileId) {
QByteArray const &a = LocalGitRepository::GetInstance(localGitRepo)->GetFileInternal(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;
}
bool SetFileInternal(char const *localGitRepo, char const *fileId, char const *json, int size) {
return LocalGitRepository::GetInstance(localGitRepo)->SetFileInternal(QString(fileId), QByteArray(json, size));
}
void DeleteMem(char *p) {
if (p) {
// fprintf(stderr, "delete pointer %p\n", p);
delete p;
}
}
#ifdef __cplusplus
}
#endif