XML parsing sucessful

This commit is contained in:
Adrien Bourmault 2021-08-24 12:09:54 +02:00
parent d6538985b8
commit 88f0c6a7b9
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
4 changed files with 81 additions and 36 deletions

View File

@ -6,11 +6,11 @@
<!-- Model identity informations --> <!-- Model identity informations -->
<identity> <identity>
<name>Modèle de test</name>
<author>Adrien Bourmault</author> <author>Adrien Bourmault</author>
<author_id>1</author_id> <author_id>1</author_id>
<date>2021-08-21-21h34m44s</date> <!-- TODO timestamp ? --> <date>2021-08-21-21h34m44s</date> <!-- TODO timestamp ? -->
<version>1.0</version> <version>1.0</version>
<name>Modèle de test</name>
</identity> </identity>
@ -75,6 +75,6 @@
<!-- Non context-specific tags --> <!-- Non context-specific tags -->
<ref id="" date="">https://www.a-lec.org</ref> <ref id="" date="">https://www.a-lec.org</ref>
<quote id="" date="" author="" lang="">Comment</quote> <quote id="" date="" author="" lang="">Quote</quote>
</gem-graph-model> </gem-graph-model>

View File

@ -157,9 +157,12 @@ struct {
// //
struct { struct {
int id; int id;
bool validated;
char *name; char *name;
char *author;
time_t date;
char *version;
char *filename; char *filename;
bool validated;
int space_xMax; int space_xMax;
int space_yMax; int space_yMax;
int space_zMax; int space_zMax;
@ -172,7 +175,8 @@ struct {
} typedef Model_t; } typedef Model_t;
struct ModelField_t { struct ModelField_t {
char id[25]; bool mandatory;
char tag[25];
char *value; char *value;
size_t valueSize; size_t valueSize;
struct ModelField_t *son; struct ModelField_t *son;

View File

@ -72,15 +72,21 @@ static inline int parseTag(const char* tagName, char *destination, char destSize
xmlNodePtr cur = currentDocumentRoot; 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) { 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))) { if ((!xmlStrcmp(cur->name, (const xmlChar *)tagName))) {
//printLog("\tFound %s\n", cur->name);
curString = (char*)xmlNodeListGetString(currentDocument, curString = (char*)xmlNodeListGetString(currentDocument,
cur->xmlChildrenNode, 1); 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); snprintf(destination, destSize, "%s\n", curString);
res++; res++;
} }
@ -96,7 +102,7 @@ static inline xmlNodePtr findRoot(const char *tagName, xmlNodePtr curRoot)
xmlNodePtr cur = curRoot; xmlNodePtr cur = curRoot;
xmlNodePtr recur; xmlNodePtr recur;
printLog("Asked for %s\n", tagName); //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))){
@ -115,30 +121,41 @@ static inline xmlNodePtr findRoot(const char *tagName, xmlNodePtr curRoot)
return NULL; return NULL;
} }
static inline int parseTree(ModelField_t *tree) static inline int parseTree(ModelField_t *curTree)
{ {
ModelField_t *curTree; int currentOrSonCorrectlyParsed = 0;
curTree = tree;
if ((findRoot(curTree->id, currentDocumentRoot)) == NULL)
return EBADF;
while (curTree) { while (curTree) {
printLog("Asked for %s\n", curTree->id); if (findRoot(
curTree->tag,
if ((parseTag(curTree->id, curTree->value, curTree->valueSize) == 0)) currentDocumentRoot->parent
) == NULL) {
printLog("Error : can't find a new root\n");
return EBADF; return EBADF;
}
if (curTree->value[0]) //printLog("Asked for %s\n", curTree->tag);
if (!(curTree->value[0] == '\0')) {
printLog("Field %s: %s", curTree->id, curTree->value); // 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) if (curTree->son)
parseTree(curTree->son); currentOrSonCorrectlyParsed |= parseTree(curTree->son);
curTree = curTree->next; curTree = curTree->next;
} }
return 0; return currentOrSonCorrectlyParsed;
} }

View File

@ -73,8 +73,16 @@ void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots
// cont. model population // cont. model population
knownModel[knownModelSize-1]->name = knownModel[knownModelSize-1]->name =
(char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE);
knownModel[knownModelSize-1]->filename = knownModel[knownModelSize-1]->filename =
(char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); (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_xMax = XMAX;
knownModel[knownModelSize-1]->space_yMax = YMAX; knownModel[knownModelSize-1]->space_yMax = YMAX;
knownModel[knownModelSize-1]->space_zMax = ZMAX; knownModel[knownModelSize-1]->space_zMax = ZMAX;
@ -175,10 +183,19 @@ void ModelDelete(int id) //XXX
{ {
free(knownModel[id-1]->name); free(knownModel[id-1]->name);
knownModel[id-1]->name = NULL; knownModel[id-1]->name = NULL;
free(knownModel[id-1]->filename); 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]); free(knownModel[id-1]);
knownModel[id-1] = NULL; knownModel[id-1] = NULL;
knownModelSize--; knownModelSize--;
} }
@ -273,32 +290,39 @@ static int ModelParseFile(Model_t *model)
ModelField_t dateField; ModelField_t dateField;
ModelField_t versionField; ModelField_t versionField;
strcpy(identityField.id, "identity"); char date[25];
strcpy(identityField.tag, "identity");
identityField.mandatory = false;
identityField.value = NULL; identityField.value = NULL;
identityField.valueSize = 0; identityField.valueSize = 0;
identityField.son = &authorField; identityField.son = &nameField;
identityField.next = NULL; identityField.next = NULL;
strcpy(nameField.id, "name"); strcpy(nameField.tag, "name");
nameField.mandatory = true;
nameField.value = model->name; nameField.value = model->name;
nameField.valueSize = 255; nameField.valueSize = 255;
nameField.son = NULL; nameField.son = NULL;
nameField.next = &dateField; nameField.next = &authorField;
strcpy(authorField.id, "author"); strcpy(authorField.tag, "author");
authorField.value = NULL; authorField.mandatory = true;
authorField.value = model->author;
authorField.valueSize = 25; authorField.valueSize = 25;
authorField.son = NULL; authorField.son = NULL;
authorField.next = &nameField; authorField.next = &dateField;
strcpy(dateField.id, "date"); strcpy(dateField.tag, "date");
dateField.value = NULL; dateField.mandatory = true;
dateField.valueSize = 12; dateField.value = date;
dateField.valueSize = 25;
dateField.son = NULL; dateField.son = NULL;
dateField.next = &versionField; dateField.next = &versionField;
strcpy(versionField.id, "version"); strcpy(versionField.tag, "version");
versionField.value = NULL; versionField.mandatory = true;
versionField.value = model->version;
versionField.valueSize = 15; versionField.valueSize = 15;
versionField.son = NULL; versionField.son = NULL;
versionField.next = NULL; versionField.next = NULL;