]> gitweb.hhaalo.de Git - feierabend.git/commitdiff
add read saldo
authorBastian Dehn <hhaalo@arcor.de>
Sun, 23 Jun 2024 08:03:27 +0000 (10:03 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 23 Jun 2024 08:03:27 +0000 (10:03 +0200)
src/feierabendxml.c

index caa9c4c5f21ec5b85831292e6d166138335edc19..bb1fc516195d020c94ad688388d60d195b43344d 100644 (file)
@@ -2,6 +2,8 @@
 #include <string.h>
 #include <libxml/encoding.h>
 #include <libxml/xmlwriter.h>
+#include <libxml/parser.h>
+#include <libxml/xpath.h>
 #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 <init|entry>\n", argv[0]);
+               printf("ERROR: %s <init|add|entry>\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 <begin_hours> <begin_minutes> <end_hours> <end_minutes> <inputfile>\n", argv[0]);
+                       return 1;
+               }
+               readLastSaldo(argv[6]);
+               return 0;
+       }
+
        printf("ERROR: unknown command\n");
        return 0;
 }
\ No newline at end of file