From: Bastian Dehn Date: Sun, 23 Jun 2024 08:03:27 +0000 (+0200) Subject: add read saldo X-Git-Tag: 1.2.0^2~16^2~15 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=0d9829c03e1b1ee0bc12a95121eca637a32ab5c2;p=feierabend.git add read saldo --- diff --git a/src/feierabendxml.c b/src/feierabendxml.c index caa9c4c..bb1fc51 100644 --- a/src/feierabendxml.c +++ b/src/feierabendxml.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "worktime.h" #include "break.h" #include "time_format.h" @@ -44,7 +46,7 @@ void initTimeAcount() free(xmlElemContent); xmlElemContent = NULL; - xmlTextWriterStartDocument(xmlWriter, "1.0", "UTF-8", "true"); + xmlTextWriterStartDocument(xmlWriter, "1.0", "UTF-8", NULL); xmlElemName = xmlCharStrdup("zeitkonto"); xmlTextWriterStartElement(xmlWriter, xmlElemName); @@ -216,7 +218,7 @@ void createEntry(int begin_hour, int begin_min, int end_hour, int end_min) free(xmlElemContent); xmlElemContent = NULL; - xmlTextWriterStartDocument(xmlWriter, "1.0", "UTF-8", "true"); + xmlTextWriterStartDocument(xmlWriter, "1.0", "UTF-8", NULL); xmlElemName = xmlCharStrdup("zeitkonto"); xmlTextWriterStartElement(xmlWriter, xmlElemName); @@ -244,10 +246,59 @@ void createEntry(int begin_hour, int begin_min, int end_hour, int end_min) xmlWriter = NULL; } +void readLastSaldo(const char* file_path) +{ + xmlInitParser(); + xmlDocPtr xmldoc = xmlReadFile(file_path, "UTF-8", 0); + + if (xmldoc == NULL) { + printf("ERROR: could not open file: %s\n", file_path); + exit(EXIT_FAILURE); + } + + xmlXPathContextPtr xmlcontext = xmlXPathNewContext(xmldoc); + xmlChar* expr = xmlCharStrdup("/zeitkonto/eintrag[last()]/@saldo"); + xmlXPathObjectPtr xmlnode = xmlXPathEvalExpression(expr, xmlcontext); + + if (xmlnode->nodesetval == NULL || xmlnode->nodesetval->nodeTab == NULL) { + free(expr); + expr = NULL; + xmlXPathFreeObject(xmlnode); + xmlnode = NULL; + expr = xmlCharStrdup("/zeitkonto/@anfangssaldo"); + xmlnode = xmlXPathEvalExpression(expr, xmlcontext); + } + + if (xmlnode->nodesetval->nodeTab == NULL) { + printf("ERROR: could not find an saldo\n"); + xmlCleanupParser(); + free(expr); + expr = NULL; + xmlXPathFreeObject(xmlnode); + xmlnode = NULL; + xmlXPathFreeContext(xmlcontext); + xmlcontext = NULL; + xmlFreeDoc(xmldoc); + xmldoc = NULL; + exit(EXIT_FAILURE); + } + + xmlCleanupParser(); + + free(expr); + expr = NULL; + xmlXPathFreeObject(xmlnode); + xmlnode = NULL; + xmlXPathFreeContext(xmlcontext); + xmlcontext = NULL; + xmlFreeDoc(xmldoc); + xmldoc = NULL; +} + int main(int argc, char* argv[]) { if (argc < 2) { - printf("ERROR: %s \n", argv[0]); + printf("ERROR: %s \n", argv[0]); return 1; } @@ -265,6 +316,15 @@ int main(int argc, char* argv[]) return 0; } + if (strcmp(argv[1], "add") == 0) { + if (argc < 7) { + printf("ERROR: %s add \n", argv[0]); + return 1; + } + readLastSaldo(argv[6]); + return 0; + } + printf("ERROR: unknown command\n"); return 0; } \ No newline at end of file