base: read config file line by line

issue #2
This commit is contained in:
tylen 2024-09-26 12:32:48 +00:00 committed by Vasily Davydov
parent 807abee5f6
commit 06eee5bde7
4 changed files with 103 additions and 32 deletions

View File

@ -4,6 +4,7 @@
#include "cmd.h" #include "cmd.h"
#include "tools/log.h" #include "tools/log.h"
#include "tools/alloc_wrappers.h"
#include "platform/operations.h" #include "platform/operations.h"
#include "platform/base.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); const char* full_path = convert_relative_to_full_path(relative_project_path);
LOG_INFO("Project full path: %s", full_path); LOG_INFO("Project full path: %s", full_path);
identify_platfrom_from_project(full_path); identify_platfrom_from_project(full_path);
cdo_free((void*)full_path);
return 0; return 0;
} }

View File

@ -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); 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); LOG_DEBUG("Constructed git config file path: %s", git_config_path_buffer->data);
FILE* git_config_fp = fopen(git_config_path_buffer->data, "r"); Lines* config_file_lines = read_file_lines(git_config_path_buffer->data);
if (git_config_fp == NULL) { lines_flush(config_file_lines);
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); 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; return PL_UNRECOGNIZED;
} }

View File

@ -4,6 +4,10 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
Buffer* buffer_create(size_t capacity) { Buffer* buffer_create(size_t capacity) {
Buffer* buffer = (Buffer*)cdo_malloc(sizeof(Buffer)); Buffer* buffer = (Buffer*)cdo_malloc(sizeof(Buffer));
@ -38,13 +42,92 @@ void buffer_flush(Buffer* buffer) {
} }
void buffer_free(Buffer* buffer) { void buffer_free(Buffer* buffer) {
if (!buffer->data || !buffer) {
LOG_WARNING("Trying to free NULL pointer");
}
cdo_free(buffer->data); cdo_free(buffer->data);
buffer->data = NULL; buffer->data = NULL;
cdo_free(buffer); cdo_free(buffer);
buffer = NULL; 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;
}

View File

@ -9,10 +9,23 @@ typedef struct {
size_t capacity; size_t capacity;
} Buffer; } Buffer;
typedef struct {
Buffer** lines;
size_t size;
size_t capacity;
} Lines;
Buffer* buffer_create(size_t capacity); Buffer* buffer_create(size_t capacity);
void buffer_append(Buffer* buffer, const char* str); void buffer_append(Buffer* buffer, const char* str);
void buffer_append_c(Buffer* buffer, char c); void buffer_append_c(Buffer* buffer, char c);
void buffer_flush(Buffer* buffer); void buffer_flush(Buffer* buffer);
void buffer_free(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__*/ #endif /*__BUFFER_H__*/