From 807abee5f6b850d8bd4ad3adde7331d125157ba3 Mon Sep 17 00:00:00 2001 From: tylen Date: Wed, 25 Sep 2024 20:49:31 +0000 Subject: [PATCH] platform: read config file line by line issue #2 --- src/cdo.c | 5 +++- src/platform/base.c | 57 +++++++++++++++++++++++++++++++++++++++ src/platform/base.h | 16 +++++++++++ src/platform/operations.c | 4 --- src/platform/operations.h | 4 --- src/tools/buffer.c | 9 +++++++ src/tools/buffer.h | 1 + 7 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 src/platform/base.c create mode 100644 src/platform/base.h diff --git a/src/cdo.c b/src/cdo.c index 682d670..bdfe7b2 100644 --- a/src/cdo.c +++ b/src/cdo.c @@ -5,6 +5,7 @@ #include "cmd.h" #include "tools/log.h" #include "platform/operations.h" +#include "platform/base.h" static void __attribute__((constructor)) init_cdo(void) { atexit(exit_cmd); @@ -15,6 +16,8 @@ static void __attribute__((constructor)) init_cdo(void) { int main (int argc, char *argv[]) { struct ArgList* args = parse_args(argc, argv); const char* relative_project_path = extract_value_from_arg(args, ARG_PROJECT); - LOG_INFO("Project full path: %s", convert_relative_to_full_path(relative_project_path)); + const char* full_path = convert_relative_to_full_path(relative_project_path); + LOG_INFO("Project full path: %s", full_path); + identify_platfrom_from_project(full_path); return 0; } diff --git a/src/platform/base.c b/src/platform/base.c new file mode 100644 index 0000000..ad4674c --- /dev/null +++ b/src/platform/base.c @@ -0,0 +1,57 @@ +#include "base.h" +#include "../tools/buffer.h" +#include "../tools/log.h" + +#include +#include +#include +#include +#include +#include + +const char* SUPPORTED_PLATFORM_NAMES [] = { + "github.com", + "gitlab.com" +}; + +PlaformId identify_platfrom_from_project(const char* project_path) { + if (project_path == NULL) { + LOG_ERROR_EXIT("No path supplied for project."); + } + Buffer* git_config_path_buffer = buffer_create(strlen(project_path)); + buffer_append(git_config_path_buffer, project_path); + buffer_append(git_config_path_buffer, PATH_TO_GIT_CONFIG_RELATIVE_TO_PROJ); + LOG_DEBUG("Constructed git config file path: %s", git_config_path_buffer->data); + + FILE* git_config_fp = fopen(git_config_path_buffer->data, "r"); + if (git_config_fp == NULL) { + LOG_ERROR_EXIT("Could not open file %s. ERRNO %d; %s", + git_config_path_buffer->data, + errno, + strerror(errno)); + } + struct stat statbuf; + if (stat(git_config_path_buffer->data, &statbuf) == -1) { + LOG_ERROR_EXIT("Could not stat file %s.ERRNO %d; %s", + git_config_path_buffer->data, + errno, + strerror(errno)); + } + off_t file_size = statbuf.st_size; + buffer_free(git_config_path_buffer); + // TODO: Remove magic number from creating buffer for git config line + Buffer* line_buffer = buffer_create(128); + for (off_t i = 0; i < file_size; i++) { + unsigned char current_char = fgetc(git_config_fp); + if (current_char == '\n') { + buffer_append_c(line_buffer, '\0'); + LOG_INFO("%s", line_buffer->data); + line_buffer->size = 0; + } else { + buffer_append_c(line_buffer, current_char); + } + } + fclose(git_config_fp); + buffer_free(line_buffer); + return PL_UNRECOGNIZED; +} \ No newline at end of file diff --git a/src/platform/base.h b/src/platform/base.h new file mode 100644 index 0000000..c759246 --- /dev/null +++ b/src/platform/base.h @@ -0,0 +1,16 @@ +#ifndef __PLATFORM_BASE_H__ +#define __PLATFORM_BASE_H__ + +#define PATH_TO_GIT_CONFIG_RELATIVE_TO_PROJ "/.git/config" + +typedef enum PlaformId { + PL_GITHUB, + PL_GITLAB, + PL_UNRECOGNIZED +} PlaformId; + +extern const char* SUPPORTED_PLATFORM_NAMES []; + +PlaformId identify_platfrom_from_project(const char* project_path); + +#endif /*__PLATFORM_BASE_H__*/ \ No newline at end of file diff --git a/src/platform/operations.c b/src/platform/operations.c index b63731a..cd5ba55 100644 --- a/src/platform/operations.c +++ b/src/platform/operations.c @@ -44,7 +44,3 @@ const char* convert_relative_to_full_path(const char* relative_path) { return full_path; } - -const char* get_platform_name_from_project(const char* project_path) { - return NULL; -} diff --git a/src/platform/operations.h b/src/platform/operations.h index 5a4d6c5..779fd11 100644 --- a/src/platform/operations.h +++ b/src/platform/operations.h @@ -1,10 +1,6 @@ #ifndef __PLATFORM_OPERATIONS_H__ #define __PLATFORM_OPERATIONS_H__ -#define PATH_TO_GIT_CONFIG_RELATIVE_TO_PROJ ".git/config" - const char* convert_relative_to_full_path(const char* relative_path); -const char* get_platform_name_from_project(const char* project_path); - #endif /*__PLATFORM_OPERATIONS_H__*/ \ No newline at end of file diff --git a/src/tools/buffer.c b/src/tools/buffer.c index e53458d..bebc3f9 100644 --- a/src/tools/buffer.c +++ b/src/tools/buffer.c @@ -23,6 +23,15 @@ void buffer_append(Buffer* buffer, const char* str) { buffer->size += len; } +void buffer_append_c(Buffer* buffer, char c) { + if (buffer->size + 1 >= buffer->capacity) { + buffer->capacity *= 2; + buffer->data = (char*)cdo_realloc(buffer->data, buffer->capacity); + } + *(buffer->data + buffer->size) = c; + buffer->size++; +} + void buffer_flush(Buffer* buffer) { printf("%s", buffer->data); buffer_free(buffer); diff --git a/src/tools/buffer.h b/src/tools/buffer.h index 8dfbbf1..ce915cc 100644 --- a/src/tools/buffer.h +++ b/src/tools/buffer.h @@ -11,6 +11,7 @@ typedef struct { Buffer* buffer_create(size_t capacity); void buffer_append(Buffer* buffer, const char* str); +void buffer_append_c(Buffer* buffer, char c); void buffer_flush(Buffer* buffer); void buffer_free(Buffer* buffer);