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 { struct {
int id; int id;
bool validated;
char *name; char *name;
char *filename; char *filename;
int space_xMax; int space_xMax;

View File

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

View File

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