mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-15 07:56:11 +08:00
cLib: Move some common parts to common.c
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
CC=clang
|
||||
|
||||
test: gitjournal.c test.c keygen.c
|
||||
$(CC) -o test -g test.c gitjournal.c keygen.c -lgit2 -lssl -lcrypto
|
||||
test: gitjournal.c test.c keygen.c common.c
|
||||
$(CC) -o test -g test.c common.c gitjournal.c keygen.c -lgit2 -lssl -lcrypto
|
||||
|
65
gj_common/common.c
Normal file
65
gj_common/common.c
Normal file
@ -0,0 +1,65 @@
|
||||
#include "gitjournal.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <errno.h>
|
||||
#include <git2.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
void gj_log_internal(const char *format, ...)
|
||||
{
|
||||
char buffer[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsprintf(buffer, format, args);
|
||||
gj_log(buffer);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
gj_error *gj_error_info(int err)
|
||||
{
|
||||
if (err == 0)
|
||||
return NULL;
|
||||
|
||||
gj_error *error = (gj_error *)malloc(sizeof(gj_error));
|
||||
error->message_allocated = false;
|
||||
if (err >= GJ_ERR_FIRST && err <= GJ_ERR_LAST)
|
||||
{
|
||||
switch (err)
|
||||
{
|
||||
case GJ_ERR_EMPTY_COMMIT:
|
||||
error->code = err;
|
||||
error->message = "Empty Commit";
|
||||
break;
|
||||
|
||||
case GJ_ERR_PULL_INVALID_STATE:
|
||||
error->code = err;
|
||||
error->message = "GitPull Invalid State";
|
||||
break;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
const git_error *e = git_error_last();
|
||||
if (e)
|
||||
{
|
||||
error->code = e->klass;
|
||||
error->message = (char *)malloc(strlen(e->message));
|
||||
strcpy(error->message, e->message);
|
||||
error->message_allocated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
error->code = 1000;
|
||||
error->message = "Unknown Message";
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
void gj_error_free(const gj_error *err)
|
||||
{
|
||||
if (err->message_allocated)
|
||||
free(err->message);
|
||||
free((void *)err);
|
||||
}
|
@ -1,78 +1,14 @@
|
||||
#include "gitjournal.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <git2.h>
|
||||
|
||||
#define GJ_ERR_FIRST -954
|
||||
#define GJ_ERR_EMPTY_COMMIT -954
|
||||
#define GJ_ERR_PULL_INVALID_STATE -955
|
||||
#define GJ_ERR_LAST -955
|
||||
|
||||
#define UNUSED(x) (void)(x)
|
||||
|
||||
void gj_log_internal(const char *format, ...)
|
||||
{
|
||||
char buffer[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsprintf(buffer, format, args);
|
||||
gj_log(buffer);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
gj_error *gj_error_info(int err)
|
||||
{
|
||||
if (err == 0)
|
||||
return NULL;
|
||||
|
||||
gj_error *error = (gj_error *)malloc(sizeof(gj_error));
|
||||
error->message_allocated = false;
|
||||
if (err >= GJ_ERR_FIRST && err <= GJ_ERR_LAST)
|
||||
{
|
||||
switch (err)
|
||||
{
|
||||
case GJ_ERR_EMPTY_COMMIT:
|
||||
error->code = err;
|
||||
error->message = "Empty Commit";
|
||||
break;
|
||||
|
||||
case GJ_ERR_PULL_INVALID_STATE:
|
||||
error->code = err;
|
||||
error->message = "GitPull Invalid State";
|
||||
break;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
const git_error *e = git_error_last();
|
||||
if (e)
|
||||
{
|
||||
error->code = e->klass;
|
||||
error->message = (char *)malloc(strlen(e->message));
|
||||
strcpy(error->message, e->message);
|
||||
error->message_allocated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
error->code = 1000;
|
||||
error->message = "Unknown Message";
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
void gj_error_free(const gj_error *err)
|
||||
{
|
||||
if (err->message_allocated)
|
||||
free(err->message);
|
||||
free((void *)err);
|
||||
}
|
||||
|
||||
int match_cb(const char *path, const char *spec, void *payload)
|
||||
{
|
||||
UNUSED(spec);
|
||||
|
@ -33,7 +33,14 @@ void gj_error_free(const gj_error *err);
|
||||
// This must be implemented by you
|
||||
void gj_log(const char *message);
|
||||
|
||||
void gj_log_internal(const char *format, ...);
|
||||
|
||||
int gj_generate_ssh_keys(const char *private_key_path,
|
||||
const char *public_key_path, const char *comment);
|
||||
|
||||
#define GJ_ERR_FIRST -954
|
||||
#define GJ_ERR_EMPTY_COMMIT -954
|
||||
#define GJ_ERR_PULL_INVALID_STATE -955
|
||||
#define GJ_ERR_LAST -955
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user