From 88f0c6a7b9aa6da6c46c167bf4f8689057de707a Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 24 Aug 2021 12:09:54 +0200 Subject: [PATCH] XML parsing sucessful --- debian/var/models/example.xml | 4 +-- include/base.h | 8 +++-- include/xml.h | 55 +++++++++++++++++++++++------------ src/model.c | 50 ++++++++++++++++++++++--------- 4 files changed, 81 insertions(+), 36 deletions(-) diff --git a/debian/var/models/example.xml b/debian/var/models/example.xml index 025e2b1..9d66ff7 100644 --- a/debian/var/models/example.xml +++ b/debian/var/models/example.xml @@ -6,11 +6,11 @@ - Modèle de test Adrien Bourmault 1 2021-08-21-21h34m44s 1.0 + Modèle de test @@ -75,6 +75,6 @@ https://www.a-lec.org - Comment + Quote diff --git a/include/base.h b/include/base.h index aa85457..e8e57fb 100644 --- a/include/base.h +++ b/include/base.h @@ -157,9 +157,12 @@ struct { // struct { int id; - bool validated; char *name; + char *author; + time_t date; + char *version; char *filename; + bool validated; int space_xMax; int space_yMax; int space_zMax; @@ -172,7 +175,8 @@ struct { } typedef Model_t; struct ModelField_t { - char id[25]; + bool mandatory; + char tag[25]; char *value; size_t valueSize; struct ModelField_t *son; diff --git a/include/xml.h b/include/xml.h index f17e6e9..fd7a86d 100644 --- a/include/xml.h +++ b/include/xml.h @@ -72,15 +72,21 @@ static inline int parseTag(const char* tagName, char *destination, char destSize xmlNodePtr cur = currentDocumentRoot; - printLog("Asked for %s\n", tagName); + //printLog("Asked for %s\n", tagName); + // Enumerate all brothers in tree from current root while (cur != NULL) { - printLog("Got %s\n", cur->name); + //printLog("\tGot %s (res = %d)\n", cur->name, res); + + // Check if current tag is the asked tag if ((!xmlStrcmp(cur->name, (const xmlChar *)tagName))) { + //printLog("\tFound %s\n", cur->name); curString = (char*)xmlNodeListGetString(currentDocument, cur->xmlChildrenNode, 1); + //printLog("\t\tString : %s\n", curString); - if (curString[0] != '\n' && destination) { + // Check there is text in that tag (and that is asked) + if (destination && curString[0] != '\n') { snprintf(destination, destSize, "%s\n", curString); res++; } @@ -96,7 +102,7 @@ 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))){ @@ -115,30 +121,41 @@ static inline xmlNodePtr findRoot(const char *tagName, xmlNodePtr curRoot) return NULL; } -static inline int parseTree(ModelField_t *tree) +static inline int parseTree(ModelField_t *curTree) { - ModelField_t *curTree; - - curTree = tree; - - if ((findRoot(curTree->id, currentDocumentRoot)) == NULL) - return EBADF; + int currentOrSonCorrectlyParsed = 0; while (curTree) { - printLog("Asked for %s\n", curTree->id); - - if ((parseTag(curTree->id, curTree->value, curTree->valueSize) == 0)) + if (findRoot( + curTree->tag, + currentDocumentRoot->parent + ) == NULL) { + printLog("Error : can't find a new root\n"); return EBADF; + } - if (curTree->value[0]) - if (!(curTree->value[0] == '\0')) { - printLog("Field %s: %s", curTree->id, curTree->value); + //printLog("Asked for %s\n", curTree->tag); + + // Try to parse current tag + if (parseTag(curTree->tag, curTree->value, curTree->valueSize) != 0) { + // There are values in that tag + if (curTree->value[0]) + if (!(curTree->value[0] == '\0')) { + printLog("Field %s: %s", curTree->tag, curTree->value); + } + } else { + // There aren't values in that tag + if (curTree->mandatory) { + // But it was mandatory + printLog("Error : can't find %s\n", curTree->tag); + return EBADF; } + } if (curTree->son) - parseTree(curTree->son); + currentOrSonCorrectlyParsed |= parseTree(curTree->son); curTree = curTree->next; } - return 0; + return currentOrSonCorrectlyParsed; } diff --git a/src/model.c b/src/model.c index f066a83..99f15d9 100644 --- a/src/model.c +++ b/src/model.c @@ -73,8 +73,16 @@ void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots // cont. model population knownModel[knownModelSize-1]->name = (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); + knownModel[knownModelSize-1]->filename = (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); + + knownModel[knownModelSize-1]->author = + (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); + + knownModel[knownModelSize-1]->version = + (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); + knownModel[knownModelSize-1]->space_xMax = XMAX; knownModel[knownModelSize-1]->space_yMax = YMAX; knownModel[knownModelSize-1]->space_zMax = ZMAX; @@ -175,10 +183,19 @@ void ModelDelete(int id) //XXX { free(knownModel[id-1]->name); knownModel[id-1]->name = NULL; + free(knownModel[id-1]->filename); - knownModel[id-1]->name = NULL; + knownModel[id-1]->filename = NULL; + + free(knownModel[id-1]->author); + knownModel[id-1]->author = NULL; + + free(knownModel[id-1]->version); + knownModel[id-1]->version = NULL; + free(knownModel[id-1]); knownModel[id-1] = NULL; + knownModelSize--; } @@ -273,32 +290,39 @@ static int ModelParseFile(Model_t *model) ModelField_t dateField; ModelField_t versionField; - strcpy(identityField.id, "identity"); + char date[25]; + + strcpy(identityField.tag, "identity"); + identityField.mandatory = false; identityField.value = NULL; identityField.valueSize = 0; - identityField.son = &authorField; + identityField.son = &nameField; identityField.next = NULL; - strcpy(nameField.id, "name"); + strcpy(nameField.tag, "name"); + nameField.mandatory = true; nameField.value = model->name; nameField.valueSize = 255; nameField.son = NULL; - nameField.next = &dateField; + nameField.next = &authorField; - strcpy(authorField.id, "author"); - authorField.value = NULL; + strcpy(authorField.tag, "author"); + authorField.mandatory = true; + authorField.value = model->author; authorField.valueSize = 25; authorField.son = NULL; - authorField.next = &nameField; + authorField.next = &dateField; - strcpy(dateField.id, "date"); - dateField.value = NULL; - dateField.valueSize = 12; + strcpy(dateField.tag, "date"); + dateField.mandatory = true; + dateField.value = date; + dateField.valueSize = 25; dateField.son = NULL; dateField.next = &versionField; - strcpy(versionField.id, "version"); - versionField.value = NULL; + strcpy(versionField.tag, "version"); + versionField.mandatory = true; + versionField.value = model->version; versionField.valueSize = 15; versionField.son = NULL; versionField.next = NULL;