From 8279b5dcc0bb0e24535f239f300bba50237f1612 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Mon, 16 Feb 2026 16:54:12 +0100 Subject: [PATCH] add init xml with saldo --- src/feierabendxml.c | 12 +++++++++++- src/time_format.c | 13 +++++++++++++ src/time_format.h | 1 + src/xml.c | 13 +++---------- src/xml.h | 2 +- tests/time_format_tests.c | 24 +++++++++++++++++++++++- 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/feierabendxml.c b/src/feierabendxml.c index 2bf94b9..79c6c00 100644 --- a/src/feierabendxml.c +++ b/src/feierabendxml.c @@ -13,7 +13,17 @@ int main(const int argc, const char* argv[]) } if (strcmp(argv[1], "init") == 0) { - init_time_acount(); + if (argc == 2) { + init_time_acount(0, 0); + return 0; + } + + if (argc < 4) { + printf("ERROR: %s init [ ]\n", argv[0]); + return 1; + } + + init_time_acount(atoi(argv[2]), atoi(argv[3])); return 0; } diff --git a/src/time_format.c b/src/time_format.c index f546b97..2937803 100644 --- a/src/time_format.c +++ b/src/time_format.c @@ -122,6 +122,19 @@ char* get_time_str_from_tm(const struct tm time) return timestr; } +char* get_time_str_from_int(const uint8_t hour, const uint8_t min) +{ + char* timestr = malloc(sizeof(char) * MAX_TIME_STR_LENGTH); + if (timestr == NULL) + return NULL; + + memset(timestr, 0, MAX_TIME_STR_LENGTH); + + sprintf(timestr, "%02d:%02d", hour, min); + + return timestr; +} + time_t get_seconds_from_str(const char* timestr) { char* str = malloc(sizeof(char) * 7); diff --git a/src/time_format.h b/src/time_format.h index 2317c3c..313e6b4 100644 --- a/src/time_format.h +++ b/src/time_format.h @@ -10,6 +10,7 @@ char* get_weekday_str(const uint8_t wday); char* get_weekday_short_str(const uint8_t wday); char* get_date_str(const struct tm date); char* get_time_str_from_tm(const struct tm time); +char* get_time_str_from_int(const uint8_t hour, const uint8_t min); time_t get_seconds_from_str(const char* timestr); uint16_t get_year_from_str(const char* date); diff --git a/src/xml.c b/src/xml.c index af87c36..a903f25 100644 --- a/src/xml.c +++ b/src/xml.c @@ -247,7 +247,7 @@ void _write_entry_node(xmlTextWriterPtr xmlWriter, xmlTextWriterEndElement(xmlWriter); } -void init_time_acount() +void init_time_acount(const uint8_t hour, const uint8_t min) { xmlCharEncodingHandlerPtr encodingHandler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); xmlOutputBufferPtr xmloutbuffer = xmlOutputBufferCreateFd(1, encodingHandler); @@ -262,13 +262,8 @@ void init_time_acount() _write_element(xmlWriter, "zeitkonto"); - xmlChar* xmlElemName = xmlCharStrdup("anfangssaldo"); - xmlElemContent = xmlCharStrdup("00:00"); - xmlTextWriterWriteAttribute(xmlWriter, xmlElemName, xmlElemContent); - free(xmlElemName); - xmlElemName = NULL; - free(xmlElemContent); - xmlElemContent = NULL; + char* timestr = get_time_str_from_int(hour, min); + _write_attribute(xmlWriter, "anfangssaldo", ×tr); xmlTextWriterEndElement(xmlWriter); xmlTextWriterEndDocument(xmlWriter); @@ -285,8 +280,6 @@ void init_time_acount() free(xmlElemContent); xmlElemContent = NULL; - free(xmlElemName); - xmlElemName = NULL; } void add_entry(const char* date, diff --git a/src/xml.h b/src/xml.h index a6381b3..9a6bb8e 100644 --- a/src/xml.h +++ b/src/xml.h @@ -7,7 +7,7 @@ typedef struct { size_t size; } mem_file_t; -void init_time_acount(); +void init_time_acount(const uint8_t hour, const uint8_t min); void add_entry(const char* date, const uint8_t begin_hour, const uint8_t begin_min, diff --git a/tests/time_format_tests.c b/tests/time_format_tests.c index 05c069a..45acc28 100644 --- a/tests/time_format_tests.c +++ b/tests/time_format_tests.c @@ -311,6 +311,26 @@ void get_time_str_from_tm_test() timestr = NULL; } +void get_time_str_from_four_nine_test() +{ + char* timestr = get_time_str_from_int(4, 9); + + assert_string_equal(timestr, "04:09"); + + free(timestr), + timestr = NULL; +} + +void get_time_str_from_twenty_three_fity_nine_test() +{ + char* timestr = get_time_str_from_int(23, 59); + + assert_string_equal(timestr, "23:59"); + + free(timestr), + timestr = NULL; +} + int main() { const struct CMUnitTest tests[] = { @@ -342,7 +362,9 @@ int main() cmocka_unit_test(get_month_from_date_test), cmocka_unit_test(get_dat_from_date_test), cmocka_unit_test(get_date_str_from_tm_test), - cmocka_unit_test(get_time_str_from_tm_test) + cmocka_unit_test(get_time_str_from_tm_test), + cmocka_unit_test(get_time_str_from_four_nine_test), + cmocka_unit_test(get_time_str_from_twenty_three_fity_nine_test) }; return cmocka_run_group_tests(tests, NULL, NULL); -- 2.47.3