platform: read config file line by line

issue #2
This commit is contained in:
tylen 2024-09-25 20:49:31 +00:00 committed by Vasily Davydov
parent 62496e12c1
commit 807abee5f6
7 changed files with 87 additions and 9 deletions

View File

@ -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;
}

57
src/platform/base.c Normal file
View File

@ -0,0 +1,57 @@
#include "base.h"
#include "../tools/buffer.h"
#include "../tools/log.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
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;
}

16
src/platform/base.h Normal file
View File

@ -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__*/

View File

@ -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;
}

View File

@ -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__*/

View File

@ -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);

View File

@ -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);