ATBTariffCalculator/CalculatorCInterface/calculator_c_interface_lib.cpp

149 lines
4.2 KiB
C++
Raw Permalink Normal View History

2024-03-04 16:31:14 +01:00
#include "calculator_c_interface_lib.h"
2024-03-28 16:45:46 +01:00
#include "local_git_repository.h"
#include "git_library.h"
2024-03-04 16:31:14 +01:00
2024-03-13 13:17:02 +01:00
//#include <git2/common.h>
#include <QMap>
#include <QString>
#include <QDir>
2024-03-21 16:57:53 +01:00
#include <QFile>
#include <QByteArray>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
2024-03-14 15:04:06 +01:00
#include <mutex>
2024-03-13 13:17:02 +01:00
2024-03-11 14:27:33 +01:00
#include <git2.h>
#include <stdio.h>
2024-03-13 13:17:02 +01:00
#include <stdint.h>
2024-03-21 16:57:53 +01:00
#include <algorithm>
2024-03-11 14:27:33 +01:00
2024-04-03 16:29:58 +02:00
//static QMap<QString, git_repository *> customerRepoMap;
//static std::mutex m;
2024-03-13 13:17:02 +01:00
#ifndef _WIN32
# include <unistd.h>
#endif
#include <errno.h>
2024-03-04 16:31:14 +01:00
#ifdef __cplusplus
extern "C" {
#endif
2024-03-13 13:17:02 +01:00
2024-03-11 14:27:33 +01:00
TariffCalculatorHandle NewTariffCalculator(void) {
2024-03-11 12:14:28 +01:00
return new TariffCalculator();
}
2024-03-04 16:31:14 +01:00
2024-03-11 14:27:33 +01:00
void DeleteTariffCalculator(TariffCalculatorHandle handle) {
2024-03-11 12:14:28 +01:00
delete handle;
2024-03-04 16:31:14 +01:00
}
2024-03-28 16:45:46 +01:00
int InitGitLibraryInternal(void) {
return GitLibrary::Init();
2024-03-11 14:27:33 +01:00
}
2024-03-28 16:45:46 +01:00
int ShutdownGitLibraryInternal(void) {
LocalGitRepository::DestroyAllRepositories();
return GitLibrary::Shutdown();
2024-03-11 14:27:33 +01:00
}
2024-03-28 16:45:46 +01:00
int CloneRepositoryInternal(char const *url, char const *local_path) {
return GitLibrary::CloneRepository(url, local_path);
2024-03-11 14:27:33 +01:00
}
2024-03-11 12:14:28 +01:00
2024-04-03 16:29:58 +02:00
int CheckoutRepositoryInternal(char const *url, char const *localRepoName, char const *branchName) {
return GitLibrary::CheckoutRepository(url, localRepoName, branchName);
2024-03-13 13:17:02 +01:00
}
2024-04-03 16:29:58 +02:00
int CommitFileInternal(char const *localRepoName, char const *branchName,
char const *fName, char const *commitMessage) {
return GitLibrary::CommitFile(localRepoName, branchName, fName, commitMessage);
2024-03-13 13:17:02 +01:00
}
2024-04-03 16:29:58 +02:00
int PushRepositoryInternal(char const *localRepoName, char const *branchName,
char const *userName, char const *userPassword) {
return GitLibrary::PushRepository(localRepoName, branchName, userName, userPassword);
}
2024-03-14 15:04:06 +01:00
2024-04-15 16:58:00 +02:00
int PullRepositoryInternal(char const *localRepoName, char const *remoteRepoName) {
return GitLibrary::PullRepository(localRepoName, remoteRepoName);
2024-03-14 15:04:06 +01:00
}
2024-03-21 16:57:53 +01:00
#include <local_git_repository.h>
2024-03-14 15:04:06 +01:00
2024-03-21 16:57:53 +01:00
2024-03-27 08:40:16 +01:00
void SetReposRootDirectoryInternal(char const *p) {
LocalGitRepository::SetReposRootDirectory(QString::fromUtf8(p));
}
char const *GetReposRootDirectoryInternal() {
2024-03-28 16:45:46 +01:00
return (char const *)LocalGitRepository::GetReposRootDirectory().constData();
2024-03-27 08:40:16 +01:00
}
char const *GetLocalRepositoryPathInternal(char const *localGitRepo) {
2024-03-28 16:45:46 +01:00
return (char const *)LocalGitRepository::GetInstance(localGitRepo)->localRepositoryPath().constData();
2024-03-27 08:40:16 +01:00
}
2024-03-25 16:13:26 +01:00
int32_t GetFileMenuSizeInternal(char const *localGitRepo) {
2024-03-28 16:45:46 +01:00
return LocalGitRepository::GetInstance(localGitRepo)->GetFileMenuSizeInternal();
2024-03-21 16:57:53 +01:00
}
2024-03-25 16:13:26 +01:00
char const *GetFileMenuInternal(const char *localGitRepo) {
2024-03-28 16:45:46 +01:00
QByteArray const &a = LocalGitRepository::GetInstance(localGitRepo)->GetFileMenuInternal().constData();
2024-03-22 13:46:54 +01:00
if (a.isValidUtf8()) {
2024-03-25 16:13:26 +01:00
int const len = GetFileMenuSizeInternal(localGitRepo);
2024-03-22 13:46:54 +01:00
if (len > 0) {
char *json = new char [len+1];
// fprintf(stderr, "allocate pointer %p\n", json);
memset(json, 0x00, len+1);
2024-03-21 16:57:53 +01:00
memcpy(json, a.constData(), std::min(len, (int)a.size()));
2024-03-22 13:46:54 +01:00
return json;
2024-03-21 16:57:53 +01:00
}
}
2024-03-22 13:46:54 +01:00
return nullptr;
2024-03-21 16:57:53 +01:00
}
2024-03-25 16:13:26 +01:00
char const *GetFileNameInternal(char const *localGitRepo, char const *fileId) {
2024-03-28 16:45:46 +01:00
QByteArray const &a = LocalGitRepository::GetInstance(localGitRepo)->GetFileNameInternal(fileId);
2024-03-21 16:57:53 +01:00
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) {
2024-03-28 16:45:46 +01:00
return LocalGitRepository::GetInstance(localGitRepo)->GetFileSize(fileId);
2024-03-21 16:57:53 +01:00
}
2024-03-25 16:13:26 +01:00
char const *GetFileInternal(char const *localGitRepo, char const *fileId) {
2024-03-28 16:45:46 +01:00
QByteArray const &a = LocalGitRepository::GetInstance(localGitRepo)->GetFileInternal(fileId);
2024-03-21 16:57:53 +01:00
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;
}
2024-03-25 16:13:26 +01:00
bool SetFileInternal(char const *localGitRepo, char const *fileId, char const *json, int size) {
2024-03-28 16:45:46 +01:00
return LocalGitRepository::GetInstance(localGitRepo)->SetFileInternal(QString(fileId), QByteArray(json, size));
2024-03-25 16:13:26 +01:00
}
2024-03-21 16:57:53 +01:00
void DeleteMem(char *p) {
if (p) {
// fprintf(stderr, "delete pointer %p\n", p);
delete p;
}
}
2024-03-14 15:04:06 +01:00
2024-03-04 16:31:14 +01:00
#ifdef __cplusplus
}
#endif