From d6538985b89fb78ba866455b7dc17e9393dafc7d Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 24 Aug 2021 01:39:00 +0200 Subject: [PATCH] WIP: [BUG] parsing without order constraint --- debian/var/models/example.xml | 4 ++-- include/base.h | 1 + include/xml.h | 38 +++++++++++++++++------------------ src/model.c | 11 +++++++--- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/debian/var/models/example.xml b/debian/var/models/example.xml index 96ffacb..025e2b1 100644 --- a/debian/var/models/example.xml +++ b/debian/var/models/example.xml @@ -73,8 +73,8 @@ - + https://www.a-lec.org - Comment + Comment diff --git a/include/base.h b/include/base.h index e8ed459..aa85457 100644 --- a/include/base.h +++ b/include/base.h @@ -174,6 +174,7 @@ struct { struct ModelField_t { char id[25]; char *value; + size_t valueSize; struct ModelField_t *son; struct ModelField_t *next; } typedef ModelField_t; diff --git a/include/xml.h b/include/xml.h index 66fb1b1..f17e6e9 100644 --- a/include/xml.h +++ b/include/xml.h @@ -31,7 +31,7 @@ static xmlDocPtr currentDocument = NULL; static xmlNodePtr currentDocumentRoot = NULL; -void openCurrentDocument(const char *filename) +static inline void openCurrentDocument(const char *filename) { currentDocument = xmlParseFile(filename); @@ -53,28 +53,29 @@ void openCurrentDocument(const char *filename) } } -void closeCurrentDocument(void) +static inline void closeCurrentDocument(void) { xmlFreeDoc(currentDocument); currentDocumentRoot = NULL; currentDocument = NULL; } -static void resetDocumentRoot(void) +static inline void resetDocumentRoot(void) { currentDocumentRoot = xmlDocGetRootElement(currentDocument); } -int parseTag(const char* tagName, char *destination, char destSize) +static inline int parseTag(const char* tagName, char *destination, char destSize) { char *curString; char res = 0; xmlNodePtr cur = currentDocumentRoot; - //printLog("Asked for %s\n", tagName); + printLog("Asked for %s\n", tagName); while (cur != NULL) { + printLog("Got %s\n", cur->name); if ((!xmlStrcmp(cur->name, (const xmlChar *)tagName))) { curString = (char*)xmlNodeListGetString(currentDocument, cur->xmlChildrenNode, 1); @@ -90,12 +91,12 @@ int parseTag(const char* tagName, char *destination, char destSize) return res; } -xmlNodePtr findRoot(const char *tagName, xmlNodePtr curRoot) +static inline xmlNodePtr findRoot(const char *tagName, xmlNodePtr curRoot) { xmlNodePtr cur = curRoot; xmlNodePtr recur; - //printLog("Asked for %s\n", tagName); + printLog("Asked for %s\n", tagName); while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)tagName))){ @@ -114,26 +115,25 @@ xmlNodePtr findRoot(const char *tagName, xmlNodePtr curRoot) return NULL; } -int parseTree(ModelField_t *tree) +static inline int parseTree(ModelField_t *tree) { ModelField_t *curTree; - char tempString[50]; curTree = tree; + if ((findRoot(curTree->id, currentDocumentRoot)) == NULL) + return EBADF; + while (curTree) { - //printLog("Asked for %s\n", curTree->id); - if ((findRoot(curTree->id, currentDocumentRoot)) == NULL) + printLog("Asked for %s\n", curTree->id); + + if ((parseTag(curTree->id, curTree->value, curTree->valueSize) == 0)) return EBADF; - bzero(tempString, sizeof(tempString)); - if ((parseTag(curTree->id, tempString, sizeof(tempString)) == 0 - && curTree->value)) - return EBADF; - - if (!(tempString[0] == '\0')) { - printLog("Field %s: %s", curTree->id, tempString); - } + if (curTree->value[0]) + if (!(curTree->value[0] == '\0')) { + printLog("Field %s: %s", curTree->id, curTree->value); + } if (curTree->son) parseTree(curTree->son); diff --git a/src/model.c b/src/model.c index fa343ec..f066a83 100644 --- a/src/model.c +++ b/src/model.c @@ -275,26 +275,31 @@ static int ModelParseFile(Model_t *model) strcpy(identityField.id, "identity"); identityField.value = NULL; - identityField.son = &nameField; + identityField.valueSize = 0; + identityField.son = &authorField; identityField.next = NULL; strcpy(nameField.id, "name"); nameField.value = model->name; + nameField.valueSize = 255; nameField.son = NULL; - nameField.next = &authorField; + nameField.next = &dateField; strcpy(authorField.id, "author"); authorField.value = NULL; + authorField.valueSize = 25; authorField.son = NULL; - authorField.next = &dateField; + authorField.next = &nameField; strcpy(dateField.id, "date"); dateField.value = NULL; + dateField.valueSize = 12; dateField.son = NULL; dateField.next = &versionField; strcpy(versionField.id, "version"); versionField.value = NULL; + versionField.valueSize = 15; versionField.son = NULL; versionField.next = NULL;