From 5ee70f6c44b6aea45fcb62ab341d7603e773f803 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 17 Sep 2021 16:14:04 +0200 Subject: [PATCH] Progressing into parsing and model file syntax --- Makefile | 4 + debian/var/models/example_II.xml | 28 +----- include/base.h | 3 +- include/model.h | 18 ++++ src/model.c | 31 ++----- src/parsing.c | 149 ++++++++++++++++++++++--------- 6 files changed, 144 insertions(+), 89 deletions(-) diff --git a/Makefile b/Makefile index e41c7be..30ce70d 100644 --- a/Makefile +++ b/Makefile @@ -121,6 +121,10 @@ run-gdb: all @gdb bin/gem-graph-server \ -ex "set args -C debian/etc -M debian/var/models -U debian/var/users" +run-valgrind: all + @valgrind bin/gem-graph-server \ + -C debian/etc -M debian/var/models -U debian/var/users + run-both: all @bin/gem-graph-server -C debian/etc -M debian/var/models \ -U debian/var/users & sleep 1 && bin/gem-graph-ctl diff --git a/debian/var/models/example_II.xml b/debian/var/models/example_II.xml index 62f6f89..1716654 100644 --- a/debian/var/models/example_II.xml +++ b/debian/var/models/example_II.xml @@ -7,9 +7,9 @@ Modèle de test - Adrien Bourmault, Jean Sirmai - 1,2 - 1629830000 + Jean Sirmai + 2 + 1629830000 1.0 @@ -30,7 +30,7 @@ - + @@ -91,26 +91,6 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/include/base.h b/include/base.h index 7008393..334fc89 100644 --- a/include/base.h +++ b/include/base.h @@ -160,7 +160,8 @@ struct Model_t { int id; bool validated; // true if model has been correctly parsed char *name; - char *author; + char *owner; + char *owner_id; time_t date; char *version; char *filename; diff --git a/include/model.h b/include/model.h index 4721249..ba55608 100644 --- a/include/model.h +++ b/include/model.h @@ -26,6 +26,24 @@ /* -------------------------------------------------------------------------- */ +#define MODEL_STRING_SIZE 48 +#define MAX_MODEL_NUMBER 1 +#define MAX_MODEL_NAME_SIZE MODEL_STRING_SIZE +#define MAX_MODEL_FILENAME_SIZE MODEL_STRING_SIZE +#define MAX_OWNER_NAME_SIZE MODEL_STRING_SIZE +#define MAX_VERSION_SIZE MODEL_STRING_SIZE + +#define ARROW_NUMBER 6 +#define SITE_NUMBER 2 +#define MAX_CYCLES 10 +#define MAX_THREAD 0 +#define XMAX 39 +#define YMAX 0 +#define ZMAX 0 +#define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1) + +/* -------------------------------------------------------------------------- */ + // -------------------------------------------------------------------------- // // Model init function (and model discovery) // // -------------------------------------------------------------------------- // diff --git a/src/model.c b/src/model.c index adf8dc6..a11b3ac 100644 --- a/src/model.c +++ b/src/model.c @@ -23,24 +23,11 @@ #include "../include/parsing.h" #include "../include/arrows.h" #include "../include/scheduler.h" +#include "../include/model.h" #include #include -#define MAX_MODEL_NUMBER 1 -#define MAX_MODEL_NAME_SIZE 48 -#define MAX_MODEL_FILENAME_SIZE 48 -#define MAX_AUTHOR_NAME_SIZE 48 -#define MAX_VERSION_SIZE 16 -#define ARROW_NUMBER 6 -#define SITE_NUMBER 2 -#define MAX_CYCLES 10 -#define MAX_THREAD 0 -#define XMAX 39 -#define YMAX 0 -#define ZMAX 0 -#define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1) - static Model_t **loadedModel; static int loadedModelSize; // begins to 1 @@ -59,12 +46,12 @@ void printModels(char *buf) sprintf(buf + strlen(buf),"Known models\n"); for (int i = 0; i <= knownModelSize-1; i++) { sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s, date: %lu, " - "author: %s\n", + "owner: %s\n", knownModel[i]->id, knownModel[i], knownModel[i]->name, knownModel[i]->date, - knownModel[i]->author + knownModel[i]->owner ); } @@ -95,7 +82,7 @@ int ModelLoad(int id) return loadedModelSize; } -void ModelUnload(int id) +int ModelUnload(int id) { // Destroy scheduler SchedDestroy(loadedModel[id-1]->scheduler); @@ -196,8 +183,8 @@ void ModelAddToKnown(Model_t **newModel) // TODO manage deletion and empty slots knownModel[knownModelSize-1]->filename = (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); - knownModel[knownModelSize-1]->author = - (char *) calloc(1, sizeof(char) * MAX_AUTHOR_NAME_SIZE); + knownModel[knownModelSize-1]->owner = + (char *) calloc(1, sizeof(char) * MAX_OWNER_NAME_SIZE); knownModel[knownModelSize-1]->version = (char *) calloc(1, sizeof(char) * MAX_VERSION_SIZE); @@ -219,8 +206,8 @@ void ModelDelete(int id) //XXX free(knownModel[id-1]->filename); knownModel[id-1]->filename = NULL; - free(knownModel[id-1]->author); - knownModel[id-1]->author = NULL; + free(knownModel[id-1]->owner); + knownModel[id-1]->owner = NULL; free(knownModel[id-1]->version); knownModel[id-1]->version = NULL; @@ -293,7 +280,7 @@ void ModelSystemInit(Parameters_t *parameters) // Ask to parse the new model if (ParseModelXML(newModel) != 0) { ModelDelete(newModel->id); - continue;XMAX + continue; }; // Check model is valid and/or parsed diff --git a/src/parsing.c b/src/parsing.c index 4a9e4e7..d145fba 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -20,70 +20,134 @@ //=-------------------------------------------------------------------------=// #include "../include/base.h" +#include "../include/model.h" #include #include -#include -#include +#include /* -------------------------------------------------------------------------- */ -static int parseIdentityXML(Model_t*, xmlDocPtr, xmlNodePtr); - static int parseIdentityNameXML(Model_t*, xmlDocPtr, xmlNodePtr); +#define NO_DESTINATION NULL -// Table of all parsers -struct parserTableXML_t { +/* -------------------------------------------------------------------------- */ + +struct ModelParserTableXML_t; + +struct ParserTableXML_t +{ const xmlChar *tag; - int (*execute) (Model_t*, xmlDocPtr, xmlNodePtr); + int (*parse) (xmlDoc*, + struct ModelParserTableXML_t*, + int currentParser, + xmlNode*); + void *destination; } typedef ParserTableXML_t; -static ParserTableXML_t toplevelParserTableXML[] = +struct ModelParserTableXML_t { - {(const xmlChar *)"identity", parseIdentityXML}, -}; - -static ParserTableXML_t identityParserTableXML[] = -{ - {(const xmlChar *)"name", parseIdentityChildren}, - {(const xmlChar *)"author", parseIdentityChildren}, - {(const xmlChar *)"author_id", parseIdentityChildren}, - {(const xmlChar *)"date", parseIdentityChildren}, - {(const xmlChar *)"version", parseIdentityChildren}, -}; + size_t len; + ParserTableXML_t *table; +} typedef ModelParserTableXML_t; /* -------------------------------------------------------------------------- */ -static int parseIdentityNameXML (Model_t *model, xmlDocPtr doc, - xmlNodePtr currentNode) +int parseParentFieldXML(xmlDocPtr, + ModelParserTableXML_t*, + int, + xmlNodePtr); + +int parseTextField(xmlDocPtr, + ModelParserTableXML_t*, + int, + xmlNodePtr); + +/* -------------------------------------------------------------------------- */ + +// -------------------------------------------------------------------------- // +// Parsing NOTHING // +// -------------------------------------------------------------------------- // +int parseStubFieldXML (xmlDocPtr doc, + ModelParserTableXML_t *ModelTable, + int currentParser, + xmlNodePtr currentNode) { - xmlChar *key; - key = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1); - printLog("key: %s\n", key); - xmlFree(key); + xmlChar *content; + content = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1); + + printLog("%s (stub): %s\n", + ModelTable->table[currentParser].tag, + content); + + xmlFree(content); return 0; } -static int parseIdentityXML (Model_t *model, xmlDocPtr doc, - xmlNodePtr currentNode) +// -------------------------------------------------------------------------- // +// Parsing a text field // +// -------------------------------------------------------------------------- // +int parseTextFieldXML (xmlDocPtr doc, + ModelParserTableXML_t *ModelTable, + int currentParser, + xmlNodePtr currentNode) { - xmlChar *key; - currentNode = currentNode->xmlChildrenNode; - while (currentNode != NULL) { - for (int i = 0; i < LEN(identityParserTableXML); i++) { + xmlChar *content; + content = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1); + + printLog("%s: %s\n", ModelTable->table[currentParser].tag, + content); + + strncpy((char *)ModelTable->table[currentParser].destination, + (char *)content, MODEL_STRING_SIZE); + xmlFree(content); + return 0; +} + +int parseParentFieldXML (xmlDocPtr doc, + ModelParserTableXML_t *ModelTable, + int currentParser, + xmlNodePtr currentNode) +{ + currentNode = currentNode->xmlChildrenNode; + while (currentNode != NULL) { + for (int i = 0; i < ModelTable->len; i++) { if ((!xmlStrcmp(currentNode->name, - identityParserTableXML[i].tag))) { - identityParserTableXML[i].execute(model, - doc, - currentNode); + ModelTable->table[i].tag))) { + ModelTable->table[i].parse(doc, + ModelTable, + i, + currentNode); } } - currentNode = currentNode->next; - } + currentNode = currentNode->next; + } return 0; } int ParseModelXML(Model_t *model) { + ParserTableXML_t table[] = + { + {(const xmlChar *)"identity", parseParentFieldXML, + model}, + {(const xmlChar *)"name", parseTextFieldXML, + model->name}, + {(const xmlChar *)"owner", parseTextFieldXML, + model->owner}, + {(const xmlChar *)"owner_id", parseStubFieldXML, + model->owner_id}, + {(const xmlChar *)"date", parseStubFieldXML, + model->date}, + {(const xmlChar *)"version", parseTextFieldXML, + model->version}, + }; + + ModelParserTableXML_t modelParserTable = + { + LEN(table), + &table[0] + }; + printLog("Parsing model %s\n", model->name); xmlDocPtr xmlDocument; xmlNodePtr currentNode; @@ -114,12 +178,13 @@ int ParseModelXML(Model_t *model) currentNode = currentNode->xmlChildrenNode; while (currentNode != NULL) { - for (int i = 0; i < LEN(toplevelParserTableXML); i++) { + for (int i = 0; i < modelParserTable.len; i++) { if ((!xmlStrcmp(currentNode->name, - toplevelParserTableXML[i].tag))) { - toplevelParserTableXML[i].execute(model, - xmlDocument, - currentNode); + modelParserTable.table[i].tag))) { + modelParserTable.table[i].parse(xmlDocument, + &modelParserTable, + i, + currentNode); } }