From 06eee5bde701a127ad05dc7f073e77f21a56df59 Mon Sep 17 00:00:00 2001 From: tylen Date: Thu, 26 Sep 2024 12:32:48 +0000 Subject: [PATCH] base: read config file line by line issue #2 --- src/cdo.c | 2 + src/platform/base.c | 31 +--------------- src/tools/buffer.c | 89 +++++++++++++++++++++++++++++++++++++++++++-- src/tools/buffer.h | 13 +++++++ 4 files changed, 103 insertions(+), 32 deletions(-) diff --git a/src/cdo.c b/src/cdo.c index bdfe7b2..0f8ccf6 100644 --- a/src/cdo.c +++ b/src/cdo.c @@ -4,6 +4,7 @@ #include "cmd.h" #include "tools/log.h" +#include "tools/alloc_wrappers.h" #include "platform/operations.h" #include "platform/base.h" @@ -19,5 +20,6 @@ int main (int argc, char *argv[]) { 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); + cdo_free((void*)full_path); return 0; } diff --git a/src/platform/base.c b/src/platform/base.c index ad4674c..ef2bf5a 100644 --- a/src/platform/base.c +++ b/src/platform/base.c @@ -23,35 +23,8 @@ PlaformId identify_platfrom_from_project(const char* 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; + Lines* config_file_lines = read_file_lines(git_config_path_buffer->data); + lines_flush(config_file_lines); 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/tools/buffer.c b/src/tools/buffer.c index bebc3f9..293539e 100644 --- a/src/tools/buffer.c +++ b/src/tools/buffer.c @@ -4,6 +4,10 @@ #include #include +#include +#include +#include +#include Buffer* buffer_create(size_t capacity) { Buffer* buffer = (Buffer*)cdo_malloc(sizeof(Buffer)); @@ -38,13 +42,92 @@ void buffer_flush(Buffer* buffer) { } void buffer_free(Buffer* buffer) { - if (!buffer->data || !buffer) { - LOG_WARNING("Trying to free NULL pointer"); - } cdo_free(buffer->data); buffer->data = NULL; cdo_free(buffer); buffer = NULL; } +Lines* lines_create(size_t capacity) { + Lines* _lines = (Lines*)cdo_malloc(sizeof(Lines)); + _lines->lines = (Buffer**)cdo_malloc(capacity * sizeof(Buffer*)); + _lines->size = 0; + _lines->capacity = capacity; + return _lines; +} +Lines* read_file_lines(const char* file_path) { + FILE* _file_pointer = fopen(file_path, "r"); + if (_file_pointer == NULL) { + LOG_ERROR_EXIT("Could not open file %s. ERRNO %d; %s", + file_path, + errno, + strerror(errno)); + } + struct stat statbuf; + if (stat(file_path, &statbuf) == -1) { + LOG_ERROR_EXIT("Could not stat file %s.ERRNO %d; %s", + file_path, + errno, + strerror(errno)); + } + off_t file_size = statbuf.st_size; + size_t line_numbers = 0; + for (off_t i = 0; i < file_size; i++) { + unsigned char current_char = fgetc(_file_pointer); + if (current_char == '\n') line_numbers++; + } + LOG_DEBUG("file %s has %lu lines.", file_path, line_numbers); + rewind(_file_pointer); + + Lines* file_lines = lines_create(line_numbers); + // TODO: Remove magic number from creating line buffer + Buffer* line_buffer = buffer_create(1); + for (off_t i = 0; i < file_size; i++) { + unsigned char current_char = fgetc(_file_pointer); + if (current_char == '\n') { + buffer_append_c(line_buffer, '\0'); + lines_append(file_lines, line_buffer); + line_buffer->size = 0; + } else { + buffer_append_c(line_buffer, current_char); + } + } + buffer_free(line_buffer); + fclose(_file_pointer); + + return file_lines; +} + +void lines_append(Lines* lines, Buffer* buffer) { + if (lines->size + 1 >= lines->capacity) { + lines->capacity *= 2; + lines->lines = (Buffer**)cdo_realloc(lines->lines, lines->capacity * sizeof(Buffer*)); + } + Buffer* new_buffer = buffer_create(buffer->size + 1); + new_buffer->size = buffer->size; + memcpy(new_buffer->data, buffer->data, buffer->size + 1); + lines->lines[lines->size] = new_buffer; + lines->size++; +} + +void lines_flush(Lines* lines) { + for(size_t i = 0; i < lines->size; i++) { + printf("%s\n", lines->lines[i]->data); + buffer_free(lines->lines[i]); + } + cdo_free(lines->lines); + lines->lines = NULL; + cdo_free(lines); + lines = NULL; +} + +void lines_free(Lines* lines) { + for(size_t i = 0; i < lines->size; i++) { + buffer_free(lines->lines[i]); + } + cdo_free(lines->lines); + lines->lines = NULL; + cdo_free(lines); + lines = NULL; +} diff --git a/src/tools/buffer.h b/src/tools/buffer.h index ce915cc..254edc3 100644 --- a/src/tools/buffer.h +++ b/src/tools/buffer.h @@ -9,10 +9,23 @@ typedef struct { size_t capacity; } Buffer; +typedef struct { + Buffer** lines; + size_t size; + size_t capacity; +} Lines; + 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); +Lines* lines_create(size_t capacity); +Lines* read_file_lines(const char* file_path); +void lines_append(Lines* lines, Buffer* buffer); +void lines_flush(Lines* lines); +void lines_free(Lines* lines); + + #endif /*__BUFFER_H__*/