cLib: Move some common parts to common.c

This commit is contained in:
Vishesh Handa
2019-05-29 14:18:40 +02:00
parent 8c7e479420
commit c3d9d653f7
4 changed files with 76 additions and 68 deletions

View File

@ -1,4 +1,4 @@
CC=clang CC=clang
test: gitjournal.c test.c keygen.c test: gitjournal.c test.c keygen.c common.c
$(CC) -o test -g test.c gitjournal.c keygen.c -lgit2 -lssl -lcrypto $(CC) -o test -g test.c common.c gitjournal.c keygen.c -lgit2 -lssl -lcrypto

65
gj_common/common.c Normal file
View 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);
}

View File

@ -1,78 +1,14 @@
#include "gitjournal.h" #include "gitjournal.h"
#include <stdio.h>
#include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <string.h>
#include <stdio.h>
#include <git2.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) #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) int match_cb(const char *path, const char *spec, void *payload)
{ {
UNUSED(spec); UNUSED(spec);

View File

@ -33,7 +33,14 @@ void gj_error_free(const gj_error *err);
// This must be implemented by you // This must be implemented by you
void gj_log(const char *message); void gj_log(const char *message);
void gj_log_internal(const char *format, ...);
int gj_generate_ssh_keys(const char *private_key_path, int gj_generate_ssh_keys(const char *private_key_path,
const char *public_key_path, const char *comment); 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 #endif