Parse and add model

This commit is contained in:
Adrien Bourmault 2021-08-24 01:14:48 +02:00
parent d36242933d
commit 991b2ec549
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
3 changed files with 52 additions and 16 deletions

View File

@ -157,6 +157,7 @@ struct {
//
struct {
int id;
bool validated;
char *name;
char *filename;
int space_xMax;

View File

@ -65,39 +65,56 @@ static void resetDocumentRoot(void)
currentDocumentRoot = xmlDocGetRootElement(currentDocument);
}
void parseTag(const char* tagName, char *destination, char destSize)
int parseTag(const char* tagName, char *destination, char destSize)
{
char *curString;
char res = 0;
xmlNodePtr cur = currentDocumentRoot;
//printLog("Asked for %s\n", tagName);
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)tagName))) {
curString = (char*)xmlNodeListGetString(currentDocument,
cur->xmlChildrenNode, 1);
if (curString[0] != '\n' && destination)
if (curString[0] != '\n' && destination) {
snprintf(destination, destSize, "%s\n", curString);
res++;
}
free(curString);
}
cur = cur->next;
};
}
return res;
}
void findRoot(const char *tagName, xmlNodePtr curRoot)
xmlNodePtr findRoot(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)
findRoot(tagName, cur->xmlChildrenNode);
if (cur->xmlChildrenNode) {
if ((recur = findRoot(tagName, cur->xmlChildrenNode) != NULL))
return recur;
}
cur = cur->next;
}
return NULL;
}
void parseTree(ModelField_t *tree)
int parseTree(ModelField_t *tree)
{
ModelField_t *curTree;
char tempString[50];
@ -105,15 +122,23 @@ void parseTree(ModelField_t *tree)
curTree = tree;
while (curTree) {
findRoot(curTree->id, currentDocumentRoot);
parseTag(curTree->id, tempString, 50);
//printLog("Asked for %s\n", curTree->id);
if ((findRoot(curTree->id, currentDocumentRoot)) == NULL)
return EBADF;
if (!(tempString[0] == '\0'))
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->son)
parseTree(curTree->son);
curTree = curTree->next;
}
return 0;
}

View File

@ -48,7 +48,7 @@ Model_t *lastModel;
Model_t **lastModelAddr;
static void ModelParseFile(Model_t *model);
static int ModelParseFile(Model_t *model);
void ModelSystemDestroy(void);
@ -179,6 +179,7 @@ void ModelDelete(int id) //XXX
knownModel[id-1]->name = NULL;
free(knownModel[id-1]);
knownModel[id-1] = NULL;
knownModelSize--;
}
void ModelShutdown(void)
@ -235,10 +236,13 @@ void ModelSystemInit(Parameters_t *parameters)
strncpy(newModel->name, modelDirEntry->d_name,
extensionPosition - modelDirEntry->d_name);
ModelParseFile(newModel);
if (ModelParseFile(newModel) != 0) {
ModelDelete(newModel->id);
continue;
};
// Check model is valid and/or parsed
if (newModel->siteNumber == 0) {
if (newModel->validated == false) {
ModelDelete(newModel->id);
continue;
}
@ -261,7 +265,7 @@ void ModelSystemDestroy(void)
/* -------------------------------------------------------------------------- */
static void ModelParseFile(Model_t *model)
static int ModelParseFile(Model_t *model)
{
ModelField_t identityField;
ModelField_t authorField;
@ -275,7 +279,7 @@ static void ModelParseFile(Model_t *model)
identityField.next = NULL;
strcpy(nameField.id, "name");
nameField.value = NULL;
nameField.value = model->name;
nameField.son = NULL;
nameField.next = &authorField;
@ -300,7 +304,13 @@ static void ModelParseFile(Model_t *model)
openCurrentDocument(model->filename);
resetDocumentRoot();
parseTree(&identityField);
if (parseTree(&identityField) != 0) {
printLog("Invalid document, parsing can't succeed!\n");
closeCurrentDocument();
return EBADF;
}
closeCurrentDocument();
model->validated = true;
return 0;
}