From: Bastian Dehn Date: Mon, 9 Dec 2024 17:37:38 +0000 (+0100) Subject: read xml file into memory X-Git-Tag: 1.3.2^2~1^2~1 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=99dac553d7ca9e15aa832abc45d7cd36947e5d7f;p=feierabend.git read xml file into memory --- diff --git a/src/xml.c b/src/xml.c index 27117e8..c9f71f3 100644 --- a/src/xml.c +++ b/src/xml.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "xml.h" #include "feierabend.h" @@ -14,6 +15,19 @@ #define MAX_STRING_LENGTH 36 +void init_memFile(memFile* mem) { + mem->data = malloc(sizeof(char*)); + mem->size = malloc(sizeof(size_t)); +} + +void free_memFile(memFile* mem) { + free(*mem->data); + free(mem->data); + free(mem->size); + mem->data = NULL; + mem->size = NULL; +} + void init_feierabend(feierabend* fabend) { fabend->now = malloc(sizeof(time_t)); @@ -37,6 +51,22 @@ void free_feierabend(feierabend* fabend) fabend->timestr = NULL; } +void readFileIntoMemory(const char* file_path, memFile* mem) +{ + char* buf = malloc(sizeof(char)); + FILE* memstream = open_memstream(mem->data, mem->size); + FILE* file = fopen(file_path, "r"); + + while(fread(buf, sizeof(char), 1, file) != 0) { + fwrite(buf, sizeof(char), 1, memstream); + } + fclose(file); + fclose(memstream); + + free(buf); + buf = NULL; +} + void initTimeAcount() { xmlChar* xmlElemName = NULL; @@ -364,6 +394,10 @@ void addEntry(const char* date, int end_min, const char* file_path) { + memFile* fileContent = malloc(sizeof(memFile)); + init_memFile(fileContent); + readFileIntoMemory(file_path, fileContent); + char* saldostr = malloc(sizeof(char) * MAX_STRING_LENGTH); memset(saldostr, 0, MAX_STRING_LENGTH); readLastSaldo(file_path, saldostr); @@ -376,7 +410,7 @@ void addEntry(const char* date, free(xmlElemContent); xmlElemContent = NULL; - xmlTextReaderPtr xmlreader = xmlReaderForFile(file_path, "UTF-8", 0); + xmlTextReaderPtr xmlreader = xmlReaderForMemory(*fileContent->data, *fileContent->size, NULL, "UTF-8", 0); xmlNodePtr xmlnode = NULL; xmlTextWriterStartDocument(xmlwriter, "1.0", "UTF-8", NULL); @@ -410,6 +444,9 @@ void addEntry(const char* date, xmlCleanupCharEncodingHandlers(); xmlDictCleanup(); + free_memFile(fileContent); + free(fileContent); + fileContent = NULL; free(xmlElemContent); xmlElemContent = NULL; free(saldo); diff --git a/src/xml.h b/src/xml.h index bf00052..1198925 100644 --- a/src/xml.h +++ b/src/xml.h @@ -1,6 +1,11 @@ #ifndef XML_H #define XML_H +typedef struct { + char** data; + size_t* size; +} memFile; + void initTimeAcount(); void createEntry(int begin_hour, int begin_min,