From b267fc5a093492e521e677c8e7d1e5a54c09ab78 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 15 Sep 2021 18:25:35 +0200 Subject: [PATCH] WIP: Crushing and rebuilding from the ground an XML parser --- Makefile | 2 +- debian/var/models/example.xml | 6 +- include/arrows.h | 1 + include/centers.h | 1 + include/cmds.h | 1 + include/model.h | 1 + include/scheduler.h | 1 + include/server.h | 1 + include/supervisor.h | 1 + include/terminal.h | 1 + include/worker.h | 1 + include/xml.h | 178 +--------------------------------- src/cli.c | 2 + src/main.c | 2 + src/model.c | 69 +------------ src/scheduler.c | 2 + src/server.c | 2 + src/supervisor.c | 2 + src/worker.c | 2 + 19 files changed, 28 insertions(+), 248 deletions(-) diff --git a/Makefile b/Makefile index 4ebaebe..e41c7be 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ DEBDIR=debian SERVEROBJ= $(BINDIR)/scheduler.o $(BINDIR)/server.o $(BINDIR)/worker.o \ $(BINDIR)/centers.o $(BINDIR)/cmds.o $(BINDIR)/model.o \ - $(BINDIR)/main.o $(BINDIR)/arrows.o + $(BINDIR)/main.o $(BINDIR)/arrows.o $(BINDIR)/parsing.o CLIOBJ= $(BINDIR)/cli.o SRCS=$(patsubst $(BINDIR)/%.o,$(SRCDIR)/%.c,$(SERVEROBJ)) \ diff --git a/debian/var/models/example.xml b/debian/var/models/example.xml index 0f2908a..03643ef 100644 --- a/debian/var/models/example.xml +++ b/debian/var/models/example.xml @@ -9,7 +9,7 @@ Modèle de test Adrien Bourmault 1 - 1629822515 + 1629822515 1.0 @@ -18,7 +18,7 @@ - + @@ -26,7 +26,7 @@ - + diff --git a/include/arrows.h b/include/arrows.h index 20be61f..8543726 100644 --- a/include/arrows.h +++ b/include/arrows.h @@ -19,6 +19,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#pragma once #ifndef BASE_H #include "../include/base.h" #endif diff --git a/include/centers.h b/include/centers.h index 45f93cb..f1007e1 100644 --- a/include/centers.h +++ b/include/centers.h @@ -19,6 +19,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#pragma once #ifndef BASE_H #include "../include/base.h" #endif diff --git a/include/cmds.h b/include/cmds.h index a36f3c1..a4280d6 100644 --- a/include/cmds.h +++ b/include/cmds.h @@ -19,6 +19,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#pragma once #ifndef BASE_H #include "../include/base.h" #endif diff --git a/include/model.h b/include/model.h index 7f716f0..4721249 100644 --- a/include/model.h +++ b/include/model.h @@ -19,6 +19,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#pragma once #ifndef BASE_H #include "../include/base.h" #endif diff --git a/include/scheduler.h b/include/scheduler.h index 7532a47..adc07b8 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -19,6 +19,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#pragma once #ifndef BASE_H #include "../include/base.h" #endif diff --git a/include/server.h b/include/server.h index e8f2353..8110fb0 100644 --- a/include/server.h +++ b/include/server.h @@ -19,6 +19,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#pragma once #ifndef BASE_H #include "../include/base.h" #endif diff --git a/include/supervisor.h b/include/supervisor.h index 6b74039..5932f1b 100644 --- a/include/supervisor.h +++ b/include/supervisor.h @@ -19,6 +19,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#pragma once #ifndef BASE_H #include "../include/base.h" #endif diff --git a/include/terminal.h b/include/terminal.h index 7f09846..7c1f68d 100644 --- a/include/terminal.h +++ b/include/terminal.h @@ -19,6 +19,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#pragma once #ifndef BASE_H #include "../include/base.h" #endif diff --git a/include/worker.h b/include/worker.h index 70c88c7..b00f18b 100644 --- a/include/worker.h +++ b/include/worker.h @@ -19,6 +19,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#pragma once #ifndef BASE_H #include "../include/base.h" #endif diff --git a/include/xml.h b/include/xml.h index 3747f7e..d56d876 100644 --- a/include/xml.h +++ b/include/xml.h @@ -19,6 +19,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#pragma once #ifndef BASE_H #include "../include/base.h" #endif @@ -30,181 +31,4 @@ #include #include -/* -------------------------------------------------------------------------- */ -static xmlDocPtr currentDocument = NULL; -static xmlNodePtr currentDocumentRoot = NULL; - -/* -------------------------------------------------------------------------- */ - -// -------------------------------------------------------------------------- // -// Open and set a document as current document // -// -------------------------------------------------------------------------- // -static inline void XmlSetNewDocument(const char *filename) -{ - currentDocument = xmlParseFile(filename); - - if (currentDocument == NULL ) { - printLog("%s not parsed successfully.\n", filename); - xmlFreeDoc(currentDocument); - currentDocument = NULL; - return; - } - - currentDocumentRoot = xmlDocGetRootElement(currentDocument); - - if (currentDocumentRoot == NULL ) { - printLog("%s is invalid (no root)\n", filename); - xmlFreeDoc(currentDocument); - currentDocument = NULL; - currentDocumentRoot = NULL; - return; - } -} - -// -------------------------------------------------------------------------- // -// Close and unset current document // -// -------------------------------------------------------------------------- // -static inline void XmlCloseCurrentDocument(void) -{ - xmlFreeDoc(currentDocument); - currentDocumentRoot = NULL; - currentDocument = NULL; -} - -// -------------------------------------------------------------------------- // -// Reset current working root to document root // -// -------------------------------------------------------------------------- // -static inline void XmlResetWorkingRoot(void) -{ - currentDocumentRoot = xmlDocGetRootElement(currentDocument); -} - -// -------------------------------------------------------------------------- // -// Parse content of a given XML tag // -// -------------------------------------------------------------------------- // -static inline int XmlParseTag(const char* tagName, char *destination, - char destSize) -{ - char *curString; - size_t cursor = 0; - char res = 0; - - xmlNodePtr cur = currentDocumentRoot; - - //printLog("Asked for %s\n", tagName); - - // Enumerate all brothers in tree from current root - while (cur != NULL) { - //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); - - // Check there is text in that tag (and that is asked) - if (destination && curString[0] != '\n') { - cursor = - snprintf( - destination + cursor * sizeof(char), - destSize, - "%s\n", - curString); - res++; - } - free(curString); - } - cur = cur->next; - } - return res; -} - -// -------------------------------------------------------------------------- // -// Find a working root for a given tag // -// -------------------------------------------------------------------------- // -static inline xmlNodePtr findWorkingRoot(const char *tagName, - xmlNodePtr curRoot) -{ - xmlNodePtr cur = curRoot; - xmlNodePtr recur; - - //printLog("Asked for %s\n", tagName); - - while (cur != NULL) { - if ((!xmlStrcmp(cur->name, (const xmlChar *)tagName))){ - currentDocumentRoot = cur; - return cur; - } - - if (cur->xmlChildrenNode) { - if ((recur = - findWorkingRoot(tagName, - (xmlNodePtr)cur->xmlChildrenNode)) != NULL) - return recur; - } - - cur = cur->next; - } - - return NULL; -} - -// -------------------------------------------------------------------------- // -// Parse and validate an XML tree from given structural tree // -// and current document // -// -------------------------------------------------------------------------- // -static inline int XmlParseTree(ModelField_t *curTree) -{ - int currentOrSonCorrectlyParsed = 0; - int curValueLen; - - while (curTree) { - if (findWorkingRoot( - curTree->tag, - currentDocumentRoot->parent - ) == NULL) { - printLog("Error : can't find a new root\n"); - return EBADF; - } - - //printLog("Asked for %s\n", curTree->tag); - - // Try to parse current tag - if (XmlParseTag(curTree->tag, curTree->destination, - curTree->size) != 0) { - // There are values in that tag - if (curTree->destination[0]) - if (!(curTree->destination[0] == '\0')) { - curValueLen = strlen(curTree->destination); - - // Delete \n chars - for(int i = 0; i < curValueLen; i++) { - if(curTree->destination[i] == '\n') { - memmove(&curTree->destination[i], - &curTree->destination[i+1], - curValueLen - i); - curValueLen--; - i--; - } - } - printLog("Field %s: %s\n", curTree->tag, curTree->destination); - } - } 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) - currentOrSonCorrectlyParsed |= XmlParseTree(curTree->son); - - curTree = curTree->next; - } - return currentOrSonCorrectlyParsed; -} diff --git a/src/cli.c b/src/cli.c index 3927441..b298b28 100644 --- a/src/cli.c +++ b/src/cli.c @@ -44,6 +44,8 @@ #define KEY_BACKSPACE 127 #define KEY_DELETE 126 // accessible after sequence KEY_ESCAPE_1, 2, 3 +/* -------------------------------------------------------------------------- */ + // // Print monitor screen // diff --git a/src/main.c b/src/main.c index 6d6af93..8a32941 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,8 @@ #include "../include/server.h" #include "../include/model.h" +/* -------------------------------------------------------------------------- */ + static Server_t *server; static void SigIntTermHandler(int signum) diff --git a/src/model.c b/src/model.c index 90917d0..f4330c8 100644 --- a/src/model.c +++ b/src/model.c @@ -20,7 +20,7 @@ //=-------------------------------------------------------------------------=// #include "../include/base.h" -#include "../include/xml.h" +#include "../include/parsing.h" #include "../include/arrows.h" #include "../include/scheduler.h" @@ -50,8 +50,6 @@ static int knownModelSize; // begins to 1 Model_t *lastModel; Model_t **lastModelAddr; - -static inline int ModelParseFile(Model_t *model); void ModelSystemDestroy(void); /* -------------------------------------------------------------------------- */ @@ -293,7 +291,7 @@ void ModelSystemInit(Parameters_t *parameters) extensionPosition - modelDirEntry->d_name); // Ask to parse the new model - if (ModelParseFile(newModel) != 0) { + if (ParseModelXML(newModel) != 0) { ModelDelete(newModel->id); continue; }; @@ -338,66 +336,3 @@ static inline void CreateField(ModelField_t *modelField, modelField->son = son; modelField->next = next; } - -static inline int ModelParseFile(Model_t *model) -{ - char date[25]; - - ModelField_t identityField; - ModelField_t identityNameField; - ModelField_t identityAuthorField; - ModelField_t identityDateField; - ModelField_t identityVersionField; - ModelField_t parametersField; - ModelField_t parametersModelizationField; - ModelField_t parametersSpaceField; - - CreateField(&identityField, - "identity", false, NULL, 0, &identityNameField, NULL); - - CreateField(&identityNameField, - "name", true, model->name, MAX_MODEL_NAME_SIZE, - NULL, &identityAuthorField); - - CreateField(&identityAuthorField, - "author", true, model->author, MAX_AUTHOR_NAME_SIZE, - NULL, &identityDateField); - - CreateField(&identityDateField, - "date", true, date, 25, - NULL, &identityVersionField); - - CreateField(&identityVersionField, - "version", true, model->version, MAX_VERSION_SIZE, NULL, NULL); - - CreateField(¶metersField, - "parameters", false, NULL, 0, NULL, NULL); - - CreateField(¶metersModelizationField, - "modelization", false, NULL, 0, NULL, NULL); - - CreateField(¶metersSpaceField, - "space", false, NULL, 0, NULL, NULL); - - - // TODO modify model according to things in file - - printLog("Parsing model %s\n", model->name); - XmlSetNewDocument(model->filename); - - XmlResetWorkingRoot(); - - XmlCloseCurrentDocument(); - - if (XmlParseTree(&identityField) != 0) { - printLog("Invalid document, parsing can't succeed!\n"); - return EBADF; - } - - // Interpret what needs to be - model->date = strtol(date, NULL, 0); - - // validate when we're finished - model->validated = true; - return 0; -} diff --git a/src/scheduler.c b/src/scheduler.c index e32f8a5..ecb3f37 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -26,6 +26,8 @@ #include +/* -------------------------------------------------------------------------- */ + static void *schedulerMain(void *scheduler); /* -------------------------------------------------------------------------- */ diff --git a/src/server.c b/src/server.c index 3d55992..494f548 100644 --- a/src/server.c +++ b/src/server.c @@ -29,6 +29,8 @@ #include #include +/* -------------------------------------------------------------------------- */ + static void *serverMain(void *server); /* -------------------------------------------------------------------------- */ diff --git a/src/supervisor.c b/src/supervisor.c index a3e8805..385647d 100644 --- a/src/supervisor.c +++ b/src/supervisor.c @@ -21,6 +21,8 @@ #include "../include/base.h" +/* -------------------------------------------------------------------------- */ + static void *supervisorMain(void *supervisor); /* -------------------------------------------------------------------------- */ diff --git a/src/worker.c b/src/worker.c index 4c168fd..356c103 100644 --- a/src/worker.c +++ b/src/worker.c @@ -21,6 +21,8 @@ #include "../include/base.h" +/* -------------------------------------------------------------------------- */ + static void *WorkerMain(void *worker); /* -------------------------------------------------------------------------- */